之前的文章是在同一個物理機里面搭建LNMP+wordpress,現(xiàn)在來試一下nginx一臺,php一臺,mysql一臺分布式的搭建lnmp.
前期準(zhǔn)備:
關(guān)閉防火墻和selinux:
iptables -F #關(guān)閉防火墻
setenforce 0 #關(guān)閉selinux
getenforce #查看selinux狀態(tài)
vim /etc/selinux/config #在配置文件中修改selinux
[root@localhost html]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled #改成disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
:wq
需要準(zhǔn)備的服務(wù)器:
OS:Centos7.4x86_64
Nginx-1.12.2 IP地址:192.168.1.108
PHP-5.6 IP地址:192.168.1.109
MySQL5.6 IP地址:192.168.1.107
1.安裝mysql并配置(107主機上操作):
查詢并卸載原有包:
先查詢系統(tǒng)中是否已經(jīng)安裝了mysql,如果有先刪除了再安裝新包,在centos7中默認(rèn)自帶了mariadb需要卸載
[root@mysql ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.centos.x86_64
[root@mysql ~]# rpm -e mariadb-libs-5.5.56-2.el7.centos.x86_64 --nodeps
mysql的安裝部分已經(jīng)在之前的mysql的主從復(fù)制的文章里安裝過了,這里給出地址,不再贅述.
http://www.itdecent.cn/p/742c85366a5c
創(chuàng)建wordpress用戶密碼:
安裝完成mysql之后,可以先創(chuàng)建wordpress的數(shù)據(jù)庫,用戶密碼權(quán)限等.
#登錄到mysql
[root@localhost mysql]# /usr/local/mysql/bin/mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#創(chuàng)建數(shù)據(jù)庫
mysql> create database wordpress;
Query OK, 1 row affected (0.22 sec)
#創(chuàng)建用戶密碼,及權(quán)限
mysql> grant all on wordpress.* to wuser@'%' identified by '123456';
Query OK, 0 rows affected (0.19 sec)
#刷新權(quán)限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.09 sec)
mysql> \q
Bye
#重啟mysql
[root@localhost mysql]# /etc/init.d/mysql.server restart
Shutting down MySQL..... SUCCESS!
Starting MySQL...................................... SUCCESS!
mysql的安裝配置就完成了.
2.安裝nginx(108主機操作):
安裝環(huán)境包:
yum install gcc gcc-c++ openssl-devel zlib-devel zlib pcre-devel -y
創(chuàng)建指定用戶:
這個nginx用戶要和php服務(wù)器上創(chuàng)建的nginx兩者id一直,這里先創(chuàng)建出來
[root@nginx ~]# groupadd -g 1001 nginx
[root@nginx ~]#useradd -u 900 nginx -g nginx -s /sbin/nologin
[root@nginx ~]# tail -1 /etc/passwd
nginx:x:900:1001::/home/nginx:/sbin/nologin
下載并解壓:
將源碼包下載至/usr/local/src目錄中
[root@nginx ~]# cd /usr/local/src
[root@nginx src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[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 --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
創(chuàng)建軟連接并啟動測試:
[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@localhost src]# ps aux | grep nginx
root 12469 0.0 0.1 46096 2100 ? Ss 16:09 0:00 nginx: master process nginx
nginx 12933 0.0 0.1 48548 2524 ? S 16:49 0:00 nginx: worker process
root 13323 0.0 0.0 112704 976 pts/1 R+ 17:28 0:00 grep --color=auto nginx
這樣nginx就編譯安裝完成了,可以在客戶端訪問192.168.1.108來查看是否出現(xiàn)nginx的歡迎頁面.
3.安裝php(109主機上操作):
安裝編譯環(huán)境包:
yum -y install gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel
安裝依賴的libmcrypt:
[root@php src]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@php src]# tar zxf libmcrypt-2.5.7.tar.gz
[root@php src]# cd libmcrypt-2.5.7/
[root@php libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
...過程省略...
下載并解壓:
將php下載至/usr/local/src目錄中
[root@php ~]# cd /usr/local/src
[root@php src]# wget http://cn2.php.net/distributions/php-5.6.27.tar.gz
[root@php src]# tar zxf php-5.6.27.tar.gz
[root@php src]# cd php-5.6.27/
編譯并安裝:
[root@php php-5.6.27]#./configure --prefix=/usr/local/php5.6 --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
...過程省略...
創(chuàng)建用戶id:
這個nginx的id號要和nginx主機上的保持一致!!!
[root@php php-5.6.27]# groupadd -g 1001 nginx
[root@php php-5.6.27]# useradd -u 900 nginx -g nginx -s /sbin/nologin
[root@php php-5.6.27]# tail -1 /etc/passwd
nginx:x:900:1001::/home/nginx:/sbin/nologin
復(fù)制配置文件及腳本:
[root@php php-5.6.27]# cp php.ini-production /etc/php.ini
[root@php php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-5.6.27]# chmod +x /etc/init.d/php-fpm
[root@php php-5.6.27]# chkconfig --add php-fpm #添加服務(wù)到啟動列表
[root@php php-5.6.27]# chkconfig php-fpm on #開啟啟動
修改主配置文件:
#復(fù)制配置文件
[root@php php-5.6.27]# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
修改配置文件參數(shù),對應(yīng)參數(shù)如下:
[root@php php-5.6.27]# vim /usr/local/php5.6/etc/php-fpm.conf
pid = run/php-fpm.pid
user = nginx
group = nginx
listen = 192.168.1.109:9000 //改成PHP主機的IP地址
pm.max_children = 50 #php進程相關(guān)的配置
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
#修改這些參數(shù)至對應(yīng)的值,其他不用動,
啟動并檢查服務(wù):
配置完成保存退出之后就可以啟動php服務(wù),并檢查是否啟動成功.
[root@localhost php-5.6.27]# service php-fpm start
Starting php-fpm done
[root@localhost php-5.6.27]# netstat -anput | grep php
tcp 0 0 192.168.1.109:9000 0.0.0.0:* LISTEN 120498/php-fpm: mas
到這里3臺服務(wù)器上都安裝上了對應(yīng)的服務(wù),下面來進行LNMP搭建的配置.
4.LNMP配置:
配置Nginx支持PHP環(huán)境(在108主機上操作):
修改/usr/local/nginx/conf/nginx.conf中的配置文件
[root@nginx ~]#vim /usr/local/nginx/conf/nginx.conf
...省略...
location / {
root /www; #更改網(wǎng)頁目錄
index index.php index.html index.htm; #添加index.php
}
location ~ \.php$ {
root /www; 更改目錄
fastcgi_pass 192.168.1.109:9000; #注意:在這里添加PHP主機IP地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
...省略...
#將上面的容內(nèi)修改好,省略部分暫時不用修改,然后保存退出即可
:wq
在 /usr/local/nginx/conf/fastcgi_params添加配置
[root@localhost src]# vim /usr/local/nginx/conf/fastcgi_params
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_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$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 REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
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;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
"/usr/local/nginx/conf/fastcgi_params" 26L, 1077C
:wq
創(chuàng)建目錄及測試頁:(108和109上操作)
在nginx主機(108)和php主機(109),兩臺主機上都要進行如下操作.
[root@nginx ~]# mkdir /www
[root@nginx ~]# chown nginx:nginx /www/
[root@nginx ~]# cd /www/
[root@nginx www]# vim index.php
<?php
phpinfo();
?>
:wq
重啟nginx以及php并訪問測試頁:(108和109上操作)
完成上述步驟之后就可以重啟nginx主機(108)和php主機(109),然后在客戶端訪問192.168.1.108就能訪問到php測試頁了
[root@nginx www]# nginx -s reload
[root@php www]# service php-fpm restart
看到測試頁說明已經(jīng)搭建完成了分布式的LNMP服務(wù),下面開始搭建wordpress論壇.
5.搭建wordpress論壇:
下載并解壓wordpress:(108和109上操作)
wordpress要同時下載到nginx(108)和php(109)并解壓到/www/目錄中.
#nginx主機上
[root@nginx ~]# tar zxf wordpress-4.9.1-zh_CN.tar.gz
[root@nginx ~]# mv wordpress/* /www/ #保證剪切到/www目錄下沒有其他文件
#php主機上
[root@php ~]# tar zxf wordpress-4.9.1-zh_CN.tar.gz
[root@php ~]# mv wordpress/* /www #保證剪切到/www目錄下沒有其他文件
修改wordpress配置文件(108和109上操作):
可以通過wp-config-sample.php 模版文件了創(chuàng)建wp-config.php 文件,這個文件名一定要正確,否則會報錯,找不到配置文件的.
[root@nginx src]# cp /www/wp-config-sample.php /www/wp-config.php
[root@nginx src]# vim /www/wp-config.php
...省略...
// ** MySQL 設(shè)置 - 具體信息來自您正在使用的主機 ** //
/** WordPress數(shù)據(jù)庫的名稱 */
define('DB_NAME', 'wordpress'); #mysql創(chuàng)建的數(shù)據(jù)庫名稱
/** MySQL數(shù)據(jù)庫用戶名 */
define('DB_USER', 'wuser'); #mysql創(chuàng)建的用戶名稱
/** MySQL數(shù)據(jù)庫密碼 */
define('DB_PASSWORD', '123456'); #mysql創(chuàng)建的用戶密碼
/** MySQL主機 */
define('DB_HOST', '192.168.1.107'); #mysql主機IP
/** 創(chuàng)建數(shù)據(jù)表時默認(rèn)的文字編碼 */
define('DB_CHARSET', 'utf8');
/** 數(shù)據(jù)庫整理類型。如不確定請勿更改 */
...省略...
#將mysql(107主機)創(chuàng)建的數(shù)據(jù)庫與用戶密碼信息填好,就可以保證退出,省略部分不用修改
:wq
因為兩個主機上的配置是一樣,直接scp傳過去即可.
[root@nginx src]# scp /www/wp-config.php root@192.168.1.109:/www/
root@192.168.1.109's password:
wp-config.php 100% 2910 1.9MB/s 00:00
訪問測試:http://192.168.1.108/
在客戶端上訪問http://192.168.1.108/即可出現(xiàn)wordpress論壇安裝提示了.

用戶名和密碼要按mysql創(chuàng)建的用戶來進行填寫.mysql地址要寫安裝mysql主機的IP

正確填寫之后就可以登錄了.

登錄之后就看見這個界面.說明配置成功了.