nginx for centos安装

300 阅读2分钟

1. 预构建包安装

1.1 Installing a Prebuilt CentOS/RHEL Package from an OS Repository

  1. Install the EPEL repository:

    $ sudo yum install epel-release
    
  2. Update the repository:

    $ sudo yum update
    
  3. Install NGINX Open Source:

    $ sudo yum install nginx
    
  4. Verify the installation:

    $ sudo nginx -v
    nginx version: nginx/1.6.3
    

1.2 Installing a Prebuilt CentOS/RHEL Package from the Official NGINX Repository

  1. Set up the yum repository for RHEL or CentOS by creating the file nginx.repo in /etc/yum.repos.d, for example using vi:

    $ sudo vi /etc/yum.repos.d/nginx.repo
    
  2. Add the following lines to nginx.repo:

    [nginx]
    name=nginx repo
    baseurl=https://nginx.org/packages/mainline/<OS>/<OSRELEASE>/$basearch/
    gpgcheck=0
    enabled=1
    

    where:

    • The /mainline element in the pathname points to the latest mainline version of NGINX Open Source; delete it to get the latest stable version

    • <OS> is either rhel or centos

    • <OSRELEASE> is the release number (6, 6._x_, 7, 7._x_ and so on)

      For example, to get the latest mainline package for CentOS 7, insert:

      [nginx-stable]
      name=nginx stable repo
      baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
      gpgcheck=1
      enabled=1
      gpgkey=https://nginx.org/keys/nginx_signing.key
      module_hotfixes=true
      
  3. Save the changes and quit vi (press ESC and type wq at the : prompt).

  4. Update the repository:

    $ sudo yum update
    
  5. Install the NGINX Open Source package:

    $ sudo yum install nginx
    
  6. Start NGINX Open Source:

    $ sudo nginx
    
  7. Verify that NGINX Open Source is up and running:

    $ curl -I 127.0.0.1
    HTTP/1.1 200 OK
    Server: nginx/1.13.8
    

2. systemctl管理nginx

sudo systemctl enable nginx  # 开机自启
sudo systemctl start nginx   # 启动
sudo systemctl stop nginx    # 停止

3. 查看nginx配置文件

3.1 查看Nginx安装目录

使用命令 which nginxwhereis nginx 。 前者只适用于软件的安装目录被添加进了系统 Path 的的情况。

3.2 查看 nginx 配置文件所在目录

使用命令 find / | grep nginx.conf 。其作用为:查找 (find) ,在系统根目录 (/) 及其子目录下,所有名为 nginx.conf 的文件。

执行结果如下:

/usr/xxxx/www-servers/nginx/files/nginx.conf
/etc/nginx/nginx.conf

我们得到两条结果,说明存在两个 Nginx 配置文件。第二步,需要确认当前运行的服务器使用的是哪个配置文件。

3.3 确认 Nginx 启动的是哪个配置文件

使用命令 ps -ef | grep nginx 。其作用为:查看正在运行的名叫 nginx 的进程 (ps)。

-e : 显示所有进程。 -f : 全格式。

执行结果如下:

root     22524     1  0 06:47 ?        00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf
nginx    22525 22524  0 06:47 ?        00:00:00 nginx: worker process
root     22634 22471  0 07:16 pts/1    00:00:00 grep --colour=auto nginx

从第一行末尾可以看到,当前启动的是 /etc/nginx/nginx.conf 。

引用自:
1.nginx docs
2. Linux下检测Nginx安装目录,修改配置,重启