您好,登錄后才能下訂單哦!
實(shí)驗(yàn)思路
第一步 安裝及運(yùn)行控制
第二步 配置文件調(diào)整
第三步 狀態(tài)統(tǒng)計(jì)及虛擬主機(jī)
第四步 LNMP構(gòu)建
第五步 LNMP平臺(tái)部署(上線)web應(yīng)用(網(wǎng)站項(xiàng)目)
2.實(shí)驗(yàn)環(huán)境:
主機(jī) OS IP地址 軟件 說(shuō)明概述
Ctos6-1 Centos6.5 192.168.200.254 ftp及yum源 提供基礎(chǔ)環(huán)境
Ctos6-2 Centos6.5 192.168.200.202 Nginx、mysql、php Nginx網(wǎng)站
3.重點(diǎn)內(nèi)容:
重點(diǎn)內(nèi)容1: 什么是nginx:
輕量級(jí)HTTP服務(wù)軟件,具有穩(wěn)定性、高效性、低消耗、高并發(fā)連接處理能力的一款專門用于處理靜態(tài)頁(yè)面的web服務(wù)軟件。
重點(diǎn)內(nèi)容2: 安裝及運(yùn)行控制:
1.下載軟件—>安裝依賴包(支持軟件)創(chuàng)建用戶和組—>安裝
2.運(yùn)行控制:
nginx –t ##檢查配置
nginx ##啟動(dòng)
killall –s 信號(hào) nginx ##信號(hào)有HUP重載配置,QUIT退出、KILL殺死
重點(diǎn)內(nèi)容3: 配置文件:/usr/local/nginx/conf/nginx.conf
1.基本格式:
vi /usr/local/nginx/conf/nginx.conf
:%g/^$/d ##刪除空行
:%g/#/d ##刪除包含#號(hào)的行即注釋
配置項(xiàng)格式:關(guān)鍵字 值;
配置包含:全局配置,I/O事件配置(events {配置項(xiàng)}),HTTP配置(http { server { location {} } })http配置可以有多個(gè)server配置,server配置可以有多個(gè)location配置。
:wq
2.重要配置項(xiàng):
Server_name 網(wǎng)站域名;
Location / {
Root html; ##網(wǎng)站的根目錄
Index index.html; ##指定默認(rèn)首頁(yè)
}
重點(diǎn)內(nèi)容4: 訪問(wèn)狀態(tài)統(tǒng)計(jì)及虛擬主機(jī):
1.訪問(wèn)狀態(tài):nginx內(nèi)置HTTP_STUB_STATUS模塊
啟用這個(gè)功能需要兩個(gè)步驟:一是編譯安裝是指定--with-http_stub_status_module,二是修改配置文件。
2.虛擬主機(jī):nginx支持虛擬主機(jī)(端口、ip、域名)
1)nginx實(shí)現(xiàn)虛擬主機(jī):一個(gè)虛擬主機(jī)一個(gè)server{不同監(jiān)聽(tīng)地址、域名、端口、日志位置、網(wǎng)頁(yè)根目錄}
2)常用虛擬主機(jī)為基于域名虛擬主機(jī),配置多個(gè)虛擬機(jī)時(shí)只需要添加多個(gè)域名解析同時(shí)設(shè)置多個(gè)server{}配置即可。
重點(diǎn)內(nèi)容5: LNMP架構(gòu)及應(yīng)用部署:nginx處理靜態(tài)頁(yè)面,php-fpm處理動(dòng)態(tài)頁(yè)面
1.安裝MySQL數(shù)據(jù)庫(kù):編譯安裝優(yōu)化調(diào)整初始化數(shù)據(jù)庫(kù)啟動(dòng)mysql服務(wù)
2.安裝php:編譯安裝(注意--php-fpm啟用php支持)安裝后調(diào)整添加zendguardloader
3.配置php-fpm(nginx支持php環(huán)境):一創(chuàng)建php-fpm監(jiān)聽(tīng)TCP/9000端口,二添加nginx轉(zhuǎn)發(fā)php請(qǐng)求到9000端口。
1)創(chuàng)建php-fpm配置文件:
vi /usr/local/php5/etc/php-fpm.conf ##fpm配置文件
[global] ##全局配置
pid = run/php-fpm.pid
[www] ##網(wǎng)站配置
listen = 127.0.0.1:9000 ##監(jiān)聽(tīng)的ip:端口
user = nginx ##用戶必須是nginx進(jìn)程的用戶
group = nginx
pm = dynamic
pm.max_children = 50 ##啟動(dòng)時(shí)開(kāi)啟的進(jìn)程數(shù)
pm.start_servers= 20 ##最少空閑進(jìn)程數(shù)
pm.min_spare_servers = 5 ##最小空閑進(jìn)程數(shù)
pm.max_spare_servers = 35 ##最多進(jìn)程數(shù)
:wq
2)配置nginx支持PHP解析:兩種方式二選一
方式一:代理的方式(轉(zhuǎn)發(fā)php請(qǐng)求到其他能解析php的主機(jī))(集群中使用)
Server {
Location ~ .php$ { proxy_pass http://php解析主機(jī)ip:端口 }
}
方式二:調(diào)用php-fpm進(jìn)程(單機(jī)使用)
Server {
Location ~ .php$ { ##匹配URL中php的請(qǐng)求
root html; ##網(wǎng)頁(yè)根目錄
fastcgi_pass 127.0.0.1:9000; ##指定fpm監(jiān)聽(tīng)地址及端口
fastcgi_index index.php; ##指定php首頁(yè)文件
include fastcgi.conf; ##引入fastcgi.conf配置
}
}
4.發(fā)布php應(yīng)用(代碼上線):下載程序代碼(網(wǎng)頁(yè)項(xiàng)目)并解壓復(fù)制到網(wǎng)頁(yè)根目錄創(chuàng)建數(shù)據(jù)庫(kù)并授權(quán)網(wǎng)頁(yè)初始化設(shè)置及訪問(wèn)。
三.項(xiàng)目實(shí)驗(yàn)步驟(操作截圖或者操作命令)
1.安裝及運(yùn)行控制:
1)安裝nginx:192.168.200.202
[root@localhost ~]# lftp 192.168.200.254 ##下載
lftp 192.168.200.254:~> cd tools/
lftp 192.168.200.254:/tools> get nginx-1.6.0.tar.gz
802956 bytes transferred
lftp 192.168.200.254:/tools> bye
[root@localhost ~]# yum -y install pcre-devel zlib-devel &>/dev/null ##安裝依賴包
[root@localhost ~]# useradd -M -s /sbin/nologin nginx ##創(chuàng)建用戶
[root@localhost ~]# tar zxf nginx-1.6.0.tar.gz -C /usr/src/ &>/dev/null ##解壓
[root@localhost ~]# cd /usr/src/nginx-1.6.0/
[root@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module &&make &&make install ##安裝
[root@localhost nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ###創(chuàng)建軟鏈接,優(yōu)化命令搜索路徑
[root@localhost nginx-1.6.0]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 8月 31 17:02 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.6.0]# cd
2)運(yùn)行控制:192.168.200.202
[root@localhost ~]# 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@localhost ~]#
[root@localhost ~]# nginx ##啟動(dòng)服務(wù)
[root@localhost ~]# netstat -utpln |grep 80 ##驗(yàn)證
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3250/nginx
[root@localhost ~]#
3)使用nginx服務(wù)腳本:
[root@localhost ~]# killall -s H
UP nginx ##重新加載配置文件,相當(dāng)于reload
[root@localhost ~]# killall -s QUIT nginx ##退出,正常結(jié)束
[root@localhost ~]# killall -s KILL nginx ##強(qiáng)制殺死
[root@localhost ~]# vi /etc/init.d/nginx
#!/bin/bash
NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in ##$1表示第一位置變量,$0表示腳本本身
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
:wq
[root@localhost ~]# chkconfig --add nginx ##添加系統(tǒng)服務(wù)
[root@localhost ~]# chmod +x /etc/init.d/nginx ##授權(quán)
[root@localhost ~]# /etc/init.d/nginx restart ##重啟驗(yàn)證
nginx is stopping!!
nginx is starting!!
[root@localhost ~]# netstat -utpln |grep nginx ##查看nginx監(jiān)聽(tīng)端口
tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 3277/nginx
[root@localhost ~]#
4)訪問(wèn)驗(yàn)證:
真機(jī)訪問(wèn)驗(yàn)證:192.168.200.202
linux客戶端訪問(wèn)驗(yàn)證:192.168.200.254
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks --dump http://192.168.200.202 ##訪問(wèn)
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
配置文件調(diào)整(配置文件決定服務(wù)的安全性,功能、穩(wěn)定性等配置文件調(diào)整十分重要)
基本優(yōu)化:
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
:%g/^$/d
:%g/#/d
:set nu
調(diào)整并發(fā)鏈接數(shù):
并發(fā)連接數(shù)=worker_process(工作進(jìn)程數(shù))X worker_connections=2x4096=8192
1 worker_processes 2;
2 events {
3 worker_connections 4096;
4 }
10 charset utf-8; ##支持中文字符集,utf-8萬(wàn)國(guó)碼
16 index index.html index.htm index.php; ##支持php首頁(yè)
:wq
[root@localhost ~]# nginx –t ##測(cè)試
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@localhost ~]#
[root@localhost ~]# /etc/init.d/nginx restart
nginx is stopping!!
nginx is starting!!
[root@localhost ~]#
[root@localhost ~]# ps aux |grep nginx |grep worker |wc -l ##驗(yàn)證工作進(jìn)程數(shù)
2)虛擬主機(jī)配置:
A.設(shè)置dns解析:192.168.200.254
[root@ns ~]# cd /var/named/chroot/var/named
[root@ns named]# vi ../../etc/named.conf ##最后添加
zone "linuxren.cn." IN {
type master;
file "linuxren.cn.zone";
};
:wq
[root@ns named]# cp linuxfan.cn.zone linuxren.cn.zone
[root@ns named]# sed -i 's/fan/ren/g' linuxren.cn.zone
[root@ns named]# /etc/init.d/named restart
停止 named:. [確定]
啟動(dòng) named: [確定]
[root@localhost ~]#
[root@ns named]# vi /etc/resolv.conf
nameserver 192.168.200.254
nameserver 10.0.0.2
:wq
[root@ns named]# nslookup www.linuxfan.cn
Server: 192.168.200.254
Address: 192.168.200.254#53
Name: www.linuxfan.cn
Address: 192.168.200.202
[root@ns named]# nslookup www.linuxren.cn
Server: 192.168.200.254
Address: 192.168.200.254#53
Name: www.linuxren.cn
Address: 192.168.200.202
[root@ns named]#
B.修改配置文件及準(zhǔn)備測(cè)試目錄:192.168.200.202
[root@localhost ~]# mkdir /var/www/ ##創(chuàng)建測(cè)試目錄
[root@localhost ~]# echo www.linuxfan.cn >/usr/local/nginx/html/index.html ##創(chuàng)建測(cè)試首頁(yè)
[root@localhost ~]# echo www.linuxren.cn >/var/www/index.html
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf ##添加如下內(nèi)容,注意行號(hào)
13 server_name www.linuxfan.cn; ##修改網(wǎng)站域名
27 server {
28 listen 80;
29 server_name www.linuxren.cn; ##修改網(wǎng)站域名
30 location / {
31 root /var/www/; ##指定網(wǎng)頁(yè)根目錄
32 index index.html index.htm index.php;
33 }
34 error_page 500 502 503 504 /50x.html;
35 location = /50x.html {
36 root html;
37 }
38 }
:wq
[root@localhost ~]# nginx –t ##檢查語(yǔ)法
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@localhost ~]#
[root@localhost ~]# /etc/init.d/nginx restart ##重啟服務(wù)
nginx is stopping!!
nginx is starting!!
[root@localhost ~]#
C:訪問(wèn)測(cè)試:192.168.200.254
[root@localhost ~]# vi /etc/resolv.conf
; generated by /sbin/dhclient-script
nameserver 192.168.200.254 ##修改原有的nameserver
:wq
[root@localhost ~]# elinks --dump http://www.linuxfan.cn ##訪問(wèn)測(cè)試
www.linuxfan.cn
[root@localhost ~]# elinks --dump http://www.linuxren.cn
www.linuxren.cn
[root@localhost ~]#
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
:wq
[root@localhost ~]# chmod +x /etc/init.d/lnmp
[root@localhost ~]# chkconfig --add lnmp
[root@localhost ~]# /etc/init.d/lnmp status
nginx is running!
php-fpm is runing!
[root@localhost ~]#
[root@localhost ~]# vi /usr/local/nginx/html/index.php
<?php
$link=mysql_connect('localhost','root','123123');
if ($link) echo '<h2>恭喜,數(shù)據(jù)庫(kù)連接成功了,你牛!';
mysql_close();
?>
:wq
[root@localhost ~]#
訪問(wèn)驗(yàn)證:192.168.200.11(真機(jī))注意將DNS設(shè)置192.168.200.254
5.LNMP平臺(tái)部署(上線)web應(yīng)用(網(wǎng)站項(xiàng)目):192.168.200.202
[root@localhost ~]# yum -y install unzip ##安裝解壓軟件
[root@localhost ~]# lftp ftp.linuxfan.cn
lftp ftp.linuxfan.cn:~> cd tools/
lftp ftp.linuxfan.cn:/tools> get SKYUC.v3.4.2.SOURCE.zip ##下載網(wǎng)站項(xiàng)目
8249271 bytes transferred
lftp ftp.linuxfan.cn:/tools> bye
[root@localhost ~]#
[root@localhost ~]# unzip SKYUC.v3.4.2.SOURCE.zip ##解壓
[root@localhost ~]# cd SKYUC.v3.4.2.SOURCE/
[root@localhost ~]# cp -rf wwwroot /usr/local/nginx/html/skyuc ##復(fù)制項(xiàng)目,也可以用ln命令來(lái)鏈接
[root@localhost ~]# cd /usr/local/nginx/html/skyuc ##進(jìn)入目錄
[root@localhost ~]# chown -R nginx:nginx admincp/ data/ templates/ upload/ ##授權(quán)
[root@localhost ~]# mysql -uroot -p123123 -s ##登錄mysql
create database skyucdb; ##創(chuàng)建數(shù)據(jù)庫(kù)
grant all on skyucdb.* to runskyuc@'localhost' identified by '123123'; ##授權(quán)本地訪問(wèn)
quit;
[root@localhost ~]#
瀏覽器訪問(wèn):http://192.168.200.202/skyuc
web頁(yè)面的操作比較簡(jiǎn)單大家自己完成。
[root@localhost ~]# cd /usr/local/nginx/html/skyuc ##192.168.200.202上刪除安裝文件
[root@localhost skyuc]# rm -rf install/
[root@localhost skyuc]#
結(jié)果:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。