基本配置:
操作系統爲:CentOS 7.2
[root@localhost qwe]# cat /etc/redhat-release CentOS Linux release
7.2.1511 (Core)
確定主機名:LNMP
vim /etc/hostname
確定IP地址:192.168.0.195
ip a
/etc/sysconfig/network-scripts/ifcfg-eth0
關閉防火牆:service iptables stop
一、配置Nginx網站服務
1.運行Nginx需要pcre、zilib等軟件包支持,先安裝開發包
[root@LNMP qwe]# yum -y install pcre-devel zlib-devel
2.創建運行的用戶和組
[root@LNMP qwe]# useradd -M -s /sbin/nologin nginx //不指定宿主目錄且禁止登錄shell
[root@LNMP qwe]# id nginx
uid=1000(nginx) gid=1000(nginx) 組=1000(nginx)
3.編譯安裝Nginx
[root@LNMP ~]# tar zxvf nginx-1.10.1.tar.gz
[root@LNMP nginx-1.10.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@LNMP nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
./configure: error: C compiler cc is not found //這個錯誤是你沒有安裝c語言編輯器
[root@LNMP nginx-1.10.1]# yum -y install gcc* gcc-*
[root@LNMP nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
### http_stub_status_module //添加狀態統計模塊
[root@LNMP nginx-1.10.1]# make && make install
[root@LNMP nginx-1.10.1]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@LNMP nginx-1.10.1]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 Jul 19 08:07 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
執行nginx –t可以檢查配置文件nginx.conf中的錯誤
[root@LNMP ~]# 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
4.直接執行nginx運行服務
[root@LNMP qwe]# ngxin
[root@LNMP qwe]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4790/nginx: master
Killall –s HUP nginx //HUP表示重載配置
Killall –s QUIT nginx //QUIT表示退出進程
5.編寫Nginx服務控制腳本
[root@LNMP qwe]# touch /etc/init.d/nginx
[root@LNMP qwe]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
PROC="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROC
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -anpt | grep nginx
else
echo "Nginx is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
esac
exit 0
[root@LNMP qwe]# chmod +x /etc/init.d/nginx
[root@LNMP qwe]# chkconfig --add nginx
[root@LNMP qwe]# chkconfig --list nginx
nginx 0:關 1:關 2:關 3:開 4:關 5:開 6:關
6.Nginx的訪問狀態統計
[root@LNMP qwe]# vim /usr/local/nginx/conf/nginx.conf
#user nobody; //默認用戶nobody
worker_processes 1; //工作進程數量
#error_log logs/error.log; //錯誤日誌文件位置
#pid logs/nginx.pid; //PID文件位置
events {
use epoll; //啓用epoll模型
worker_connections 1024; //每進程處理連接數量
}
http {
······
server {
listen 80; //監聽端口默認80
server_name www.benet.com; //網站名稱
charset utf-8; //網站默認字符集
location / { //根目錄配置
root html; //網站根目錄
index index.html index.htm; //默認首頁
}
location /status { //訪問位置/status
stub_status on; //開啓status統計功能
access_log off; //關閉此位置的日誌記錄
}
error_page 500 502 503 504 /50x.html; //內部錯誤的反饋頁面
location = /50x.html { //錯誤頁面配置
root html;
}
}
}
[root@Nginx ~]# /etc/init.d/nginx reload
使用客戶機訪問www.benet.com/status //如果域名訪問的話,需要在客戶機加上映射