CentOS 安装 Nginx

CentOS 安装 Nginx

一、安装Nginx前的准备

首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装

$ yum install gcc-c++
$ yum install pcre pcre-devel
$ yum install zlib zlib-devel
$ yum install openssl openssl--devel

下载Nginx最新版

查看Nginx最新版

从官网下载最新稳定版的nginx

$ wget http://nginx.org/download/nginx-1.14.2.tar.gz

解压nginx压缩包

$ tar -zxvf nginx-1.14.2.tar.gz
$ cd nginx-1.14.2

编译安装

官方编译安装文档

$ ./configure --prefix=/usr/local/nginx
$ ./configure --with-http_ssl_module

$ make && make install

查看Nginx安装位置

$ whereis nginx
nginx: /usr/local/nginx

三、Nginx的启动和关闭

检查配置文件是否正确

$ /usr/local/nginx/sbin/nginx -t
或
$ cd /usr/local/nginx/sbin
$ ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

进入到Nginx目录,查看版本

$ ./sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module
configure arguments: --prefix=/usr/local/nginx

启动Nginx(默认配置文件)

$ ./sbin/nginx
root     18974     1  0 21:07 ?        00:00:00 nginx: master process ./sbin/nginx
nobody   18975 18974  0 21:07 ?        00:00:00 nginx: worker process
root     18979 13354  0 21:07 pts/1    00:00:00 grep --color=auto nginx

查看nginx启动状态

$ ps -aux|grep nginx
root     18974  0.0  0.0  20552   612 ?        Ss   21:07   0:00 nginx: master process ./sbin/nginx
nobody   18975  0.0  0.0  21004  1328 ?        S    21:07   0:00 nginx: worker process
root     19171  0.0  0.0 112720   980 pts/1    S+   21:24   0:00 grep --color=auto nginx

更改Nginx配置文件

//cd 
$ cd /usr/local/nginx/conf/

//修改
$ vim nginx.conf

//释放注释并修改    
    pid    /run/nginx.pid

添加nginx到systemctl

  • 进入到 /usr/lib/systemd/system 目录下,编辑文件 nginx.service
$ cd /usr/lib/systemd/system
$ vim nginx.service
  • nginx.service 加入如下代码
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
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
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 设置开机自启动
$ systemctl enable nginx.service
  • 命令
    • 开机自启:systemctl enable nginx.service
    • 停止开机自启:systemctl disable nginx.service
    • 查看状态:systemctl status nginx.service
    • 启动服务:systemctl start nginx.service
    • 停止服务:systemctl stop nginx.service
    • 重启服务:systemctl restart nginx.service
    • 重新读取nginx配置:systemctl reload nginx.service

你可能感兴趣的:(CentOS)