LNMP分离安装
很多人在搭建的时候都是使用的一台机器来部署LNMP环境,但是我们在实际的工作中一般都是分离部署的。也就是说MySQL是MySQL;它是一台单机,分离部署自己跑自己的服务,提高效率!
三台机器来部署LNMP环境,如下:
OS:Centos7.3x86_64
Nginx-1.12.2 IP地址:192.168.10.101
PHP-5.5 IP地址:192.168.10.103
MySQL5.7 IP地址:192.168.10.105
NFS的IP地址:192.168.10.105
一:NFS的安装
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install nfs-utils rpcbind
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
(2)设置共享目录
[root@localhost ~]# mkdir -p /opt/wwwroot
[root@localhost ~]# vi /etc/exports
/opt/wwwroot 192.168.10.0/24(rw,sync,no_root_squash)
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind
二、安装Nginx
1)安装相关的依赖包
[root@nginx ~]# yum install -y gcc* zlib-devel pcre-devel
2)建立Nginx用户指定id
[root@nginx ~]#useradd -u 1001 nginx
3)下载Nginx源码安装包并安装
[root@nginx src]# tar zxf nginx-1.12.2.tar.gz
[root@nginx src]# cd nginx-1.12.2/
[root@nginx-1.12.2]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
4)创建软连接并启动Nginx访问测试
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx 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
[root@nginx nginx-1.12.2]# nginx
[root@nginx nginx-1.12.2]# netstat -anput | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27185/nginx: master
[root@localhost ~]#mkdir /www
[root@localhost ~]# mount 192.168.10.105:/opt/wwwroot /www
到这里Nginx已经安装完毕,接下来安装php
二、安装PHP
1)安装相关的依赖包
[root@localhost ~]# yum -y install gcc* libxml2-devel pcre-devel zlib-devel
3)正式安装php
[root@localhost ~]#useradd nginx
[root@localhost ~]# tar zxvf php-5.5.38.tar.gz
[root@localhost ~]# cd php-5.5.38/
[root@localhost php-5.5.38]# ./configure --prefix=/usr/local/php --with-mysql --with-mysqli --enable-fpm && make && make install
提供PHP的配置文件及服务脚本
[root@localhost php-5.5.38]# cp php.ini-production /etc/php.ini
提供php-fpm的服务控制脚本
[root@php php-5.5.38]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.5.38]# chmod +x /etc/init.d/php-fpm
[root@localhost php-5.5.38]# chkconfig --add php-fpm
[root@localhost php-5.5.38]# chkconfig php-fpm on
[root@localhost php-5.5.38]# cd /usr/local/php/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# ln -s /usr/local/php/sbin/* /usr/local/sbin/
[root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@localhost php-5.5.38]# vi /usr/local/php/etc/php-fpm.conf
修改内容如下:
pid = run/php-fpm.pid
listen = 192.168.10.103:9000 //PHP主机的IP地址
修改完成后启动php服务
[root@localhost etc]# /usr/local/sbin/php-fpm
到这里我们的php完成!接下来安装MySQL
三、安装MySQL
注:可以用yum安装数据库,那么下面安装mysql的步骤就不需要了
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install mariadb-server mariadb
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysqladmin -u root password "pwd123"
四、配置Nginx支持PHP环境
[root@nginx ~]#vim /usr/local/nginx/conf/nginx.conf
user nginx;
location / {
root /www; 更改网页目录
index index.php index.html index.htm; 添加index.php
}
location ~ \.(gif|jpg|jpeg|png|bmp|swf)$ {
root /www;
}
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.conf;
}
如果要做php的负载均衡,nginx的配置文件修改如下:
upstream wordpress {
server 192.168.10.101:9000;
server 192.168.10.102:9000;
}
server {
listen 80;
server_name localhost;
root /www;
charset utf8;
location / {
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_pass wordpress;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
}
}
[root@nginx ~]# mkdir /www
[root@nginx ~]# chown nginx:nginx /www/
[root@nginx ~]# cd /www/
[root@nginx www]# vim index.php
[root@nginx www]# cat index.php
<?php
phpinfo();
?>
到这里我们先别重启Nginx接下来在PHP主机操作(1.20)
[root@php ~]# mkdir /www
[root@php ~]# chown -R nginx:nginx /www/
[root@php ~]# cd /www/
[root@php www]# vim index.php
[root@php www]# cat index.php
<?php
phpinfo();
?>
创建完成后接下来我们重启nginx以及php,访问测试页
[root@nginx www]# nginx -s reload
[root@php www]# service php-fpm restart
五:wordpress网站的部署
1:在mysql中
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on wordpress.* to yankerp@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
2:在nfs中
[root@nginx ~]# tar zxf wordpress-4.9.1-zh_CN.tar.gz
[root@nginx ~]# mv wordpress/* /opt/wwwroot
[root@nginx ~]# useradd nginx
[root@nginx ~]# chown -R nginx:nginx /opt/wwwroot