三期總目錄鏈接
目錄
一、Nginx服務
1、安裝及運行控制
2、組態檔nginx.conf
2.1全域配置 解釋:
2.2 I/O事件配置 解釋:
2.3 HTTP配置 解釋:
3、訪問狀態統計及虛擬主機應用
二、LNMP架構及應用部署
1、搭建LNMP網站平臺
1.1、安裝MYSQL資料庫(三章———Mysql資料庫系統3.1)
1.2、安裝PHP決議環境
1.3配置nginx支持PHP環境
2、在LNMP平臺中部署web應用
2.1部署程式代碼
復習題
一、Nginx服務
Nginx (engine x) 是一款輕量級的HTTP服務器軟體,優點:穩定性好、豐富的功能集、簡單的組態檔和低系統資源的消耗,以及占有記憶體少,并發能力強(單臺物理服務器可支持30000~50000個并發請求)正因如此,大量提供社交網路、新聞資訊、電子商務的企業紛紛選擇 Nginx來提供服務 例:百度、京東、新浪、網易、騰訊、淘寶等
1、安裝及運行控制
[root@C7--01 ~]# yum -y install pcre-devel zlib-devel #安裝支持軟體 (提供相應的庫 和頭檔案)
.........
....
[root@C7--01 ~]# useradd -M -s /sbin/nologin nginx #創建運行用戶,組
[root@C7--01 ~]# tar xf nginx-1.12.0.tar.gz -C /usr/src/ #解壓nginx
[root@C7--01 ~]# cd /usr/src/nginx-1.12.0/ #進入目錄
[root@C7--01 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
............
.....
安裝目錄設定為/usr/loca/nginx ,運行用戶,組設定為nginx,啟用 --with-http_stub_status_module 支持狀態統計,便于查看服務器的連接資訊
問題:在進行決議安裝時出現錯誤
[root@C7--01 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
checking for OS
+ Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
解決方法:安裝 gcc openssl-devel
[root@C7--01 nginx-1.12.0]# yum -y install gcc pcre-devel zlib-devel openssl-devel
[root@C7--01 nginx-1.12.0]# make && make install #編譯安裝
[root@C7--01 nginx-1.12.0]# ls /usr/local/nginx/ #驗證安裝
conf html logs sbin
優化執行(方便nginx 運行)
[root@C7--01 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #軟連接
[root@C7--01 nginx-1.12.0]# ls -l /usr/local/sbin/
總用量 0
lrwxrwxrwx 1 root root 27 8月 28 00:13 nginx -> /usr/local/nginx/sbin/nginx
查看nginx幫助命令
[root@C7--01 ~]# nginx -h
nginx version: nginx/1.12.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
1.1啟動、停止
[root@C7--01 ~]# nginx #啟動nginx服務
[root@C7--01 ~]# netstat -utpln | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 44886/nginx: master
訪問測驗

在字符界面可以使用文本瀏覽器查看
-dump:將HTML檔案以純文本的方式列印到標準輸出設備
[root@C7--01 ~]# yum -y install elinks #安裝文本瀏覽器
[root@C7--01 ~]# elinks --dump http://192.168.1.1
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to [1]nginx.org.
Commercial support is available at [2]nginx.com.
Thank you for using nginx.
References
Visible links
1. http://nginx.org/
2. http://nginx.com/
主程式Nginx支持標準的行程信號,通過kill或killall命令發送HUP信號表示多載配置,QUIT信號表示退出行程,KILL信號表示殺死行程,最小化安裝的centos 系統默認沒有安裝killall命令,需要先安裝
[root@C7--01 ~]# yum -y install psmisc #安裝killall命令
...........
.....
[root@C7--01 ~]# killall -s HUP nginx # 多載配置 相當于 killall -1 nginx
[root@C7--01 ~]# netstat -utpln | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 46095/nginx: master
[root@C7--01 ~]# killall -s QUIT nginx # 停止服務 相當于 killall -3 nginx (退出行程)
[root@C7--01 ~]# netstat -utpln | grep nginx
撰寫nginx服務腳本使用 systemctl 工具進行管理
[root@C7--01 ~]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$NP;
if [ $? -eq 0 ]
then
echo "nginx is starting!! "
fi
;;
stop)
kill -s QUIT $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx is stopping!! "
fi
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx config file is reload! "
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@C7--01 ~]# chmod +x /etc/init.d/nginx #賦予執行權限
[root@C7--01 ~]# chkconfig --add nginx #添加為系統服務
[root@C7--01 ~]# systemctl status nginx #查看nginx 服務狀態
● nginx.service - SYSV: Nginx Server Control Script
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
2、組態檔nginx.conf
Nginx服務器主組態檔:/usr/local/nginx/conf/nginx.conf;有三大塊內容:全域配置、I/O事件配置、HIIP配置;配置陳述句的格式為“關鍵字 值;”(末尾以分號表示結束),“#”開始為注釋
2.1全域配置 解釋:
[root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody; #運行用戶
worker_processes 1; #作業行程數量;可參考CPU核心總數指定作業行程數,網站訪問量越大,行程數設定越多
#error_log logs/error.log; #錯誤日志檔案的位置
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #PID檔案位置
......
..
2.2 I/O事件配置 解釋:
events { #使用events界定標記指定nginx行程的I/O回應模型,每個行程連接數等
use epoll; #使用epoll模型,提高性能
worker_connections 4096; #每個行程處理4096個連接(默認為1024:每個行程的連接數量一般在50000以下根據實際情況設定)
}
注意:如作業行程數是 8;每個行程處理4096 個連接,則Nginx 提供服務的連接數是(4096X8);具體看服務器硬體和網路帶寬等物理條件性能等
2.3 HTTP配置 解釋:
[root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main; #訪問日志位置
sendfile on; #開啟高效傳輸檔案模式
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #連接保持超時
#gzip on;
server { #web服務的監聽配置
listen 80; #監聽地址及埠
server_name localhost; #網站名稱
#charset koi8-r; #網頁的默認字符集
#access_log logs/host.access.log main;
location / { #根目錄配置
root html; #網站根目錄的位置,相對于安裝目錄
index index.html index.htm; #默認首頁(搜引頁)
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #內部錯誤的反饋頁面
location = /50x.html { #錯誤頁面配置
root html;
}
.........
....
}
| nginx中的location語法:location [ = / ~ / ~* / ^~] /uri/ { … } | |
| = | 開頭表示精確匹配 |
| ~ | 開頭表示區分大小寫的正則匹配 |
| ~* | 開頭表示不區分大小寫的正則匹配 |
| !~和!~* | 分別為區分大小寫不匹配及不區分大小寫不匹配 的正則 |
| / | 通用匹配,任何請求都會匹配到 |
| ^~ | 開頭表示uri以某個常規字串開頭,理解為匹配 url路徑即可,nginx不對url做編碼,因此請求為/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格) |
| 匹配順序僅供參考:首先匹配 =,其次匹配^~, 其次是按檔案中順序的正則匹配,最后是交給 /通用 匹配,當有匹配成功時候,停止匹配,按當前匹配規則處理請求 | |
3、訪問狀態統計及虛擬主機應用
編輯組態檔:把原組態檔修改名字為nginx.conf.bak
[root@C7--01 ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
新建nginx.conf組態檔進行編輯
[root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.aaa.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
location /status { #訪問位置:/status
stub_status on; #打開狀態統計功能
access_log off; #關閉此位置的日志記錄
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@C7--01 ~]# 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@C7--01 ~]# systemctl start nginx #啟動nginx
[root@C7--01 ~]# systemctl status nginx #查看狀態
● nginx.service - SYSV: Nginx Server Control Script
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: active (exited) since 六 2021-08-28 00:36:18 CST; 19h ago
Docs: man:systemd-sysv-generator(8)
Process: 46093 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
訪問測驗:IE瀏覽器 http://192.168.1.1/status
| 當前的狀態統計資訊 | |
| Active conmections | 表示當前的活動連接數 1 |
| server accepts handled requests | 表示已經處理的連接資訊 三個數依次是:已處理的連接數(1)、成功的TCP握手次數(1)、已處理的請求數(1) |
| Reading: 0 Writing: 1 Waiting: 0 | 讀取狀態 寫入狀態 等待狀態 |
[root@C7--01 ~]# elinks --dump http://192.168.1.1/status
Active connections: 1 server accepts handled requests 1 1 1 Reading: 0
Writing: 1 Waiting: 0
圖形化測驗
3.1部署NGINX虛擬主機
方法一:配置DNS服務
[root@C7--01 ~]# yum -y install bind #安裝dns服務
.......
...
[root@C7--01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 #進入網卡
......
..
DNS1=192.168.1.1 #添加dns地址
保存退出
[root@C7--01 ~]# systemctl restart network #重啟網卡
[root@C7--01 ~]# cat /etc/resolv.conf #查看dns
# Generated by NetworkManager
search 1
nameserver 192.168.1.1
修改主組態檔
[root@C7--01 ~]# vim /etc/named.conf
options {
listen-on port 53 { 192.168.1.1; }; #修改成地址為192.168.1.1
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; }; #允許全部網段訪問
.......
....
zone "." IN {
type hint;
file "named.ca";
};
zone "mac.com" IN { #添加mac.com
type master;
file "mac"; #檔案 mac
};
zone "aaa.com" IN { #添加aaa.com
type master;
file "aaa"; #檔案aaa
};
保存退出
編輯區域組態檔
[root@C7--01 ~]# cd /var/named
[root@C7--01 named]# cp named.localhost aaa #復制named.localhost,名稱修改為aaa
[root@C7--01 named]# cp named.localhost mac #復制named.localhost,名稱修改為mac
[root@C7--01 named]# vim mac #修改mac區域組態檔
$TTL 1D
@ IN SOA mac.com. admin.mac.com. (
2021082101 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS www.mac.com.
MX 10 mail.mac.com.
www IN A 192.168.1.1
保存退出
[root@C7--01 named]# vim aaa #修改aaa區域檔案
$TTL 1D
@ IN SOA aaa.com. admin.aaa.com. (
2021080902 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS www.aaa.com.
MX 10 mail.aaa.com.
www IN A 192.168.1.1
更改區域檔案和主組態檔的權限
[root@C7--01 named]# chown named:named aaa #修改屬主:屬組
[root@C7--01 named]# chown named:named mac #修改屬主:屬組
[root@C7--01 named]# chown named:named /etc/named.conf #修改屬主:屬組
[root@C7--02 ~]# systemctl start named #啟動dns服務
測驗:決議dns兩個域名成功都是一個ip地址
C:\Users\wrzs0> nslookup
默認服務器: UnKnown
Address: 192.168.1.1
>
> www.aaa.com
服務器: UnKnown
Address: 192.168.1.1
名稱: www.aaa.com
Address: 192.168.1.1
> www.mac.com
服務器: UnKnown
Address: 192.168.1.1
名稱: www.mac.com
Address: 192.168.1.1
方法二:進入hosts檔案添加(hosts檔案是負責ip地址與域名快速決議的檔案)
[root@C7--01 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.1 www.aaa.com
192.168.1.1 www.mac.com
準備網站目錄和測驗文件
[root@C7--01 ~]# cd /usr/local/nginx/html #進入目錄
[root@C7--01 html]# ls
50x.html index.html
[root@C7--01 html]# mkdir aaa #新建html根目錄aaa
[root@C7--01 html]# mkdir mac #新建html根目錄mac
[root@C7--01 html]# ls
50x.html aaa index.html mac
編輯html首頁
[root@C7--01 html]# vim aaa/index.html
aaa <h1>今天天氣真好<h1/>
[root@C7--01 html]# vim mac/index.html
mac <h3>歡迎來到王者峽谷<h3/>
調整nginx.conf組態檔(建議洗掉訪問狀態統計配置)
[root@C7--01 ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.aaa.com; #修改網站名稱
charset utf-8;
location / {
root /usr/local/nginx/html/aaa; #修改網站根目錄
index index.html index.php;
}
}
server {
listen 80;
server_name www.mac.com; #修改網站名稱
charset utf-8;
location / {
root /usr/local/nginx/html/mac; #修改網站根目錄
index index.html index.php;
}
}
}
[root@C7--01 ~]# 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@C7--01 ~]# elinks --dump http://www.aaa.com #字符界面訪問
aaa
今天天氣真好
[root@C7--01 ~]# elinks --dump http://www.mac.com
mac
歡迎來到王者峽谷
圖形化訪問


二、LNMP架構及應用部署
LNMP平臺的構成:Linux服務器、MySQL資料庫、PHP決議環境、Nginx (和LAMP一樣需要Linux服務器、MySQL資料庫、PHP決議環境)
LNMP和LAMP的區別:在Nginx與PHP的協作配置上
1、搭建LNMP網站平臺
1.1、安裝MYSQL資料庫(三章———Mysql資料庫系統3.1)
安裝步驟按照:三章———Mysql資料庫系統3.1 安裝
1.2、安裝PHP決議環境
編譯安裝PHP
[root@C7--02 ~]# yum -y install libxml2-devel gd zlib-devel libjpeg-devel libpng-devel
已加載插件:fastestmirror
aaa | 3.6 kB 00:00:00
......
...
[root@C7--02 ~]# tar xf php-5.5.38.tar.gz -C /usr/src
[root@C7--02 ~]# cd /usr/src/php-5.5.38/
[root@C7--02 php-5.5.38]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib
......
...
[root@C7--02 php-5.5.38]# make && make install
.........
.....
[root@C7--02 php-5.5.38]# ls /usr/local/php5/
bin etc include lib php sbin var
安裝后調整
[root@C7--02 php-5.5.38]# cp php.ini-development /usr/local/php5/php.ini
[root@C7--02 ~]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[root@C7--02 ~]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
安裝ZendGuardLoader
[root@C7--02 ~]# tar xf zend-loader-php5.5-linux-x86_64_update1.tar.gz #解壓
[root@C7--02 ~]# cd zend-loader-php5.5-linux-x86_64
[root@C7--02 zend-loader-php5.5-linux-x86_64]# ls #查看
opcache.so README.txt ZendGuardLoader.so
[root@C7--02 zend-loader-php5.5-linux-x86_64]# cp ZendGuardLoader.so /usr/local/php5/lib/php/
[root@C7--02 zend-loader-php5.5-linux-x86_64]# cd
[root@C7--02 ~]# vim /usr/local/php5/php.ini #進入php.ini檔案
.........
.....
;curl.cainfo =
; Local Variables:
; tab-width: 4
; End:
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so #在最下面添加
zend_loader.enable=1 #在最下面添加
1.3配置nginx支持PHP環境
啟用php-fpm行程(默認埠號為9000)
[root@C7--02 ~]# cd /usr/local/php5/etc/
[root@C7--02 etc]# useradd -M -s /sbin/nologin php #創建用戶php
[root@C7--02 etc]# vim php-fpm.conf #創建新組態檔
[global]
pid = run/php-fpm.pid #確認pid檔案位置
[www]
listen = 127.0.0.1:9000
user = php #運行用戶
group = php #運行組
pm = dynamic
pm.max_children = 50 #最多空閑行程數
pm.start_servers = 20 #啟動時開啟的行程數
pm.min_spare_servers = 5 #最少空閑行程數
pm.max_spare_servers = 35
保存退出
[root@C7--02 etc]# /usr/local/php5/sbin/php-fpm #啟動行程
[root@C7--02 etc]# netstat -utpln | grep php #查看
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 104914/php-fpm: mas
[root@C7--02 ~]# killall -9 php-fpm #關閉行程
[root@C7--02 ~]# netstat -utpln | grep php #查看
新建LNMP啟動腳本:可以在啟動或停止 nginx 服務器時php-fpm行程也可以啟動或停止
[root@C7--02 ~]# vim /etc/init.d/lnmp
#!/bin/bash
# chkconfig: 35 95 30
# description: This script is for LNMP Management!
NGF=/usr/local/nginx/sbin/nginx
NGP=/usr/local/nginx/logs/nginx.pid
FPMF=/usr/local/php5/sbin/php-fpm
FPMP=/usr/local/php5/var/run/php-fpm.pid
case $1 in
start)
$NGF &&echo "nginx is starting! "
$FPMF && echo "php-fpm is starting! "
;;
stop)
kill -QUIT $(cat $NGP) &&echo "nginx is stoped! "
kill -QUIT $(cat $FPMP) &&echo "php-fpm is stoped! "
;;
restart)
$0 stop
$0 start
;;
reload)
kill -HUP $(cat $NGP)
kill -HUP $(cat $FPMP)
;;
status)
netstat -utpln |grep nginx &>/dev/null
if [ $? -eq 0 ]
then
echo "nginx is running! "
else
echo "nginx is not running! "
fi
netstat -upltn |grep php-fpm &>/dev/null
if [ $? -eq 0 ]
then
echo "php-fpm is runing! "
else
echo "php-fpm is not running! "
fi
;;
*)
echo "Usage $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0
保存退出
[root@C7--02 ~]# chmod +x /etc/init.d/lnmp #賦予執行權限
[root@C7--02 ~]# chkconfig --add lnmp #加入到系統服務
確認php-fpm、nginx服務已停止
[root@C7--02 ~]# netstat -anput | grep php-fpm #查看
[root@C7--02 ~]# nginx -s stop #停止nginx服務
[root@C7--02 ~]# netstat -naput | grep nginx #查看
同時啟動php-fpm、nginx服務
[root@C7--02 ~]# systemctl start lnmp #同時啟動
[root@C7--02 ~]# systemctl status lnmp #查看狀態
● lnmp.service - SYSV: This script is for LNMP Management!
Loaded: loaded (/etc/rc.d/init.d/lnmp; bad; vendor preset: disabled)
Active: active (running) since 日 2021-08-29 22:50:39 CST; 21s ago
Docs: man:systemd-sysv-generator(8)
Process: 122085 ExecStart=/etc/rc.d/init.d/lnmp start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/lnmp.service
├─122087 nginx: master process /usr/local/nginx/sbin/nginx
├─122089 nginx: worker process
├─122090 php-fpm: master process (/usr/local/php5/etc/php-fpm.conf)
├─122091 php-fpm: pool www
├─122092 php-fpm: pool www
├─122093 php-fpm: pool www
├─122094 php-fpm: pool www
├─122095 php-fpm: pool www
├─122096 php-fpm: pool www
├─122097 php-fpm: pool www
├─122098 php-fpm: pool www
├─122099 php-fpm: pool www
├─122100 php-fpm: pool www
├─122101 php-fpm: pool www
├─122102 php-fpm: pool www
├─122103 php-fpm: pool www
├─122104 php-fpm: pool www
├─122105 php-fpm: pool www
├─122106 php-fpm: pool www
├─122107 php-fpm: pool www
├─122108 php-fpm: pool www
├─122109 php-fpm: pool www
└─122110 php-fpm: pool www
8月 29 22:50:39 C7--02 systemd[1]: Starting SYSV: This script is for LNMP Management!...
8月 29 22:50:39 C7--02 lnmp[122085]: nginx is starting!
8月 29 22:50:39 C7--02 lnmp[122085]: php-fpm is starting!
8月 29 22:50:39 C7--02 systemd[1]: Started SYSV: This script is for LNMP Management!.
[root@C7--02 ~]#
如果服務啟動錯誤就殺死行程:[root@C7--02 ~]# killall -9 nginx 在重啟LNMP
配置nginx支持PHP決議
讓Nginx能夠決議PHP網頁有兩種方法:一,訪問PHP頁面的Web請求轉交給其他服務器(LAMP)去處理;二,使用PHP的FPM模塊來呼叫本機的PHP環境 無論將PHP頁面交給LAMP服務器去決議,還是呼叫本機的php-fpm行程進行決議,都需要在“server { } ”配置段中添加location設定,以便指定訪問php網頁時采取何種操作
方法一轉交給LAMP服務器: (注意:\.php 輸入錯誤會導致訪問失敗)
[root@C7--02 ~]# vim /usr/local/nginx/conf/nginx.conf
......
...
server {
listen 80;
server_name www.benet.com;
charset utf-8;
location ~ \.php$ { #訪問.php頁面的配置段
proxy_pass http://目標主機ip地址:80; #apache服務器的監聽地址
}
方法二呼叫本機的php-fpm行程決議:(本次實驗使用方法二) (注意:\.php 輸入錯誤會導致訪問失敗)
[root@C7--02 ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.mac.com;
charset utf-8;
access_log logs/mac.access.log;
location / {
root /usr/local/nginx/html/mac;
index index.html index.php;
}
location ~ \.php$ {
root /usr/local/nginx/html/mac;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
創建測驗php網頁
[root@C7--02 ~]# mkdir /usr/local/nginx/html/mac #創建目錄
[root@C7--02 ~]# vim /usr/local/nginx/html/mac/test.php #創建訪問首頁mysql資料庫用戶名root密碼123.com
<?php
$link=mysqli_connect('localhost','root','123.com');
if($link) echo "<h1>訪問資料庫成功 !!</h1>";
mysqli_close($link);
?>

2、在LNMP平臺中部署web應用
LNMP平臺與LAMP平臺是非常相似的,區別在于所用Web服務軟體的不同,而這與使用PHP開發的Web應用程式并無太大關系,因此PHP應用的部署方法也是類似的
2.1部署程式代碼
[root@C7--02 ~]# yum -y install unzip
[root@C7--02 ~]# unzip Discuz_X3.3_SC_UTF8.zip #解壓discuz!社區論壇
[root@C7--02 ~]# mv upload/ /usr/local/nginx/html/mac/sqlt #把upload剪切到LNMP服務器的網站根目錄下
[root@C7--02 ~]# chown -R php:php /usr/local/nginx/html/mac/sqlt #設定屬主:屬組,讓nginx、php-fpm程式寫入權限
[root@C7--02 ~]# mysql -uroot -p123.com #進入mysql資料庫
.......
...
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.
mysql> create database sqlt; #創建庫sqlt
Query OK, 1 row affected (0.00 sec)
mysql> grant all on sqlt.* to aaa@localhost identified by '123.com'; #創建用戶aaa密碼為123.com 可以對sqlt庫擁有所有權限
Query OK, 0 rows affected (0.01 sec)
mysql> quit #退出
Bye
輸入: http://www.mac.com/sqlt/install/index.php



稍等片刻就可以訪問 www.mac.com/sqlt/forum.php

管理后臺:http://www.mac.com/sqlt/admin.php

輸入之前給的管理員賬號和密碼就可以管理社區論壇了

復習題
1、簡述LNMP平臺的構成組件,以及與LAMP平臺的區別
LNMP平臺的構成:Linux服務器、MySQL資料庫、PHP決議環境、Nginx
區別:在于所用Web服務軟體的不同
2、在編譯安裝Nginx時通過指定什么選項添加提供訪問統計的stub_status模塊?
指定:http_stub_status_module提供訪問統計的stub_status模塊
3、在Linux系統中執行killall -s HUP nginx與killall -s QUIT nginx命令的作用分別是什么?
-s HUP 相當于 -1:重新加載配置
-s QUIT 相當于 -3:停止服務????
4、在Nginx的組態檔中,哪幾個配置引數決定了正常服務的連接數?
events {
use epoll;
worker_connections 1024;
}
5、在Nginx組態檔的server { }配置段中,root陳述句的作用是什么?
作用:網站根目錄的位置,相對于安裝目錄
6、使用Nginx的狀態統計功能除了啟用內建模塊外,還需要在組態檔中添加哪些內容?
需要添加:
location /status { #訪問位置為/status
stub status on; #打開狀態統計功能
access log off; #關閉此位置的日志記錄
}
7、簡述Nginx配置虛擬主機的方法與流程
(1)準備網站目錄及測驗檔案 (2)調整nginx.conf組態檔 (3)測驗虛擬Web主機
8、mysqladmin -u root password 'pwd123'命令的作用什么?
給mysqi資料庫的root用戶設定密碼
9、Nginx對PHP的支持可以通過哪兩種方式實作?
1)把訪問 PHP 頁面的 Web 請求轉交給其他服務器(LAMP)處理
2)使用PHP的FPM模塊來呼叫本機的 PHP 環境
10、寫出php-fpm程式的啟動方法與默認監聽埠
啟動方法:/usr/local/sbin/php-fpm
默認監聽埠:9000
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/296538.html
標籤:其他
