1. 程式人生 > >Yum搭建LNMP環境(動、靜、庫分離)--技術流ken

Yum搭建LNMP環境(動、靜、庫分離)--技術流ken

 

前言

本篇部落格使用yum來搭建lnmp環境,將採用動態,靜態以及資料庫分開安裝的方式即nginx,php,mysql.會被分開安裝在不同的伺服器之上,搭建出來一套lnmp環境,並部署wordpress進行測試。

 

LNMP準備環境

centos7

firewalld關閉狀態

selinux關閉狀態

nginx伺服器IP:192.168.43.174

php、php-fpm、php-mysql伺服器IP: 192.168.43.175

MySQL伺服器IP:192.168.43.176

 

LNMP搭建

 

第一步:php、php-fpm、php-mysql伺服器搭建

下載用於和資料庫通訊的php-mysql,支援php檔案的php以及實現fastcgi的php-fpm

[[email protected] ~]# yum install php-mysql php php-fpm -y

 

第二步:配置php-fpm檔案

主要修改12行處為本機的IP地址,24行處修改為nginx端的IP地址,保證本機有apache使用者

 ; Start a new pool named 'www'.
  2 [www]
  3 
  4 ; The address on which to accept FastCGI requests.
  
5 ; Valid syntaxes are: 6 ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on 7 ; a specific port; 8 ; 'port' - to listen on a TCP socket to all addresses on a 9 ; specific port; 10 ; '/path/to/unix/socket
' - to listen on a unix socket. 11 ; Note: This value is mandatory. 12 listen = 192.168.43.175:9000 ##這裡修改為本機的IP地址 13 14 ; Set listen(2) backlog. A value of '-1' means unlimited. 15 ; Default Value: -1 16 ;listen.backlog = -1 17 18 ; List of ipv4 addresses of FastCGI clients which are allowed to connect. 19 ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original 20 ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address 21 ; must be separated by a comma. If this value is left blank, conne; must be separated by a comma. If this value is left blank, connections will be 22 ; accepted from any ip address. 23 ; Default Value: any 24 listen.allowed_clients = 192.168.43.174 ##修改為nginx端的IP地址 25 26 ; Set permissions for unix socket, if one is used. In Linux, read/write 27 ; permissions must be set in order to allow connections from a web server. Many 28 ; BSD-derived systems allow connections regardless of permissions. 29 ; Default Values: user and group are set as the running user 30 ; mode is set to 0666 31 ;listen.owner = nobody 32 ;listen.group = nobody 33 ;listen.mode = 0666 34 35 ; Unix user/group of processes 36 ; Note: The user is mandatory. If the group is not set, the default user's group 37 ; will be used. 38 ; RPM: apache Choosed to be able to access some dir as httpd 39 user = apache #確保有apache使用者 40 ; RPM: Keep a group allowed to write in log dir. 41 group = apache #確保有apache組 ...

檢查是否有apache使用者,如果沒有需要下載httpd服務,或者自建apache使用者即可

[[email protected] ~]# id apache
uid=998(apache) gid=48(apache) groups=48(apache)

 

第三步:啟動php-fpm服務

監聽本機的9000埠

[[email protected] ~]# systemctl restart php-fpm
[[email protected] ~]# ss -tnl | grep 9000
LISTEN     0      128    192.168.43.175:9000                     *:*     

 

第四步:下載nginx

在192.168.43.174伺服器上面下載nginx

[[email protected] ~]# yum install nginx -y

 

第五步:配置nginx

新增如下一個location,fastcgi_pass執行剛才配置的php伺服器端

[[email protected] ~]# vim /etc/nginx/nginx.conf
...
 server {
        listen       80;
        server_name  _;
        root         /var/www/html;
       index index.html index.php;
        # Load configuration files for the default server block.
    #    include /etc/nginx/default.d/*.conf;

        location ~ \.php$ {
        fastcgi_pass 192.168.43.175:9000;
        include fastcgi.conf;
        
        }
....

 

第六步:檢查nginx配置

[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

 

第七步:啟動nginx

檢查無誤後啟動nginx

[[email protected] ~]# systemctl restart nginx 
[[email protected] ~]# ss -tnl | lsof -i :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   9433  root    6u  IPv4 188674      0t0  TCP *:http (LISTEN)
nginx   9434 nginx    6u  IPv4 188674      0t0  TCP *:http (LISTEN)

 

第八步:下載mysql

在mysql伺服器端下載資料庫

[[email protected] ~]# yum install mariadb-server -y

 

第九步:啟動資料庫

[[email protected] ~]# systemctl restart mariadb

 

第十步:建立資料庫及使用者

建立一個wordpress資料庫,新建一個wordpress使用者

[[email protected] ~]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 116
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> create database wordpress;
Query OK, 1 row affected (0.06 sec)

MySQL [(none)]> grant all on wordpress.* to [email protected]'%' identified by '123';
Query OK, 0 rows affected, 1 warning (0.07 sec)

MySQL [(none)]> flush privileges;
Query OK, 0 rows affected (0.02 sec)

 

經過以上十步lnmp環境就已經搭建完成

 

LNMP環境部署wordpress進行測試

第一步:nginx伺服器端準備wordpress檔案

[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# rz
[[email protected] html]# ls
wordpress-3.3.1-zh_CN.zip
[[email protected] html]# yum install unzip -y
[[email protected] html]# unzip wordpress-3.3.1-zh_CN.zip

 

第二步:php伺服器端也要準備wordpress檔案

至於為什麼也要在php伺服器端準備wordpress檔案是因為nginx檔案裡面的配置,相當於動靜分離架構,動態檔案即php檔案會來php伺服器端來找

[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# rz
[[email protected] html]# ls
wordpress-3.3.1-zh_CN.zip
[[email protected] html]# yum install unzip -y
[[email protected] html]# unzip wordpress-3.3.1-zh_CN.zip
[[email protected] html]# ls
wordpress  wordpress-3.3.1-zh_CN.zip

 

 

第三步:瀏覽器測試

在瀏覽器輸入nginx伺服器的ip地址

點選建立一個配置檔案

點選現在就開始

輸入之前建立的資料庫資訊及使用者資訊,點選提交

提示建立失敗,只能進行手工建立(nginx伺服器端及php伺服器端執行下面同樣的操作)

[[email protected] html]# cd wordpress
[[email protected] wordpress]# cp wp-config-sample.php wp-config.php 
[[email protected] wordpress]# vim wp-config.php 
// ** MySQL 設定 - 具體資訊來自您正在使用的主機 ** //
/** WordPress 資料庫的名稱 */
define('DB_NAME', 'wordpress');

/** MySQL 資料庫使用者名稱 */
define('DB_USER', 'wordpress');

/** MySQL 資料庫密碼 */
define('DB_PASSWORD', '123');

/** MySQL 主機 */
define('DB_HOST', '192.168.43.176');

/** 建立資料表時預設的文字編碼 */
define('DB_CHARSET', 'utf8');

再次開啟瀏覽器進行測試

根據提示輸入以上資訊,點選下面的安裝

 

輸入賬號和密碼進行登入即可

 

至此LNMP服務搭建完成