基本配置:
操作系统为: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 //如果域名访问的话,需要在客户机加上映射