Install and configure php + nginx + mysql in linux

Source: Internet
Author: User
: This article mainly introduces the installation and configuration of php + nginx + mysql in linux. if you are interested in the PHP Tutorial, refer to it. I am mainly used to install php and interact with nginx and php.

1. install the plug-in


You can choose YUM installation or source code compilation to install gccgcc-c ++ zlibpcrepcre-devellibeventlibevent-devellibxml2 libxml2-devellibmcrypt libmcrypt-develcurl-devellibpng-devellibtool-ltdl-develgd-developenssl openssl-develncurses-develcmakemysql-devel

2. install mysql

Tar-zxvf mysql-5.5.25.tar.gz decompress the mysql package and place it in the location where you want mysql to be installed. for example, the/usr/local/webserver/mysql cmake command in this example requires this path cmake \-DCMAKE_INSTALL_PREFIX =/usr/ local/webserver/mysql \-Consumer =/user/local/webserver/mysql/data \-DSYSCONFDIR =/etc \-DEXTRA_CHARSETS = all \-DDEFAULT_CHARSET = utf8 \-DDEFAULT_COLLATION = utf8_general_ci \-DWITH_INNOBASE_STORAGE_ENGINE = 1 \-DWITH_ARCHIVE_STORAGE_ENGINE = 1 \-DWITH_BLACKH OLE_STORAGE_ENGINE = 1 \-DWITH_FEDERATED_STORAGE_ENGINE = 1 \-DWITH_PARTITION_STORAGE_ENGINE = 1 \-runtime = 1 \-DMYSQL_UNIX_ADDR =/tmp/mysqld. sock \-DMYSQL_TCP_PORT = 3306 \-DWITH_DEBUG = 0 \-DENABLED_LOCAL_INFILE = 1 press enter to execute, after the execution is complete, continue to execute make & makeinstall # set Mysql # There are five configuration information files in the support-files directory (this is important here, be sure to copy the corresponding cnf file according to your memory, otherwise mysql will never start): # my-small.cnf (memory <= 64 M) # my-medium.cnf (Memory 128 M) # my-lar Ge. cnf (memory 512 M) # my-huge.cnf (memory 1G-2G) # my-innodb-heavy-4G.cnf (memory 4 GB) cd/usr/local/webserver/mysql cp. /support-files/my-huge.cnf/etc/my. cnf vi/etc/my. cnf # Add datadir =/usr/local/webserver/mysql/data wait-timeout = 30 max_connections = 512 default-storage-engine = MyISAM # in [mysqld] segment modification max_allowed_packet = 16 M // add mysql running user and user group groupadd mysqluseradd-g mysql-s/bin/false-d/h Ome/mysql // no shell, do not log on to the local machine (for security reasons) cd/usr/local/webserver/mysql chown-R root. chown-R mysql data chgrp-R mysql. // generate a new mysql authorization table // enter the script directory cd/usr/local/webserver/mysql/scripts // generate a new mysql authorization table using the mysql_install_db script. /mysql_install_db -- basedir =/usr/local/webserver/mysql -- datadir =/usr/local/webserver/mysql/data -- user = mysql // mysql server service item settings in the system // Copy the service file and modify cd/usr/local/webserver/mysq L/support-files cp mysql. server mysqld // modify mysqld basedir =/usr/local/webserver/mysqldatadir =/usr/local/webserver/mysql/data mv mysqld/etc/init. d/mysqld chmod755/etc/init. d/mysqld after performing the preceding operations, we can use service mysqld start to start the mysql service. // Set the soft connection to enable mysql, mysqldump, the three bin commands of mysqladmin can directly run sudoln-s/usr/local/webserver/mysql/bin/mysql/usr/bin sudoln-s/usr/local/webserver in shell. /mysql/bin/mysqldump/usr/bin s Udoln-s/usr/local/webserver/mysql/bin/mysqladmin/usr/bin rm-rf/etc/mysql/my. cnf has copied this file to/etc/my. if cnf is not deleted, mysql cannot be started. /Etc/init. d/mysqld start // Set the root password mysqladmin-u root password "admin" // mysql database Chinese garbled solution vi/etc/my. cnf // then add character-set-server = utf8 // under the [mysqld] configuration option and enter mysql cd/usr/local/webserver/mysql/binmysql-u root-p prompt to enter the password mysql> show variables like '% character % ';

3. install Nginx

