一、编译安装Nginx
1、安装支持软件
[root@localhost ~]# yum -y install pcre-devel zlib-devel
2、创建运行用户、组
[root@localhost ~]# useradd -M -S /sbin/nologin nginx
3、编译安装Nginx
[root@localhost ~]# tar zxf nginx-1.12.0.tar.gz
[root@localhost ~]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install
4、安装支持软件
[root@localhost ~]# yum -y install gcc gcc-c++ pcre-devel zlib-devel
5、创建运行用户、组
[root@localhost ~]# useradd -M -S /sbin/nologin nginx
6、编译安装Nginx
[root@localhost nginx-1.12.0]#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.0]# Is -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 5月 16 16:50 /usr/local/sbin/nginx ->
/usr/local/nginx/sbin/nginx
二、Nginx运行控制
1、检查配置文件
[ [root@localhost ~]# nginx -t
2、启动、重载配置、停止Nginx
[root@localhost ~]# nginx
[root@localhost ~]# netstat -anpt | grep ngin)
tcp 0 0 0.0.0.0:80.0.0.0.0:* LISTEN 7180/nginx:master
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks http://localhost
[root@localhost ~]# killall -s HUP nginx
[root@localhost ~]# killall -s QUIT nginx
3、Nginx添加为系统服务
[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PlDFile=/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=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
三、配置文件nginx.conf文件
1、全局配置
#user nobody;
worker processes 1;
#error_ log logs/error.log;
#pid logs/nginx.pid;
2、I/O事件配置
events {
use epoll;
worker connections 4096;
}
3、HTTP配置
http {
......
access_log logs/accss.log main;
sendfile on;
......
keepalive_timeout 65;
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
location / {
root html;
index index.html index.php; }
error_ page 500 502 503 504 /50x.html;
location = /50x.html {
root html; }
}
四、Nginx的访问状态统计2-1
1、启用HTTP_ STUB_ STATUS状态统计模块
1)配置编译参数时添加–with-http_ stub_ status_ module
2)nginx -V查看已安装的Nginx是否包含HTTP_ STUB_ STATUS模块
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group=nginx --with-http_ stub status module
2、修改nginx.conf配置文件
查看当前的状态统计信息
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
location / {
root html;
index index.html index.php;}
location /status {
stub_status on;
access_log off;}
}
}
服务基础配置脚本:
nginx的压缩包自行下载(将脚本中"tar zxvf nginx-1.12.0.tar.gz"修改一下即可)
#!/bin/bash
#编译安装Nginx
#解压
tar zxvf nginx-1.12.0.tar.gz
#安装支持软件包
yum install -y gcc gcc-c++ pcre-devel zlib-devel
#创建运行用户、组
useradd -M -s /sbin/nologin nginx
#编译和安装
cd nginx-1.12.0
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
#设置软连接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
#检查配置文件
nginx -t
#将Nginx添加为系统服务(可以使用systemctl启动)
echo "[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PlDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target" >>/lib/systemd/system/nginx.service
#权限设置
chmod 754 /lib/systemd/system/nginx.service
#配置nginx.conf配置文件(I/O时间配置,HTTP配置)
sed -i '13i use epoll;' /usr/local/nginx/conf/nginx.conf
sed -i '38s/localhost/www.kgc.com/' /usr/local/nginx/conf/nginx.conf
sed -i '40s/#charset koi8-r/charset utf-8/' /usr/local/nginx/conf/nginx.conf
#配置状态统计信息
sed -i '47i location /status { \
stub_status on; \
access_log off;}' /usr/local/nginx/conf/nginx.conf
#启动服务
systemctl start nginx
#查看服务端口是否开启
netstat -ntap | grep 80
脚本结果,查看HTTP服务页面
查看状态统计信息