LNMP平臺就是Linux. Ngnix. MySQL. PHP的組合架構,需要Lnux伺服器、MySal 數據庫、PHP解析壞境
Ngnix特長:高併發,低資源,處理靜態網路存取請求非常強
Linux(i/ˈlɪnəks/ LIN-əks)是一種自由bai和開放原始碼的類duUNIX 操作系統。該操作系統的內核由林納斯·托瓦茲在zhi1991年10月5日首次發佈,在加dao上使用者空間的應用程式之後,成爲 Linux 操作系統
靜態處理和動態處理都可以做,更適合做動態處理
將動態資源請求交給PHP當中的fpm處理動態請求
主組態檔:php.ini
Nginx將動態資源分配給FPM或Apache
1·爲了與Nginx、PHP環境保持- -致, 此處選擇採用原始碼編譯的方式安裝MySQL元件
2、MySQL部署的方法
編譯安裝MySQL
優化調整
初始化數據庫
啓動MySQL服務並設定root數據庫賬號的密碼
1、設定網頁動靜分離,解析PHP,有兩種方法可以選擇
使用PHP的FPM模組
將存取PHP頁面的Web請求轉交給Apache伺服器去處理
2、較新版本的PHP已經自帶FPM模組,對PHP解析範例進行管理、優化解析效率
FastCGI將Http Server和動態指令碼語言分離開
Nginx專門處理靜態請求,轉發動態請求
PHP-FPM專[門解析PHP動態請求
單伺服器的LNMP架構通常使用FPM方式來解析PHP
1、呼叫本機的php-fpm進程設定方法
建立FPM組態檔php-fpm.conf,修改設定選項
- PID檔案、執行使用者、服務進程數等
啓動php-fpm進程
2、設定Nginx支援PHP解析
在Nginx的組態檔的server{ }設定段中設定將PHP的網頁請求轉給FPM模組處理
3、PHP頁面存取測試
本案例在單台伺服器上部署LNMP環境
案例架構
Linux+Nginx+MySQL+PHP
[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
[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 ::