centos 8.5安装nginx + 开机自动启动

下载nginx,下载地址:http://nginx.org/en/download.html

当前最新稳定版 nginx-1.20.2.tar.gz

下载完成后通过 xftp工具将nginx-1.20.2.tar.gz上传到centos服务器的某个目录下面。

 

【nginx常用命令】

cd /usr/local/nginx/sbin/  #进到nginx目录
./nginx 
./nginx -s stop  #强制停止
./nginx -s quit  #平滑停止
./nginx -s reload  #重载配置文年
nginx -t #检查配置文件是否正确
nginx -t -c /特定目录/nginx.conf #检查特定目录的配置文件是否正确
ginx -v #查看版本信息

  

【查看nginx进程信息】

ps aux | grep nginx
pa -ef | grep nginx

 

 

安装依赖

 

nginx依赖包:

gcc、libtool
pcre、pcre-devel
zlib、zlib-devel
openssl、openssl-devel

安装nginx前必须先安装这些依赖。

 

【安装gcc】

gcc是linux下的编译器,它可以编译 C、C++、Ada、Object C、Java等语言。

yum -y install make gcc gcc-c++ libtool

查看 gcc版本: gcc -v 或 gcc --version

 

【安装pcre】

pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库。

yum install -y pcre pcre-devel

查看pcre版本号:pcre-config --version

 

【安装zlib】

zlib库提供了很多种压缩和解压缩方式,nginx使用zlib对http包的内容进行gzip。

yum install -y zlib zlib-devel

 

【安装openssl】

openssl用来对http通讯进行加密。

yum install -y openssl openssl-devel

 

安装nginx

下载nginx

wget http://nginx.org/download/nginx-1.20.2.tar.gz

如果提示 “ -bash: wget: 未找到命令 ”,这是因为wget命令未安装,需要先安装wget 。

yum -y install wget

然后再地用wget下载nginx 。

 

解压到 /usr/local/nginx  目录下。

mkdir -p /usr/local/nginx
tar -zxvf nginx-1.20.2.tar.gz -C /usr/local/nginx

 

安装nginx

cd /usr/local/nginx/nginx-1.20.2
./configure  #如果将nginx安装到指定目录,加个参数,./configure --prefix=/usr/local/你指定的目录
make && make install

注意:安装完成后,会将nginx的配置文件、sbin目录等放到  【 /usr/local/nginx 】目录中,而【 /usr/local/nginx/nginx-1.20.2 】 目录只是源码而已。

  

 【启动nginx】

cd /usr/local/nginx/sbin
./nginx

查看nginx进程信息:

ps -ef | grep nginx

 

查看防火墙是否有开放80端口及http:

firewall-cmd --list-all

 

 如果没有开放,必须先开放80端口及http之后,外网才能访问到nginx。

firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

再次查看开放的端口:

firewall-cmd --list-all

 

开机自动启动nginx

在系统服务目录里创建nginx.service文件,

vi /lib/systemd/system/nginx.service

nginx.service文件内容如下:

[Unit]
#描述服务
Description=nginx
#描述服务类别
After=network.target
 
#服务运行参数的设置,注意【Service】的启动、重启、停止命令都要用绝对路径
[Service]
#后台运行的形式
Type=forking
#服务具体运行的命令
ExecStart=/usr/local/nginx/sbin/nginx
#重启命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#停止命令
ExecStop=/usr/local/nginx/sbin/nginx -s quit
#表示给服务分配独立的临时空间
PrivateTmp=true
 
#运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target

设置开机启动 :

systemctl enable nginx.service
systemctl start nginx.service

 

nginx其他命令:

systemctl start nginx.service (启动nginx服务)
systemctl stop nginx.service (停止nginx服务)
systemctl enable nginx.service (设置开机自启动)
systemctl disable nginx.service (停止开机自启动)
systemctl status nginx.service (查看服务当前状态)
systemctl restart nginx.service (重新启动服务)
systemctl list-units --type=service (查看所有已启动的服务)

 

验证nginx是否安装成功:

nginx配置文件说明

【nginx.conf 配置文件说明】

 

#user  nobody;
worker_processes  1;  #工作进程:数目。根据硬件调整,通常等于cpu数量最佳

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;  #nginx进程pid存放路径


events {
    worker_connections  1024; #工作进程的最大连接数量
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

#web服务器配置
    server {
        listen       80; #配置监听端口号
        server_name  localhost; #配置访问域名,域名可以有多个,用空格隔开

        #charset koi8-r; #字符集设置

        #access_log  logs/host.access.log  main;

        location / {
            root   html; #根目录,这里是相对路径,也可以用绝对路径 E:/wwwroot/html;
            try_files $uri $uri/ index  index.html index.htm;   #try_files $uri $uri/ 适配vue, angular, react的历史路由模式
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       
    }

#反向代理服务器配置
    server {
        listen       80
        server_name  api.loveu.com;  #域名
        location / {
            proxy_pass http://127.0.0.1:8001;
            proxy_set_header    Host                $http_host;
            proxy_set_header    X-Real-IP           $remote_addr;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    X-NginX-Proxy       true;
        }
       location /order { #根据路径转发到不同域名
            proxy_pass http://127.0.0.1:8002; 
            proxy_set_header    Host                $http_host;
            proxy_set_header    X-Real-IP           $remote_addr;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    X-NginX-Proxy       true;
        }
       location /pay {  #根据路径转发到不同域名
            proxy_pass http://127.0.0.1:8003
            proxy_set_header    Host                $http_host;
            proxy_set_header    X-Real-IP           $remote_addr;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    X-NginX-Proxy       true;
        }
    }

#反向代理 + 负载均衡配置

   #负载均衡节点配置
    upstream mall-loveu {
        server 127.0.0.1:8031 weight=1;
        server 127.0.0.1:8032 weight=1;
        server 127.0.0.1:8033 weight=1;
    }

    #负载均衡服务器入口
    server {
        listen       8030;
        server_name  127.0.0.1;
        location / {
            proxy_pass http://mall-loveu;
            proxy_set_header    Host                $http_host;
            proxy_set_header    X-Real-IP           $remote_addr;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    X-NginX-Proxy       true;
        }
    }


#https配置
    # HTTPS server
    #
    #server {
    #    listen       443 ssl; #监听https端口
    #    server_name  localhost; #配置访问域名,域名可以有多个,用空格隔开

    #    ssl_certificate      cert.pem; #证书位置
    #    ssl_certificate_key  cert.key; #私钥位置

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;  #密码加密方式
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        try_files $uri $uri/ index  index.html index.htm;
    #    }
    #}

}

 

【location 指令说明】

语法:

location [ = | ~ | ~* | ^~] uri {

}

~ 符号读作破折号。
=:用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理请求。
~:用于表示 uri 包含正则表达式,并且区分大小写。
~*:用于表示 uri 包含正则表达式,并且不区分大小写。
^~:用于不含正则表达式的 uri 前,要求nginx服务器找到识别 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。

 

posted @ 2022-01-02 20:10  民工黑猫  阅读(759)  评论(1编辑  收藏  举报