CentOS nginx + php-fpm + mysql host separation environment instance tutorial

Source: Internet
Author: User
Tags fpm install php mysql host mysql in openssl php server php and install wordpress

Experimental system: CentOS 6.6 _ x86_64

Prerequisites: most software is compiled and installed. Prepare the compiling environment in advance, and disable firewall and selinux.

Experimental Software: nginx-1.9.3 mariadb-10.0.20 php-5.6.11 memcache-2.2.7 xcache-3.2.0

Tutorial topology:



1. Install nginx

1. Solve the dependency:

Install the pcre-devel package:

Yum-y install pcre-devel

2. Add nginx users:

Useradd-r nginx

3. Decompress and compile and install nginx:


Tar xf nginx-1.9.3.tar.gz
Cd nginx-1.9.3
./Configure \
-- Prefix =/usr/local/nginx \ // installation location
-- Sbin-path =/usr/local/nginx/sbin/nginx \ // program file
-- Conf-path =/etc/nginx. conf \ // installation location of the configuration file
-- Error-log-path =/var/log/nginx/error. log \ // location where error logs are installed
-- Http-log-path =/var/log/nginx/access. log \ // access log installation location
-- Pid-path =/var/run/nginx. pid \ // pid file location
-- Lock-path =/var/lock/nginx. lock \ // lock file location
-- User = nginx \ // user identity used to run the process
-- Group = nginx \ // User group used to run processes
-- With-http_ssl_module \ // supports ssl module
-- With-http_flv_module \ // supports flv module
-- With-http_stub_status_module \ // support stub_status module
-- With-http_gzip_static_module \ // supports gzip_static module
-- Http-client-body-temp-path =/var/tmp/nginx/client // stores the temporary file location of the HTTP client request body
-- Http-proxy-temp-path =/var/tmp/nginx/proxy // store the temporary file location of the HTTP proxy
-- Http-fastcgi-temp-path =/var/tmp/nginx/fcgi // store the location of the fasycgi temporary file
-- Http-uwsgi-temp-path =/var/tmp/nginx/uwsgi \ // store The uwsgi temporary file location
-- Http-scgi-temp-path =/var/tmp/nginx/scgi \ // store the scgi temporary file location
-- With-pcre // supports the pcre library
Make & make install


4. Provide the script file:

Vim/etc/init. d/nginx
--------------------------------
#! /Bin/sh
#
# Nginx-this script starts and stops the nginx daemon
#
# Chkconfig:-85 15
# Description: Nginx is an HTTP (S) server, HTTP (S) reverse \
# Proxy and IMAP/POP3 proxy server
# Processname: nginx
# Config:/etc/nginx. conf
# Config:/etc/sysconfig/nginx
# Pidfile:/var/run/nginx. pid
 
# Source function library.
./Etc/rc. d/init. d/functions
 
# Source networking configuration.
./Etc/sysconfig/network
 
# Check that networking is up.
["$ NETWORKING" = "no"] & exit 0
 
Nginx = "/usr/local/nginx/sbin/nginx"
Prog = $ (basename $ nginx)
 
NGINX_CONF_FILE = "/etc/nginx. conf"
 
[-F/etc/sysconfig/nginx] &./etc/sysconfig/nginx
 
Lockfile =/var/lock/subsys/nginx
 
Make_dirs (){
# Make required directories
User = 'nginx-V 2> & 1 | grep "configure arguments:" | sed's/[^ *] * -- user = \ ([^] * \). */\ 1/g '-'
Options = '$ nginx-V 2> & 1 | grep' configure arguments :''
For opt in $ options; do
If ['echo $ opt | grep'. *-temp-Path'']; then
Value = 'echo $ opt | cut-d "="-f 2'
If [! -D "$ value"]; then
# Echo "creating" $ value
Mkdir-p $ value & chown-R $ user $ value
Fi
Fi
Done
}
 
Start (){
[-X $ nginx] | exit 5
[-F $ NGINX_CONF_FILE] | exit 6
Make_dirs
Echo-n $ "Starting $ prog :"
Daemon $ nginx-c $ NGINX_CONF_FILE
Retval =$?
Echo
[$ Retval-eq 0] & touch $ lockfile
Return $ retval
}
 
