[toc]
1.Nginx 安装 一、安装编译工具及库文件 1 2 3 4 5 yum -y install make zlib zlib-devel gcc gcc-c++ libtool openssl openssl-devel
二、首先要安装 PCRE PCRE 作用是让 Nginx 支持 Rewrite 功能。
1 yum -y install pcre pcre-devel
三、安装 Nginx 1、下载 Nginx,下载地址:https://nginx.org/en/download.html
2、解压安装包
1 tar -xzvf nginx-1.22.0.tar.gz
3、进入安装包目录
4、编译
1 ./configure --prefix=/usr/local/nginx
5、安装
6、查看nginx版本
1 /usr/local/nginx/sbin/nginx -v
四、启动Nginx 进入安装好的目录/usr/local/nginx/sbin
1 2 3 4 ./nginx 启动 ./nginx -s stop 快速关闭 ./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求 ./nginx -s reload 重新加载配置
五、关于防火墙和SELINUX 关闭防火墙
1 systemctl stop firewalld.service
禁止防火墙开机自启
1 systemctl stop firewalld.service
1 2 3 4 5 6 7 sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config [root@localhost ~] [root@localhost ~] Permissive
六、添加systemctl管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cat << EOF > /usr/lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking ExecStartPre=/bin/mkdir /tmp/nginx 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 EOF
七、创建环境变量 1 2 3 4 5 vim /etc/profile.d/nginx.sh NGINX_HOME=/usr/local/nginx PATH=$NGINX_HOME /sbin:$PATH export NGINX_HOME PATH
source /etc/profile.d/nginx.sh
启动nginx
1 2 mkdir -p /tmp/nginxnginx
八、修改ip为静态地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" DEFROUTE="yes" IPV4_FAILURE_FATAL="no" IPV6INIT="yes" IPV6_AUTOCONF="yes" IPV6_DEFROUTE="yes" IPV6_FAILURE_FATAL="no" IPV6_ADDR_GEN_MODE="stable-privacy" NAME="ens33" UUID="ff458f3b-f132-400d-ae06-86277ce43612" DEVICE="ens33" ONBOOT="yes" IPADDR=192.168.110.131 NETMASK=255.255.255.0 GATEWAY=192.168.110.1 BROADCAST=192.168.110.255 BOOTPROTO="static"
2.Nginx的conf学习 nginx.conf的配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 more /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
虚拟主机 虚拟主机应用场景:一个WEB服务器同时发布多个WEB站点
用ip基于端口访问 1 2 3 4 5 6 7 8 9 10 [root@localhost /] [root@localhost www] void www [root@localhost www] this is void web site... [root@localhost www] this is www web site... [root@localhost www]
修改nginx.conf配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [root@localhost www] server { listen 80; server_name localhost; location / { root /www/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 81; server_name localhost; location / { root /www/void; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
用域名基于端口访问 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [root@localhost www] server { listen 80; server_name www.julintongxue.top; location / { root /www/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 81; server_name void.julintongxue.top; location / { root /www/void; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }