原創 伺服器羣集——原始碼編譯安裝LNMP(搭建基於LNMP架構的Discuz論壇)

2020-08-12 11:46:40

一、LNMP架構解讀

LNMP平臺就是Linux. Ngnix. MySQL. PHP的組合架構,需要Lnux伺服器、MySal 數據庫、PHP解析壞境
Ngnix特長:高併發,低資源,處理靜態網路存取請求非常強
Apache:靜態處理和動態處理都可以做,更適合做動態處理
Nginx將動態資源請求交給PHP當中的fpm處理動態請求
PHP主組態檔:php.ini
Nginx將動態資源分配給FPM或Apache

MySQL安裝設定

1、爲了與Nginx、PHP環境保持- -致, 此處選擇採用原始碼編譯的方式安裝MySQL元件
2、MySQL部署的方法

  • 編譯安裝MySQL
  • 優化調整
  • 初始化數據庫
  • 啓動MySQL服務並設定root數據庫賬號的密碼

PHP解析環境的安裝

1、設定網頁動靜分離,解析PHP,有兩種方法可以選擇

  • 使用PHP的FPM模組
  • 將存取PHP頁面的Web請求轉交給Apache伺服器去處理
    2、較新版本的PHP已經自帶FPM模組,對PHP解析範例進行管理、優化解析效率
  • FastCGI將Http Server和動態指令碼語言分離開
  • Nginx專門處理靜態請求,轉發動態請求
  • PHP-FPM專[門解析PHP動態請求
  • 單伺服器的LNMP架構通常使用FPM方式來解析PHP

設定Nginx支援PHP環境

1、呼叫本機的php-fpm進程設定方法

  • 建立FPM組態檔php-fpm.conf,修改設定選項
    - PID檔案、執行使用者、服務進程數等
  • 啓動php-fpm進程
    2、設定Nginx支援PHP解析
  • 在Nginx的組態檔的server{ }設定段中設定將PHP的網頁請求轉給FPM模組處理
    3、PHP頁面存取測試

二、LNMP應用部署

本案例在單台伺服器上部署LNMP環境
案例架構
Linux+Nginx+MySQL+PHP

1、安裝nginx

[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir LNMP
[root@localhost opt]# cd LNMP/
[root@localhost LNMP]# rz -E     //使用xshell直接壓縮包到虛擬機器
rz waiting to receive.
[root@localhost LNMP]# ls
Discuz_X3.4_SC_UTF8.zip  mysql-boost-5.7.20.tar.gz  ncurses-5.6.tar.gz  nginx-1.12.2.tar.gz  php-7.1.10.tar.bz2
1、安裝環境依賴包
[root@localhost LNMP]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@localhost LNMP]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# yum -y install gcc \
gcc-c++ \
zlib-devel \
pcre pcre-devel   //安裝編譯器和其他工具
2、建立執行使用者、組
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx   //建立nginx管理使用者,不建立宿主目錄,禁止登陸到shell環境
[root@localhost nginx-1.12.2]# id nginx
uid=1001(nginx) gid=1001(nginx) 組=1001(nginx)
3、編譯安裝nginx
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module     //安裝路徑,使用者,組,統計模組功能
[root@localhost nginx-1.12.2]# make && make install
4、路徑優化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  //建立軟鏈接,便於管理員直接執行nginx命令來呼叫nginx主程式
[root@localhost nginx-1.12.2]# ls /usr/local/sbin/
nginx
5、檢測語法
[root@localhost 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
6、啓動、停止nginx服務
[root@localhost nginx-1.12.2]# nginx 
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      70833/nginx:
用宿主機win10去存取192.168.200.80,成功存取nginx網站
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      70833/nginx: master 
[root@localhost nginx-1.12.2]# killall -s HUP nginx     //過載nginx命令
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      70833/nginx: master 
[root@localhost nginx-1.12.2]# killall -s QUIT nginx    //關閉nginx命令
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      70961/nginx: master 
[root@localhost nginx-1.12.2]# kill -9 70961    //kill殺死進程但是服務殺不死
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      70962/nginx: worker 
[root@localhost nginx-1.12.2]# pkill nginx        //pkill直接殺死進程樹
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
7、新增nginx系統服務
[root@localhost nginx-1.12.2]# vim /lib/systemd/system/nginx.service    //新增nginx服務給systemctl管理
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking              //預設進程模式
PIDFile=/usr/local/nginx/logs/nginx.pid     //進程檔案
ExecStart=/usr/local/nginx/sbin/nginx         //安裝目錄
ExecReload=/usr/bin/kill -s HUP $MAINPID   //過載目錄
ExecStop=/usr/bin/kill -s QUIT $MAINPID     //停止
PrivateTmp=ture
[Install]
WantedBy=multi-user.target      //多使用者模式
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service   //設定許可權
[root@localhost nginx-1.12.2]# systemctl start nginx
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.      //出現警告
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@localhost nginx-1.12.2]# systemctl daemon-reload      //重新載入某個服務的組態檔,如果新安裝了一個服務,歸屬於 systemctl 管理,要是新服務的服務程式組態檔生效,需重新載入。
[root@localhost nginx-1.12.2]# systemctl start nginx     //啓動服務成功
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      71619/nginx: master

2、安裝MYSQL

[root@localhost nginx-1.12.2]# cd /opt/LNMP/
[root@localhost LNMP]# ls
Discuz_X3.4_SC_UTF8.zip    ncurses-5.6.tar.gz   php-7.1.10.tar.bz2 mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz
[root@localhost LNMP]# tar zxvf mysql-boost-5.7.20.tar.gz -C /opt/
[root@localhost LNMP]# cd /opt/
[root@localhost opt]# useradd -s /sbin/nologin mysql   //建立執行使用者
[root@localhost opt]# yum -y install ncurses ncurses-devel bison cmake   //bison語法識別
[root@localhost opt]# cd /opt/mysql-5.7.20/
[root@localhost mysql-5.7.20]# 
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \        //儲存引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \        //數據儲存位置
-DWITH_BOOST=boost \                                        //c++-DWITH_SYSTEMD=1                                             //開啓第二進程
[root@localhost mysql-5.7.20]# make
[root@localhost mysql-5.7.20]# make install
[root@localhost mysql-5.7.20]# cd /usr/local/
[root@localhost local]# ll
drwxr-xr-x. 11 root root 151 8月  11 09:57 nginx
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql/       //更改屬主屬組爲mysql
[root@localhost local]# ll
drwxr-xr-x. 11 mysql mysql 197 8月  11 12:18 mysql
[root@localhost local]# cd /etc/
[root@localhost etc]# vim my.cnf
[client]
port=3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysql]
port=3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
character_set_server=utf8
pid-file=/usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
server-id=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