Stop (){
Echo-n $ "Stopping $ prog :"
Killproc $ prog-QUIT
Retval =$?
Echo
[$ Retval-eq 0] & rm-f $ lockfile
Return $ retval
}
 
Restart (){
Configtest | return $?
Stop
Sleep 1
Start
}
 
Reload (){
Configtest | return $?
Echo-n $ "Reloading $ prog :"
Killproc $ nginx-HUP
RETVAL =$?
Echo
}
 
Force_reload (){
Restart
}
 
Configtest (){
$ Nginx-t-c $ NGINX_CONF_FILE
}
 
Rh_status (){
Status $ prog
}
 
Rh_status_q (){
Rh_status>/dev/null 2> & 1
}
 
Case "$1" in
Start)
Rh_status_q & exit 0
$1
;;
Stop)
Rh_status_q | exit 0
$1
;;
Restart | configtest)
$1
;;
Reload)
Rh_status_q | exit 7
$1
;;
Force-reload)
Force_reload
;;
Status)
Rh_status
;;
Condrestart | try-restart)
Rh_status_q | exit 0
;;
*)
Echo $ "Usage: $0 {start | stop | status | restart | condrestart | try-restart | reload | force-reload | configtest }"
Exit 2
Esac
--------------------------------
Chmod + x/etc/init. d/nginx
Chkconfig -- add nginx
Chkconfig nginx on
Service nginx start


5. Test the access page. nginx installation is complete:



II. Install mysql

1. Add a mysql User and create a data directory:

Useradd-r mysql
Mkdir-pv/mydata/data
Chown-R mysql: mysql/mydata/data

2. Decompress and initialize mysql:

Tar xf mariadb-10.0.20-linux-x86_64.tar.gz-C/usr/local/
Cd/usr/local/
Ln-sv mariadb-10.0.20-linux-x86_64 mysql
Cd mysql/
Chown-R root: mysql.
Scripts/mysql_install_db -- user = mysql -- datadir =/mydata/data/

3. Provide the configuration file:

Cp support-files/my-large.cnf/etc/my. cnf
Vim/etc/my. cnf
Modify the value of thread_concurrency in this file to multiply the number of your CPUs by 2, for example, thread_concurrency = 2.
In addition, you must add the following lines to specify the storage location of mysql data files: datadir =/mydata/data

4. Provide the script file:

Cp support-files/mysql. server/etc/init. d/mysqld
Chkconfig -- add mysqld
Chkconfig mysqld on
Service mysqld start

Use bin/mysql in the mysql directory to connect to mysql. The following figure shows that the connection is successful:


5. Output the mysql man manual to the man command search path:

Edit/etc/man. config and add the following lines: MANPATH/usr/local/mysql/man.
6. Output the mysql header file to the system header file path/usr/include:
This can be achieved through simple creation links:

Ln-sv/usr/local/mysql/include/usr/include/mysql

7. Output the mysql database file to the system database search path:

Echo '/usr/local/mysql/lib'>/etc/ld. so. conf. d/mysql. conf
Ldconfig

3. Install PHP

1. Solve the dependency:

Yum-y install libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel

2. Compile and install php:

./Configure -- prefix =/usr/local/php \ // installation location
-- With-mysql \ // supports mysql
-- With-pdo-mysql \ // supports the pdo module
-- With-mysqli \ // supports the mysqli module
-- With-openssl \ // supports the openssl module
-- Enable-fpm \ // supports fpm mode
-- Enable-sockets \ // enable socket support
-- Enable-sysvshm \ // enable system shared memory support
-- Enable-mbstring \ // enable multi-byte string support
-- With-freetype-dir \ // set the path of FreeType installation prefix
-- With-jpeg-dir \ // set the path of libjpeg installation prefix
-- With-png-dir \ // Set the libpng installation prefix path
-- With-zlib-dir \ // set the path of libz installation prefix
-- With-libxml-dir =/usr \// sets the libxml2 installation path.
-- Enable-xml \
-- With-mhash \ // mhash supported
-- With-mcrypt \ // supports mcrypt
-- With-config-file-path =/etc \ // configuration file path
-- With-config-file-scan-dir =/etc/php. d \ // scan path of the configuration file
-- With-bz2 \ // supports BZip2
-- With-curl // supports curl
Make & make install

3. Provide the configuration file:

Cp php. ini-production/etc/php. ini

4. Provide scripts for php-fpm:

Cp sapi/fpm/init. d. php-fpm/etc/init. d/php-fpm
Chmod + x/etc/init. d/php-fpm
Chkconfig -- add php-fpm
Chkconfig php-fpm on

5. Provide the php-fpm configuration file and edit it:

Cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf
------------------------------------------------------
Pm. max_children = 150
Pm. start_servers = 8
Pm. min_spare_servers = 5
Pm. max_spare_servers = 10
Pid =/usr/local/php/var/run/php-fpm.pid

6. Start the php-fpm service:

Service php-fpm start

IV. Integrate nginx and PHP

1. The nginx server creates a webpage file storage directory/www and modifies its permissions:

Mkdir/www
Chown-R nginx: nginx/www

2. Modify the nginx configuration file:

Vim/etc/nginx. conf
--------------------------------------
Location /{
Root/www;
Index. php index.html index.htm;
    }

Location ~ \. Php $ {
Root/www;
Fastcgi_pass 192.168.19.92: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Include fastcgi_params;
    }

3. Modify the fastcgi_params file:

Fastcgi_param GATEWAY_INTERFACE CGI/1.1;
Fastcgi_param SERVER_SOFTWARE nginx;
Fastcgi_param QUERY_STRING $ query_string;
Fastcgi_param REQUEST_METHOD $ request_method;
Fastcgi_param CONTENT_TYPE $ content_type;
Fastcgi_param CONTENT_LENGTH $ content_length;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Fastcgi_param SCRIPT_NAME $ fastcgi_script_name;
Fastcgi_param REQUEST_URI $ request_uri;
Fastcgi_param DOCUMENT_URI $ document_uri;
Fastcgi_param DOCUMENT_ROOT $ document_root;
Fastcgi_param SERVER_PROTOCOL $ server_protocol;
Fastcgi_param REMOTE_ADDR $ remote_addr;
Fastcgi_param REMOTE_PORT $ remote_port;
Fastcgi_param SERVER_ADDR $ server_addr;
Fastcgi_param SERVER_PORT $ server_port;
Fastcgi_param SERVER_NAME $ server_name;


4. Create an nginx user on the PHP Server. Ensure that the user ID and group ID of the nginx server are consistent with that of the nginx server:
5. Modify the php-fpm configuration file and restart:

Vim/usr/local/php/etc/php-fpm.conf
---------------------------------------------
Listen = 192.168.19.92: 9000 // listen to the address of the physical Nic for other machines to call
User = nginx // php-fpm run as nginx user
Group = nginx
---------------------------------------------
Service php-fpm restart

6. Create the/www directory on the PHP server and enable the nfs service:

Mkdir/www
Chown-R nginx: nginx/www
Vim/etc/exports
---------------------------------------------
/Www 192.168.19.0/24 (rw, no_root_squash)
---------------------------------------------
Service nfs start

7. Mount the nfs file on the nginx server and test php. After the test is successful, delete index. php:

Mount-t nfs 192.168.19.92:/www
Vim/www/index. php
---------------------------------------
<? Php
Phpinfo ();
?>
--------------------------------------
Service nginx restart



5. Integrate PHP and MYSQL

Create a database and user that can be accessed by the php server on the mysql server:

/Usr/local/mysql/bin/mysql
--------------------------------------------
Create database wordpress;
Grant all on wordpress. * TO 'wordpress' @ '192. 168.19.92 'identified BY '123 ';
Flush privileges;

6. Install wordpress

1. Put a webpage file in the/www folder

2. Access http: // 192.168.19.83 and install it as Prompted. If the configuration is correct, the installation is successful.


 

7. Install xcache for php

1. Unzip and install:

Tar xf xcache-3.2.0.tar.gz
Cd xcache-3.2.0
/Usr/local/php/bin/phpize
./Configure -- enable-xcache -- with-php-config =/usr/local/php/bin/php-config
Make & make install

After completion, the following error occurs: Installing shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

2. Load the module:

Vim/etc/php. ini
-----------------------------------
Extension =/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache. so // find the place where the extension configuration is located.



3. After the two Acceleration Modules are installed, restart php-fpm:

Service php-fpm restart

4. Create a php test page and check whether the module has been loaded successfully:

Related Article

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.