LNMP分离

很多人在搭建的时候都是使用的一台机器来部署LNMP环境,但是我们在实际的工作中一般都是分离部署的。也就是说MySQL是MySQL;它是一台单机,分离部署自己跑自己的服务,提高效率!

三台机器来部署LNMP环境,如下:

LNMP分离

OS:Centos7.3x86_64

Nginx-1.12.2 IP地址:192.168.10.101

PHP-5.5 IP地址:192.168.10.102

MySQL5.7 IP地址:192.168.10.103

 

 

一、首先安装Nginx(1.10主机操作)

 

1)安装相关的依赖包

[[email protected] ~]# yum install gcc gcc-c++ openssl-devel zlib-devel zlib pcre-devel -y  

 

2)建立Nginx用户指定id

[[email protected] ~]# groupadd -g 1001 nginx  

[[email protected] ~]#useradd -u 900 nginx -g nginx -s /sbin/nologin  

[[email protected] ~]# tail -1 /etc/passwd  

nginx:x:900:1001::/home/nginx:/sbin/nologin  

 

3)下载Nginx源码安装包并安装

[[email protected] src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz  

[[email protected] src]# tar zxf nginx-1.12.2.tar.gz   

[[email protected] src]# cd nginx-1.12.2/  

[[email protected]]#./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx && make && make install 

 

4)创建软连接并启动Nginx访问测试

[[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  

[[email protected] nginx-1.12.2]# nginx -t  

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok  

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful  

[[email protected] nginx-1.12.2]# nginx  

[[email protected] nginx-1.12.2]# netstat -anput | grep nginx  

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      27185/nginx: master   

 

LNMP分离
到这里Nginx已经安装完毕,接下来安装php 

 

二、安装PHP(10.102主机操作)

 

1)安装相关的依赖包

[[email protected] ~]# yum -y install gcc* libxml2-devel libcurl-devel openssl-devel bzip2-devel

 

2)安装libmcrypt

[[email protected] ~]# tar zxvf libmcrypt-2.5.8.tar.gz

[[email protected] ~]# cd libmcrypt-2.5.8/

 

[[email protected] libmcrypt-2.5.8]# ./configure

 

[[email protected] libmcrypt-2.5.8]# make && make install

 

  

 

3)正式安装php 

[[email protected] ~]# tar zxvf php-5.5.38.tar.gz

[[email protected] ~]# cd php-5.5.38/

[[email protected] php-5.5.38]#./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install 


编译安装完后我们创建Nginx用户,要求UID和GID和Nginx机器上的一致!

[[email protected] php-5.6.27]# groupadd -g 1001 nginx  

[[email protected] php-5.6.27]# useradd -u 900 nginx -g nginx -s /sbin/nologin   

[[email protected] php-5.6.27]# tail -1 /etc/passwd  

nginx:x:900:1001::/home/nginx:/sbin/nologin  

提供PHP的配置文件及提供脚本

[[email protected] php-5.5.38]# cp php.ini-production /etc/php.ini

[[email protected] php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm    

[[email protected] php-5.5.38]# chmod +x /etc/init.d/php-fpm

[[email protected] php-5.5.38]# chkconfig --add php-fpm

 

[[email protected] php-5.5.38]# chkconfig php-fpm on

修改php主配置,并编辑如下:

 

[[email protected] php-5.5.38]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

 

[[email protected] php-5.5.38]# vi /usr/local/php/etc/php-fpm.conf

修改内容如下:

pid = run/php-fpm.pid
user = nginx
group = nginx
listen = 192.168.10.102:9000  //PHP主机的IP地址
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

修改完成后启动php服务

[[email protected] php-5.5.38]# service php-fpm restart

LNMP分离

到这里我们的php完成!接下来安装MySQL

 

三、安装MySQL(10.103主机操作)

注:可以用yum安装数据库,那么下面安装mysql的步骤就不需要了

在centos7中默认自带了mariadb需要卸载

 

[[email protected] ~]# rpm -qa | grep mariadb  

mariadb-libs-5.5.44-2.el7.centos.x86_64  

[[email protected] ~]# rpm -e mariadb-libs-5.5.44-2.el7.centos.x86_64 --nodeps 

2)解压MySQL二进制包把解压的文件拷贝到/usr/local/mysql下,然后进入mysql目录查看

 

[[email protected] src]# tar zxf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz   

[[email protected] src]# mv mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql  

[[email protected] src]# cd /usr/local/mysql/  

[[email protected] mysql]# ll  

总用量 40  

drwxr-xr-x.  2 root root   4096 1月  21 16:05 bin  