設定環境變數
[root@localhost etc]# vim /etc/profile
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH   //宣告爲全域性變數
[root@localhost etc]# source /etc/profile   //載入環境變數
初始化數據庫
[root@localhost etc]# cd /usr/local/mysql/
[root@localhost mysql]# ls
bin      COPYING-test  include  man         README       share          usr
COPYING  docs          lib      mysql-test  README-test  support-files
[root@localhost mysql]# ls bin/
mysql_install_db 
mysqld 
[root@localhost mysql]# bin/mysqld --initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data         //初始化數據庫
2020-08-11T11:41:37.791663Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-11T11:41:38.155226Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-11T11:41:38.210661Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-11T11:41:38.269282Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9dec1b5f-dbc7-11ea-ab28-000c29cd9a36.
2020-08-11T11:41:38.269993Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-11T11:41:38.271838Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@localhost mysql]# cp /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# netstat -antp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      95058/mysqld 
[root@localhost mysql]# systemctl enable mysqld     //設定開機自啓
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost mysql]# mysqladmin -u root -p password "abc123"    //設定mysql密碼
Enter password:                           //空密碼所以按空格
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@localhost mysql]# mysql -u root -p
Enter password:                           //輸入剛剛設定的密碼abc123
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit
Bye
[root@localhost mysql]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      71619/nginx: master 
[root@localhost mysql]# netstat -antp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      95058/mysqld 

