编译安装Nginx服务及升级
1.关闭防火墙和安全措施,将软件包下载到opt目录中
[root@localhost opt]# systemctl stop firewalld.service
[root@localhost opt]# setenforce 0
2. 安装Nginx所需要的依赖包
[root@localhost opt]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3.创建Nginx程序用户
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
4.解包并且安装所需要的模块
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@localhost opt]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
5.编译安装
[root@localhost nginx-1.12.2]# make -j4 && make install
现有nginx版本号
6.升级nginx版本,解包并编辑模块
[root@localhost opt]# tar -zxvf nginx-1.22.0.tar.gz
[root@localhost opt]# tar -zxvf nginx-1.22.0.tar.gz
[root@localhost nginx-1.22.0]# yum -y install pcre-devel zlib zlib-devel openssl openssl-devel
[root@localhost nginx-1.22.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module
[root@localhost nginx-1.22.0]# make
7.备份文件
[root@localhost nginx-1.22.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
[root@localhost nginx-1.22.0]# cp objs/nginx /usr/local/nginx/sbin/nginx
8.升级完成,查看版本
9.添加 Nginx 系统服务
[root@localhost system]# cd /etc/init.d/
[root@localhost init.d]# vim nginx #创建一个新的文件
#!/bin/bash
#chkconfig: 35 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
10.配置nginx主配置文件
[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf
#user nginx; #指定运行用户
worker_processes 4; #指定服务器内核书
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll; #指定使用epoll模型
worker_connections 1024;
}
11.查看nginx服务运行状态
Nginx的六个实验
1.访问状态统计配置
1.1备份配置文件
[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]# cp nginx.conf nginx.conf.bak
1.2修改nginx配置文件
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kcg.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm index.php;
}
location /status{
stub_status on;
access_log off;
}
1.3 重启服务并查看nginx是否成功启动
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# 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
1.4 开启网页查看实验是否成功
2.基于授权的访问控制
2.1生成用户密码认证文件
[root@localhost conf]# yum install -y httpd-tools
[root@localhost conf]# htpasswd -c /usr/local/nginx/passwd.db tsuki
[root@localhost conf]# chown nginx /usr/local/nginx/passwd.db
[root@localhost conf]# chmod 400 /usr/local/nginx/passwd.db
2.2修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kcg.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm index.php;
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
location /status{
stub_status on;
access_log off;
}
2.3 重启服务并查看nginx是否成功启动
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# 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
2.4开启网页查看实验是否成功
3.基于客户端的访问控制
3.1修改配置文件
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm index.php;
deny 192.168.116.10;
allow all;
}
3.2重启服务并查看nginx是否成功启动
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# 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
3.3查看实验是否成功
使用被禁止的192.168.116.10查看网页
使用本机ip查看网页
4.基于域名的 Nginx 虚拟主机
4.1为虚拟主机提供域名解析
[root@localhost conf]# echo "192.168.116.13 www.kgc.com www.benet.com" >> /etc/hosts
4.2为虚拟主机准备网页文档
[root@localhost nginx]# mkdir -p /usr/local/nginx/html/benet
[root@localhost html]# mkdir -p /usr/local/nginx/html/kgc
[root@localhost html]# echo "<h1>www.kgc.com</h1>" > /usr/local/nginx/html/kgc/index.html
[root@localhost html]# echo "<h1>www.kgc.com</h1>" > /usr/local/nginx/html/benet/index.html
4.3修改nginx配置文件
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kcg.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /usr/local/nginx/html/kgc;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
4.4重启服务并查看nginx是否成功启动
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# 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.5使用域名查看实验是否成功
5.基于IP 的 Nginx 虚拟主机
5.1修改配置文件
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.116.13:80;
server_name www.kcg.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /usr/local/nginx/html/kgc;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
5.2重启服务并查看nginx是否成功启动
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# 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
5.3使用ip地址访问,查看实验是否成功
6.基于端口的 Nginx 虚拟主机
6.1修改配置文件
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.116.13:8080;
server_name www.kcg.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /usr/local/nginx/html/kgc;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
6.2重启服务并查看nginx是否成功启动
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# 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
6.3使用端口访问,查看实验是否成功