前言
本篇博客使用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
[root@server ~]# 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用戶即可
[root@server ~]# id apache uid=998(apache) gid=48(apache) groups=48(apache)
第三步:啟動php-fpm服務
監聽本機的9000端口
[root@server ~]# systemctl restart php-fpm [root@server ~]# ss -tnl | grep 9000 LISTEN 0 128 192.168.43.175:9000 *:*
第四步:下載nginx
在192.168.43.174服務器上面下載nginx
[root@proxy ~]# yum install nginx -y
第五步:配置nginx
添加如下一個location,fastcgi_pass執行剛才配置的php服務器端
[root@proxy ~]# 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配置
[root@proxy ~]# 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
[root@proxy ~]# systemctl restart nginx [root@proxy ~]# 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服務器端下載數據庫
[root@agent ~]# yum install mariadb-server -y
第九步:啟動數據庫
[root@agent ~]# systemctl restart mariadb
第十步:建立數據庫及用戶
創建一個wordpress數據庫,新建一個wordpress用戶
[root@agent ~]# 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 wordpress@'%' 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文件
[root@proxy ~]# cd /var/www/html/ [root@proxy html]# ls [root@proxy html]# rz [root@proxy html]# ls wordpress-3.3.1-zh_CN.zip [root@proxy html]# yum install unzip -y
[root@proxy html]# unzip wordpress-3.3.1-zh_CN.zip
第二步:php服務器端也要准備wordpress文件
至於為什么也要在php服務器端准備wordpress文件是因為nginx文件里面的配置,相當於動靜分離架構,動態文件即php文件會來php服務器端來找
[root@server ~]# cd /var/www/html/ [root@server html]# ls [root@server html]# rz [root@server html]# ls wordpress-3.3.1-zh_CN.zip [root@proxy html]# yum install unzip -y [root@server html]# unzip wordpress-3.3.1-zh_CN.zip [root@server html]# ls wordpress wordpress-3.3.1-zh_CN.zip
第三步:瀏覽器測試
在瀏覽器輸入nginx服務器的ip地址
點擊創建一個配置文件
點擊現在就開始
輸入之前創建的數據庫信息及用戶信息,點擊提交
提示創建失敗,只能進行手工創建(nginx服務器端及php服務器端執行下面同樣的操作)
[root@proxy html]# cd wordpress [root@proxy wordpress]# cp wp-config-sample.php wp-config.php [root@proxy 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服務搭建完成