3、安裝php

[root@localhost mysql]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel    
[root@localhost mysql]# cd /opt/LNMP/
[root@localhost LNMP]# tar jxvf php-7.1.10.tar.bz2 -C /opt/
[root@localhost LNMP]# cd /opt/php-7.1.10/
[root@localhost php-7.1.10]# ./configure --prefix=/usr/local/php --with-mysql-sock=/usr/local/mysql/mysql.sock --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-fpm --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo --enable-tokenizer --enable-zip
[root@localhost php-7.1.10]# make && make install
[root@localhost php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini //複製模板建立主組態檔
[root@localhost php-7.1.10]# vim /usr/local/php/lib/php.ini    //設定主組態檔
1170 mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m     //檢視所有模組
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib

[Zend Modules]
[root@localhost php-7.1.10]# cd /usr/local/php/etc/
 [root@localhost etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d     //fpm模板
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@localhost etc]# vim php-fpm.conf
pid = run/php-fpm.pid
[root@localhost etc]# cd /usr/local/php/etc/
[root@localhost etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# ls
www.conf  www.conf.default
[root@localhost php-fpm.d]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini 
[root@localhost php-fpm.d]# netstat -antp | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      105806/php-fpm: mas 
[root@localhost php-fpm.d]# ln -s /usr/local/php/bin/* /usr/local/bin/     //設定軟鏈接使得PHP的命令會被系統識別
[root@localhost php-fpm.d]# ps aux | grep -c "php-fpm"
4

4、讓nginx支援PHP

[root@localhost php-fpm.d]# vim /usr/local/nginx/conf/nginx.conf
 location ~ \.php$ {
            root            html;
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
            include         fastcgi_params;
        }
[root@localhost php-fpm.d]# systemctl stop nginx
[root@localhost php-fpm.d]# systemctl start nginx
[root@localhost php-fpm.d]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      106079/nginx: maste
[root@localhost php-fpm.d]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# mv index.html index.html.bak
[root@localhost html]# ls
50x.html  index.html.bak
[root@localhost html]# vim index.php
<?php
phpinfo()
?>

5、建立數據庫

[root@localhost html]# mysql -u root -p
Enter password: abc123
mysql> CREATE DATABASE bbs;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye

6、更改測試頁內容

[root@localhost html]# vim index.php 
<?php
$link=mysqli_connect('192.168.200.80','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
[root@localhost html]# systemctl restart nginx
用win10瀏覽器登錄192.168.200.80/index.php

在这里插入图片描述

7、安裝論壇


[root@localhost html]# cd /opt/LNMP
[root@localhost LNMP]# unzip Discuz_X3.4_SC_UTF8.zip
[root@localhost LNMP]# mv dir_SC_UTF8/ /opt/
[root@localhost LNMP]# cd /opt/dir_SC_UTF8/
[root@localhost dir_SC_UTF8]# ls
readme  upload  utility
[root@localhost dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs
[root@localhost nginx]# cd /usr/local/nginx/html/bbs/
[root@localhost bbs]# ls
admin.php  config           favicon.ico  index.php   misc.php    search.php  uc_client
api        connect.php      forum.php    install     plugin.php  source      uc_server
api.php    crossdomain.xml  group.php    m           portal.php  static
archiver   data             home.php     member.php  robots.txt  template

[root@localhost bbs]# chown -R root:nginx ./config/
[root@localhost bbs]# chown -R root:nginx ./data/
[root@localhost bbs]# chown -R root:nginx ./uc_client/
[root@localhost bbs]# chown -R root:nginx ./uc_server/
[root@localhost bbs]# chmod -R 777 ./config/
[root@localhost bbs]# chmod -R 777 ./data/
[root@localhost bbs]# chmod -R 777 ./uc_client/
[root@localhost bbs]# chmod -R 777 ./uc_server/

開啓win10 瀏覽器輸入如下地址安裝論壇
http://192.168.200.80/bbs/install/index.php
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述