# Tar zxvf nginx-0.8.24.tar.gz # D nginx-0.8.24 #. /configure -- prefix =/usr/local/nginx // in this section, you only need to specify a path # make & makeinstall #/usr/local/nginx/sbin/nginx // start compile the service script in Nginx (do not copy the service script. please write it in linux or press enter to break the line character, causing an exception) vi/etc/init. d/nginx write the following content into the file and save #! /Bin/bash # nginx Startup script for the Nginx HTTP Server # this script create it by gcec at 2009.10.22. # it is v.0.0.1 version. # if you find any errors on this scripts, please contact gcec cyz. # and send mail to support at gcec dot cc. # chkconfig:-85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: ngi Nx # pidfile:/var/run/nginx. pid # config:/usr/local/nginx/conf/nginx. conf nginxd =/usr/local/nginx/sbin/nginx # set nginx_config =/usr/local/nginx/conf/nginx as the path for installing nginx. conf # set nginx configuration file location nginx_pid =/var/run/nginx. pid RETVAL = 0 prog = "nginx" # Source function library .. /etc/rc. d/init. d/functions # Source networking configuration .. /etc/sysconfig/network # Check that networking is up. [$ {N ETWORKING} = "no"] & exit 0 [-x $ nginxd] | exit 0 # Start nginx daemons functions. start () {if [-e $ nginx_pid]; then echo "nginx already running .... "exit 1 fi echo-n $" Starting $ prog: "daemon $ nginxd-c $ {nginx_config} RETVAL = $? Echo [$ RETVAL = 0] & touch/var/lock/subsys/nginx return $ RETVAL} # Stop nginx daemons functions. stop () {echo-n $ "Stopping $ prog:" killproc $ nginxd RETVAL =$? Echo [$ RETVAL = 0] & rm-f/var/lock/subsys/nginx/var/run/nginx. pid} # reload nginx service functions. reload () {echo-n $ "Reloading $ prog:" # kill-HUP 'cat $ {nginx_pid} 'killproc $ nginxd-hup retval =$? Echo} # See how we were called. case "$1" in start) start; stop) stop; reload) reload; restart) stop start; status) status $ prog RETVAL = $?; *) Echo $ "Usage: $ prog {start | stop | restart | reload | status | help} "exit 1 esacexit $ RETVAL save and grant the file permission chmod 755/etc/init. d/nginx we can start the service through service nginx start.

4. install php

create user and group for fpm(fastcgi process manager)groupadd fpmuseradd --shell /sbin/nologin -g fpm fpmdownload, configure and install php5.3.3wget http://www.php.net/distributions/php-5.3.3.tar.gztar zxvf php-5.3.3.tar.gzcd php-5.3.3 
[Copy directly]. /configure -- prefix =/usr/local/php -- enable-fpm -- with-fpm-user = fpm -- with-fpm-group = fpm -- with-config-file-path = /usr/local/php/lib -- with-mysql = mysqlnd -- with-mysqli = mysqlnd -- with-pdo-mysql = mysqlnd -- without-pdo-sqlite -- without-sqlite3 -- without-sqlite -- with-curl -- enable-mbstring -- with-mhash -- with-mcrypt -- with-openssl -- with-gd -- enable-sockets -- with-gettext -- with-zlib -- enable-zip -- enable-soap -- with-xmlrpc -- with-freetype-dir =/usr/local/freetype -- enable-gd-native-ttf
--disable-fileinfo
For errors in the middle, you need to install several dependent packages in yum install [manual version]. /configure -- prefix =/usr/local/php \ -- enable-fpm \ -- with-fpm-user = fpm \ -- with-fpm-group = fpm \ -- with-config- file-path =/usr/local/php/lib # configure php. storage location of ini -- with-mysql = mysqlnd \ -- with-mysqli = mysqlnd \ -- with-pdo-mysql = mysqlnd \ -- without-pdo-sqlite \ -- without-sqlite3 \ --- sqlite \ -- with-mysql-sock =/tmp/mysql. sock \ -- with-curl \ -- enable-mbstring \ -- with-mhash \ -- with-mcrypt \ -- with-openssl \ -- with-gd \ -- enable-sockets \ --- gettext \ -- with-zlib \ -- enable-zip \ -- enable-soap \ -- with-xmlrpc \ -- with-freetype-dir =/usr/local/freetype \ -- enable-gd -native-ttf \ -- with-jpeg-dir =/usr/lib64 #64-bit lib64 32-bit lib32 make & makeinstall make error virtual memory exhausted: cannot allocate memory, add-disable-fileinfo to configure
If an error occurs in mysql_config not found, solution: add export PATH = "$ PATH: /usr/local/mysql/bin "is the directory where your mysql is installed.


5. configure php

Cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf copy source package php. ini-development to/usr/local/php/lib vi php-fpm.conf find "listen =" change to listen =/dev/shm/php-fpm.sock (require php version 5.3 or above this method to use sock file listening) cp/Backups/php-5.3.3/php. ini-development/usr/local/php/lib/php. start php/usr/local/php/sbin/php-fpm in ini. if the path is set correctly. the INI file also exists and php cannot be loaded. modify the startup command/usr/local/php/sbin/php-fpm-c/etc/php. write a service script in ini. (do not copy the service script. write it in linux. Otherwise, the carriage return or line break will cause an exception.) touch/etc/init. d/phpfpmvim/etc/init. the content of d/phpfpm is as follows:

#! /Bin/bash

Start (){

/Usr/local/php/sbin/php-fpm

/Bin/echo 'starting php listener --- [OK]'

}

Stop (){

/Usr/bin/pkill php-fpm

/Bin/echo 'stopping php listener --- [OK]'

}

Case "$1" in

Start)

Start

;;

Stop)

Stop

;;

Restart)

Stop

Start

;;

*)

Echo 'Usage: start/stop/restart'

Exit 0

;;

Esac

Save and exit

Then you can start the listener through service phpfpm start/stop/restart.

6. Configure Nginx

Cat/usr/local/php/etc/php-fpm.conf view port 127.0.0.1: 9000 modify nginx configuration file/usr/local/nginx/conf/nginx. conf # location/{// note this part. Otherwise, the PHP file will be downloaded instead of being parsed. # root html; # index index.html index.htm ;#}
Location ~ \. Php {root www; # This is the root directory of your website, fastcgi_pass 127.0.0.1: 9000; # the port for listening to the fastcgi process is specified here, and nginx interacts with php # fastcgi_pass unix: /dev/shm/php-fpm.sock;
Fastcgi_index index. php; fastcgi_param SCRIPT_FILENAME $ document_root/$ fastcgi_script_name; # because SCRIPT_FILENAME is written dead in the configuration, it does not change with $ doucument_root, we can modify the SCRIPT_FILENAME configuration as FOLLOWS # fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; include fastcgi_params ;} restart the nginx service/usr/local/nginx/sbin/nginx-s reload and create the www directory mkdir www under/usr/local/nginx to create an index. php file cd wwwvim index. php write
 If the access server works, the configuration is successful.

7. set php nginx mysql to start automatically

I want to disable the graphic interface in centos. select the recommended system running level 2 or 3. check/etc/init before configuring. d. Are there any mysql, php, and nginx service scripts? If not, configure the following operations first, for example, mysqld, nginx, phpfpm scripts are compiled and put into/etc/init. d. let's configure automatic startup. I want to use a service to start these three services. then I can write another script.

Note: All system services can be started using the service, and chkconfig can be used to add and del

However, when you use chkconfig to configure some services configured by yourself, the system prompts "service XX does not support chkconfig"

This is generally caused by a script that does not conform to the format. the solution is as follows,

Add two lines of content to the script:

# Chkconfig:-85 15

# Description: this is a World Wide Web server.

Mv/etc/init. d/mysqld/etc/init. d/webapp-mysqldmv/etc/init. d/nginx/etc/init. d/webapp-nginxmv/etc/init. d/phpfpm/etc/init. d/webapp-phpfpmtouch/etc/init. d/webappvim/etc/init. d/webapp write the following script

#! /Bin/bash

# Chkconfig:-85 15
# Description: this is a World Wide Web server.

ACTION = $1

If ["$ ACTION" = ""] | ["$ ACTION" = "start"]; then

# Start php listeners

/Sbin/service webapp-phpfpm start

# Start nginx service

/Sbin/service webapp-nginx start

# Start mysql service

/Sbin/service webapp-mysqld start

Echo "web applications [mysql, nginx, php] is running now! "

Elif ["$ ACTION" = "stop"]; then

/Sbin/service webapp-phpfpm stop

/Sbin/service webapp-nginx stop

/Sbin/service webapp-mysqld stop

Echo 'web application stopped'

Else

Echo "use start or stop or none after your service command"

Fi

Add system service startup

Chkconfig -- add webapp (note that the service script under/etc/init. d must add the content of # chkconfig and # description to support the chkconfig command here, as mentioned above)

Chkconfig -- level 3 webapp on

In this way, our lamp architecture is successfully configured.

Note:

Syntax:

Chkconfig -- list [name] is used to list services.

Chkconfig -- add name is used to add a service

Chkconfig -- del name is used to delete a service.

Chkconfig [-- level levels] name changes the startup information and checks the startup status of a specific service.

On and off indicate the start and stop of the service when the running level is changed. Reset refers to initializing service information.

For on and off switches, the system is only valid for 3, 4, and 5 by default, but the reset can be valid for all running levels.


The above describes the installation and configuration of php + nginx + mysql in linux, including the installation of mysql. if you are interested in the PHP Tutorial, please help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.