站点图标 江湖人士

最新Centos 7安装Nginx指南

最新Centos 7安装Nginx指南?在我最近的工作中,我需要在 centos 7 反向代理服务器上安装 nginx,以便我的客户将不同的域名解析到该服务器以用于他们自己的业务。

如果你想检查 centos 7 服务器是否安装了 nginx,你只需执行这个命令。

最新Centos 7安装Nginx指南

执行 ps -ef | grep nginx 命令(ps -ef:列出所有进程,grep nginx:过滤掉与 nginx 无关的进程)

ps -ef | grep nginx

查看 nginx 进程ID

ps -C nginx -o pid

这些是一些不必要的步骤,如果您确定服务器上没有安装 nginx,那么接下来是核心步骤。

Centos 7安装Nginx指南

在开始之前,您需要确保您的帐户具有 sudo 权限,最好是您可以使用 root 帐户。

1. 添加 EPEL 存储库

连接到你的 centos 7 服务器 wia SSH(我喜欢使用putty),然后使用 yum 命令安装扩展的 pacage 存储库:

sudo yum install epel-release

系统将提示您验证是否要安装该软件。键入 y,然后输入继续。

-
Total                                                                       1.8 MB/s | 2.3 MB  00:00:01
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]: y

接下来,您将安装实际的nginx软件包。

2. 安装 Nginx

在putty/终端上输入此命令,表示执行此命令。完成第一步意味着 EPEL 存储库安装成功。

sudo yum install nginx

3. 启动 nginx

sudo systemctl enable nginx
sudo systemctl start nginx

以上 2 条指令分别指示启用和启动 Nginx。

4. 检查 Nginx 状态

sudo systemctl status nginx
output
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2023-05-13 10:10:28 CST; 10s ago
  Process: 250717 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 250714 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 250711 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 250719 (nginx)
   CGroup: /system.slice/nginx.service
           ├─250719 nginx: master process /usr/sbin/nginx
           ├─250720 nginx: worker process
           ├─250721 nginx: worker process
           ├─250722 nginx: worker process
           ├─250723 nginx: worker process
           ├─250724 nginx: worker process
           ├─250725 nginx: worker process
           ├─250726 nginx: worker process
           └─250727 nginx: worker process

May 13 10:10:28 adsl-172-10-2-37.dsl.sndg02.sbcglobal.net systemd[1]: Starting The nginx HTTP and revers....
May 13 10:10:28 adsl-172-10-2-37.dsl.sndg02.sbcglobal.net nginx[250714]: nginx: the configuration file /...k
May 13 10:10:28 adsl-172-10-2-37.dsl.sndg02.sbcglobal.net nginx[250714]: nginx: configuration file /etc/...l
May 13 10:10:28 adsl-172-10-2-37.dsl.sndg02.sbcglobal.net systemd[1]: Started The nginx HTTP and reverse....
Hint: Some lines were ellipsized, use -l to show in full.

Centos 7安装Nginx指南

常用nginx命令

查看 nginx conf 文件目录

nginx -t
sudo systemctl stop nginx  --停止nginx
sudo systemctl start nginx  --启动nginx
sudo systemctl restart nginx  --重启nginx
sudo systemctl reload nginx  --重载配置
sudo systemctl disable nginx --禁用自动启动
sudo systemctl enable nginx --启用自动启动

常见Nginx反代配置

server{
#    listen   443;
        listen   80;
    server_name  znlive.com;
    client_max_body_size 1G;
    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            'znlive.com';
        proxy_set_header   X-Forwarded-Proto $scheme;              
            proxy_pass http://172.3.2.12:8080;
        index index.html index.htm;
    }
}

server{
#    listen   443;
        listen   80;
    server_name  jhrs.com;
    client_max_body_size 1G;
    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            'jhrs.com';
        proxy_set_header   X-Forwarded-Proto $scheme;              
            proxy_pass http://172.3.2.12:8081;
        index index.html index.htm;
    }
}

server{
#    listen   443;
        listen   80;
    server_name  by3.org;
    client_max_body_size 1G;
    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            'by3.org';
        proxy_set_header   X-Forwarded-Proto $scheme;              
            proxy_pass http://172.3.2.12:8082;
        index index.html index.htm;
    }
}

server{
#    listen   443;
        listen   80;
    server_name  guowaivps.org;
    client_max_body_size 1G;
    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host            'guowaivps.org';
        proxy_set_header   X-Forwarded-Proto $scheme;              
            proxy_pass http://172.3.2.12:8083;
        index index.html index.htm;
    }
}
最新Centos 7安装Nginx指南 3

Centos 7安装Nginx指南

防火墙端口设置

通常我们不需要设置额外的端口,但是例如,如果您需要打开端口 80 和 443 或其他端口,您可以执行以下命令。如果防火墙阻止端口 80 和 443,请使用以下命令启用它们。

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
退出移动版