Centos 7 安装 Nginx

685 阅读6分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

引言

Nginx 是一种流行的高性能 Web 服务器。本文记录了如何在 CentOS 7 服务器上安装和启动 Nginx。

第 1 步 — 添加 EPEL 软件存储库

首先通过 SSH 连接到您的 CentOS 7 机器,添加 CentOS 7 EPEL 存储库,然后使用yum命令安装扩展包存储库:

sudo yum install epel-release

系统将提示您确认是否要安装该软件。y然后键入ENTER以继续。

接下来,安装nginx软件包。

第 2 步 — 安装 Nginx

现在 EPEL 存储库已安装在你的服务器上,使用以下yum命令安装 Nginx:

sudo yum install nginx

再次,对验证提示回答是,然后 Nginx 将完成安装。

第 3 步 — 启动 Nginx

Nginx 安装后不会自动启动。要让 Nginx 运行,请使用以下systemctl命令:

sudo systemctl start nginx

您可以使用以下命令检查服务的状态systemctl status

sudo systemctl status nginx
Output
 nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-01-24 20:14:24 UTC; 5s ago
  Process: 1898 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1896 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1895 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 1900 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1900 nginx: master process /usr/sbin/nginx
           └─1901 nginx: worker process

Jan 24 20:14:24 centos-updates systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 24 20:14:24 centos-updates nginx[1896]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 24 20:14:24 centos-updates nginx[1896]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 24 20:14:24 centos-updates systemd[1]: Started The nginx HTTP and reverse proxy server.

服务应该是active

如果你打开了防火墙,运行以下命令以允许 HTTP 和 HTTPS 流量:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

现在可以通过在 Web 浏览器中访问服务器的公共 IP 地址来立即进行检查:

http://server_domain_name_or_IP/

你将看到默认的 CentOS 7 Nginx 网页,用于提供信息和测试目的。它应该看起来像这样:

CentOS 7 Nginx 默认

如果你看到此页面,那么您的 Web 服务器现在已正确安装。

注意: 要查找服务器的公共 IP 地址,请键入以下命令查找计算机上的网络接口:

ip addr
Output
1. lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

. . .
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

. . .

根据服务器上可用的硬件,您可能会在此处看到许多接口。接口是本地环lo回接口,不是我们想要的。在我们上面的例子中,eth0接口就是我们想要的。

获得接口名称后,您可以运行以下命令来显示服务器的公共 IP 地址。替换您在上面找到的接口名称:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'

在继续之前,如果你希望在系统启动时启用 Nginx,输入以下命令:

sudo systemctl enable nginx

Nginx 现在已安装并正在运行。

第 4 步 - 探索和配置 Nginx

如果你想通过 Nginx 开始为你自己的页面或应用程序提供服务,你需要知道 Nginx 配置文件和默认服务器根目录的位置。

默认服务器根目录

默认的服务器根目录是/usr/share/nginx/html. 放置在那里的文件将在你的网络服务器上暴露。此位置在 Nginx 附带的默认服务器块配置文件中指定,该文件位于/etc/nginx/conf.d/default.conf.

服务器块配置

任何额外的服务器块,在 Apache 中称为虚拟主机,都可以通过在/etc/nginx/conf.d. 以该目录结尾的文件.conf将在 Nginx 启动时加载。

Nginx 全局配置

主要的 Nginx 配置文件位于/etc/nginx/nginx.conf. 你可以在此处更改设置,例如运行 Nginx 守护进程的用户,以及在 Nginx 运行时生成的工作进程的数量等。

配置示例

#user  nobody;                        *#* 运行用户,默认即可,可以不进行设置
worker_processes  auto;                *# Nginx* 进程数,一般设置为和 *CPU* 核数一样
error_log  /var/log/nginx/error.log warn;   *# Nginx* 的错误日志存放目录
pid        /var/run/nginx.pid;      *# Nginx* 服务启动时的 *pid* 存放位置

events {
    use epoll;     *#* 使用*epoll*的*I/O*模型 *(* 如果你不知道*Nginx*该使用哪种轮询方法,会自动选择一个最适合你操作系统的 *)*
    worker_connections 1024;   *#* 每个进程允许最大并发数

}

http {   *#* 配置使用最频繁的部分,代理、缓存、日志定义等绝大多数功能和第三方模块的配置都在这里设置
    *#* 设置日志模式
    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  /var/log/nginx/access.log  main;   *# Nginx*访问日志存放位置

    sendfile            on;   *#* 开启高效传输模式

    tcp_nopush          on;   *#* 减少网络报文段的数量

    tcp_nodelay         on;

    keepalive_timeout   65;   *#* 保持连接的时间,也叫超时时间,单位秒

    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;      *#* 文件扩展名与类型映射表

    default_type        application/octet-stream;   *#* 默认文件类型

    include /etc/nginx/conf.d/*.conf;   *#* 加载子配置项    

    server {

    listen       80;       *#* 配置监听的端口

    server_name  localhost;    *#* 配置的域名,或者主机IP地址

    location / {

    root   html;  *#* 网站根目录

    index  index.html index.htm;   *#* 默认首页文件

    deny 172.168.22.11;   *#* 禁止访问的*ip*地址,可以为*all*

    allow 172.168.33.44; *#* 允许访问的*ip*地址,可以为*all*

    }
   
    error_page 500 502 503 504 /50x.html;  *#* 默认*50x*对应的访问页面

    error_page 400 404 error.html;   *#* 同上

    }
    
    #server可以配置多个 
    server {
        listen       8089;
        server_name  116.xx.xx.xx;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /video/ {
            root   /public/video;
            autoindex on;
        } 
    } 

}

常用命令

systemctl start firewalld  # 开启防火墙
systemctl stop firewalld   # 关闭防火墙
systemctl status firewalld # 查看防火墙开启状态,显示running则是正在运行
firewall-cmd --reload      # 重启防火墙,永久打开端口需要reload一下

# 添加开启端口,--permanent表示永久打开,不加是临时打开重启之后失效
firewall-cmd --permanent --zone=public --add-port=8888/tcp

# 查看防火墙,添加的端口也可以看到
firewall-cmd --list-all

nginx -s reload  # 向主进程发送信号,重新加载配置文件,热重启
nginx -s reopen	 # 重启 Nginx
nginx -s stop    # 快速关闭
nginx -s quit    # 等待工作进程处理完成后关闭
nginx -T         # 查看当前 Nginx 最终的配置
nginx -t -c <配置路径>    # 检查配置是否有问题,如果已经在配置目录,则不需要-c

ystemctl start nginx    # 启动 Nginx
systemctl stop nginx     # 停止 Nginx
systemctl restart nginx  # 重启 Nginx
systemctl reload nginx   # 重新加载 Nginx,用于修改配置后
systemctl enable nginx   # 设置开机启动 Nginx
systemctl disable nginx  # 关闭开机启动 Nginx
systemctl status nginx   # 查看 Nginx 运行状态

其他安装方法 ——— make构建安装方法

为Nginx创建一个文件夹

cd /usr/local/
mkdir nginx
cd nginx

下载nginx-1.13.7.tar.gz

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

解压nginx-1.13.7.tar.gz

tar -xvf nginx-1.13.7.tar.gz

进入Nginx目录

cd /usr/local/nginx/nginx-1.13.7

执行以下命令

./configure
make
make install

配置文件如下,可修改

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