Nginx 之快速安装

Centos安装nginx

1、下载nginx

# 下载后的文件,放到以下目录下
cd /usr/local

# 联网的情况下,通过命令下载
curl -O http://nginx.org/download/nginx-1.22.1.tar.gz

# 未联网的情况下:http://nginx.org/en/download.html

# 解压
tar -zxvf nginx-1.22.1.tar.gz

Nginx 之快速安装_第1张图片

2、安装所需环境

#安装gcc
#官网下载的 nginx 源码进行编译,编译依赖 gcc 环境
yum install gcc-c++

#安装pcre pcre-devel
#PCRE(Perl Compatible Regular Expressions) 是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 依赖 PCRE 库。
yum install -y pcre pcre-devel

#安装zlib
#zlib 适用于数据压缩的函式库,由 Jean-loup Gailly (负责 compression)和 Mark Adler (负责 decompression)开发。 nginx 依赖 zlib 库。
yum install -y zlib zlib-devel

#安装OpenSSL
#OpenSSL 是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信,避免窃听,同时确认另一端连接者的身份。这个包广泛被应用在互联网的网页服务器上。Nginx 也依赖 OpenSSL,需要在 Centos 安装此库。
yum install -y openssl openssl-devel

3、安装Nginx

#配置Nginx
cd /usr/local/nginx-1.22.1
#不需要SSL
./configure --prefix=/usr/local/nginx
#需要SSL
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

#编译、安装
make
make install

#查找安装路径
whereis nginx

在这里插入图片描述
4、配置Nginx

vi /usr/local/nginx/conf/nginx.conf

Nginx跨域配置

default.http.conf配置:

server {
        listen       80;
        server_name  xxx.cn www.xxx.cn;
        client_max_body_size 150M;
 
        location / {
            root   /home/busapp/ui/;
            #index  index.html;
            # 解决vue打包项目后刷新404的问题
            try_files $uri $uri/ /index.html =404; 
        }
 
        location /api/ {
            proxy_pass http://127.0.0.1:27071/;
            proxy_redirect              off;
            proxy_set_header            Host $host;
            proxy_set_header            X-Real-IP $remote_addr;
            proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_connect_timeout 60s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }
        location /ptapi/ {
            proxy_pass http://127.0.0.1:27071/;
            proxy_redirect              off;
            proxy_set_header            Host $host;
            proxy_set_header            X-Real-IP $remote_addr;
            proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_connect_timeout 60s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

default.https.conf配置:

# 创建证书目录
cd /usr/local/nginx/conf/
mkdir cert
server {
        listen 443 ssl;
        server_name  xxx.cn www.xxx.cn;
 
        ssl_certificate      cert/7369597_xxx.cn.pem;
        ssl_certificate_key  cert/7369597_xxx.cn.key;
 
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
 
        root   /home/busapp/ui/;
        index  index.html;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        client_max_body_size 50M;
        location /api/ {
            proxy_pass http://127.0.0.1:27071/;
            proxy_redirect              off;
            proxy_set_header            Host $host;
            proxy_set_header            X-Real-IP $remote_addr;
            proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_connect_timeout 60s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }
	      location /ptapi/ {
            proxy_pass http://127.0.0.1:27071/;
            proxy_redirect              off;
            proxy_set_header            Host $host;
            proxy_set_header            X-Real-IP $remote_addr;
            proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_connect_timeout 60s;
            proxy_read_timeout 60s;
            proxy_send_timeout 60s;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

5、开放访问端口

#--permanent永久生效,没有此参数重启后失效
firewall-cmd --zone=public --add-port=9000/tcp --permanent
#重新载入配置
firewall-cmd --reload
#查看已经开启的端口
firewall-cmd --zone=public --list-ports

Nginx 之快速安装_第2张图片
6、设置开机自启
方案一:

vi /lib/systemd/system/nginx.service

#nginx.service内添加以下内容:
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

#使配置生效
systemctl daemon-reload

#设置开机启动
systemctl enable nginx.service

方案二:
即在rc.local增加启动代码就可以了。

vim /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx

#设置执行权限
chmod 755 rc.local

Nginx 之快速安装_第3张图片

7、Nginx相关命令

cd /usr/local/nginx/sbin/

#启动
./nginx

#启动并指定配置文件
./nginx -c /usr/local/nginx/conf/nginx.conf

#重新加载配置文件(用户基本感觉不到)
./nginx -s reload

#停止nginx
./nginx -s quit

#强制停止nginx
./nginx -s stop

#查看Nginx是否还存在
which nginx

#查询nginx进程
ps -ef|grep nginx

#卸载Nginx
yum remove nginx

你可能感兴趣的:(Nginx,nginx)