apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute gcc openssh-server iotop unzip zip make -y
nginx/1.16.1 云盘下载(请优先支持官方下载)
实验步骤:
[root@U8 ~]# cd /usr/local/src/
[root@U8 src]# ll
total 1024
drwxr-xr-x 3 root root 4096 Dec 30 20:33 ./
drwxr-xr-x 10 root root 4096 Dec 27 08:43 ../
-rw-r--r-- 1 root root 1032630 Dec 30 18:49 nginx-1.16.1.tar.gz
#解压下载好的nginx包
[root@U8 src]# tar xf nginx-1.16.1
[root@U8 src]# cd nginx-1.16.1/
#编译安装,并添加自定义模块
[root@U8 src]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@U8 src]# make -j 4 && make install
#创建nginx用户
[root@U8 src]# useradd -u 66 -r -s /sbin/nologin nginx
#查看编译安装后生成的目录
[root@U8 src]# tree -d /apps/
/apps/
└── nginx
├── client_body_temp
├── conf
├── fastcgi_temp
├── html
├── logs
├── proxy_temp
├── sbin
├── scgi_temp
└── uwsgi_temp
10 directories
#修改目录属主属组为nginx
[root@U8 src]# chown -R nginx:nginx /apps/nginx/
查看nginx版本:
[root@U8 src]# /apps/nginx/sbin/nginx -V
创建nginx⾃启动脚本:
[root@U8 src]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
ExecStartPre=/bin/rm -f /apps/nginx/logs/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@U8 src]# vim /apps/nginx/conf/nginx.conf
#取消该行的 # 注释
pid logs/nginx.pid;
启动nginx⾃启动脚本:
[root@U8 src]# systemctl daemon-reload
[root@U8 src]# systemctl restart nginx
设置开机自启
[root@U8 src]# systemctl enable --now nginx
[root@U8 src]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-12-30 21:54:46 CST; 1min 10s ago
Main PID: 9911 (nginx)
Tasks: 2 (limit: 1685)
CGroup: /system.slice/nginx.service
├─9911 nginx: master process /apps/nginx/sbin/nginx
└─9913 nginx: worker process
………………………………………………………………………
浏览器访问nginx默认界面
添加PATH变量
[root@U8: ~]# vim /etc/profile.d/nginx.sh
PATH="/apps/nginx/sbin:$PATH"
[root@U8: ~]# source /etc/profile
生成的nginx主配置文件路径:
/apps/nginx/conf/nginx.conf
[root@U8 src]# grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$"
worker_processes 1; #启动⼯作进程数数量
pid logs/nginx.pid; #events设置块
events {
worker_connections 1024; #单个nginx⼯作进程可以接受的最⼤并发
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65; #⻓连接超时时间,单位是秒
server {
listen 80; #配置server监听的端⼝
server_name localhost;
location / {
root html; #默认⻚⾯的⽬录名称
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html; #错误⻚⾯的⽂件名称
location = /50x.html {
root html; #默认⻚⾯所在的⽬录
}
}
}
不要轻易改动nginx的主配置文件,建立子配置文件进行配置
[root@U8 src]# mkdir /apps/nginx/conf/conf.d
[root@U8 src]# touch /apps/nginx/conf/conf.d/pc.conf
[root@U8 src]# vim /apps/nginx/conf/conf.d/pc.conf
server {
listen 80;
server_name dushansao.com; #请勿使用特殊字符
location / {
root /data/nginx/html/pc;
}
}
[root@U8 src]# mkdir -pv /data/nginx/html/pc
mkdir: created directory '/data/nginx'
mkdir: created directory '/data/nginx/html'
mkdir: created directory '/data/nginx/html/pc'
[root@U8 src]# tree -d /data/
/data/
├── lost+found
├── nginx
│ └── html
│ └── pc
└── scripts
5 directories
[root@U8 src]# echo "dushansao PC web" > /data/nginx/html/pc/index.html
[root@U8 ~]# vim /apps/nginx/conf/nginx.conf
#添加此行至文件尾部(写入http中会影响后续试验)
include /apps/nginx/conf/conf.d/*.conf;
图示
#重启nginx
[root@U8 src]# systemctl reload nginx
#本机IP地址
[root@U8 pc]# ip a
...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP g0
link/ether 00:0c:29:8b:4c:65 brd ff:ff:ff:ff:ff:ff
inet 192.168.124.30/24 brd 192.168.124.255 scope global eth0
...
修改windows hosts文件
hosts文件存放目录: C:\Windows\System32\drivers\etc
修改hosts文件添加此行虚拟机IP :
192.168.124.30 dushansao.com
#该文件默认无法修改,请设置该文件的写权限及取消仅读
浏览器访问
实验失败及报错的原因:
1、配置文件格式或内容编辑错误
2、配置文件或目录对nginx权限不足及属主属组非nginx
3、nginx服务未能启动或启动失败
4、请务必重启虚拟机(重启服务有时并不能生效)!