-rw-r--r--.  1 7161 31415 17987 3月  18 2017 COPYING  

drwxr-xr-x.  2 root root     52 1月  21 16:05 docs  

drwxr-xr-x.  3 root root   4096 1月  21 16:05 include  

drwxr-xr-x.  5 root root   4096 1月  21 16:05 lib  

drwxr-xr-x.  4 root root     28 1月  21 16:05 man  

-rw-r--r--.  1 7161 31415  2478 3月  18 2017 README  

drwxr-xr-x. 28 root root   4096 1月  21 16:05 share  

drwxr-xr-x.  2 root root     86 1月  21 16:05 support-files  

 

3)优化执行路径

 

[[email protected] mysql]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile   

[[email protected] mysql]# source /etc/profile  

4)创建data以及log目录如下:

 

[[email protected] mysql]# cd /usr/local/mysql/  

[[email protected] mysql]# mkdir data  

[[email protected] mysql]# mkdir log  

5)创建mysql用户和组

 

[[email protected] mysql]# groupadd mysql && useradd -r -g mysql -s /bin/false mysql  

6)建立MySQL主配置文件

 

[[email protected] mysql]# vim /etc/my.cnf  

[[email protected] mysql]# cat /etc/my.cnf   

[client]    

socket=/usr/local/mysql/mysql.sock    

[mysqld]    

basedir=/usr/local/mysql    

datadir=/usr/local/mysql/data    

pid-file=/usr/local/mysql/data/mysqld.pid    

socket=/usr/local/mysql/mysql.sock    

log_error=/usr/local/mysql/log/mysql.err  

7)设置相应的权限并初始化MySQL

 

[[email protected] mysql]# chmod 750 data/ && chown -R mysql . && chgrp -R mysql . && bin/mysqld --initialize --user=mysql  

8)复制启动脚本并启动MySQL数据库

 

[[email protected] mysql]#  cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && service mysqld start  

查看mysql是否启动成功(Mysql密码存放在:/usr/local/mysql/log/mysql.err文件中) 

LNMP分离
到这里MySQL正式安装完毕

 

四、配置Nginx支持PHP环境(1.10主机操作)

[[email protected] ~]#vim /usr/local/nginx/conf/nginx.conf

 

location / {  

            root   /www;  更改网页目录  

            index  index.php index.html index.htm;  添加index.php

        }  

location ~ \.php$ {  

            root           /www;    更改目录  

            fastcgi_pass   192.168.10.102:9000;  注意:在这里添加PHP主机IP地址  

            fastcgi_index  index.php;  

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  

            include        fastcgi_params;  

        }  

[[email protected] ~]# vim /usr/local/nginx/conf/fastcgi_params添加如下: 

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

添加完成后创建/www目录并创建测试页如下:

 

[[email protected] ~]# mkdir /www  

[[email protected] ~]# chown nginx:nginx /www/  

[[email protected] ~]# cd /www/  

[[email protected] www]# vim index.php  

[[email protected] www]# cat index.php   

<?php  

phpinfo();  

?>  

到这里我们先别重启Nginx接下来在PHP主机操作(1.20)

 

[[email protected] ~]# mkdir /www  

[[email protected] ~]# chown -R nginx:nginx /www/  

[[email protected] ~]# cd /www/  

[[email protected] www]# vim index.php  

[[email protected] www]# cat index.php   

<?php  

phpinfo();  

?>  

创建完成后接下来我们重启nginx以及php,访问测试页(1.10&1.20操作)

 

[[email protected] www]# nginx -s reload  

[[email protected] www]# service php-fpm restart   

LNMP分离

访问到如上图那么证明我们的nginx支持PHP了接下来我们部署wordpress个人站点

在部署前我们在数据库服务器上创建wordpress数据库及授权帐户(Mysql服务器操作1.30)

 

mysql> create database wordpress;  

Query OK, 1 row affected (0.00 sec)  

mysql> grant all on wordpress.* to [email protected]'%' identified by '123456';  

Query OK, 0 rows affected, 1 warning (0.00 sec)  

解压wordpress压缩包(Nginx1.10操作)

 

[[email protected] ~]# tar zxf wordpress-4.9.1-zh_CN.tar.gz   

[[email protected] ~]# mv wordpress/* /www/  

在php主机上同样的操作

[[email protected] ~]# tar zxf wordpress-4.9.1-zh_CN.tar.gz   

[[email protected] ~]# mv wordpress/* /www  

访问测试:http://192.168.10.101/

LNMP分离

LNMP分离