linux/centOS 下安装 ngnix

  Nginx 是一款轻量级的 Web 服务器/反向代理服务器,比较流行,建议在 Linux 下安装运行。html

Nginx 须要的依赖

它们包括:gcc,openssl,zlib,pcre(可经过rpm -q命令查询是否已安装),已安装则跳过nginx

  • 安装 gcc
    安装 Nginx 的编译环境 gcc
yum install gcc-c++
  • 安装 pcre pcre-devel
    nginxhttp 模块使用 pcre 解析正则表达式,因此安装 perl 兼容的正则表达式库
yum install -y pcre pcre-devel
  • 安装 zlib
    nginx 使用 zlibhttp 包的内容进行 gzip
yum install -y zlib zlib-devel
  • 安装 Open SSL
    nginx 不只支持 http 协议,还支持 https(即在 ssl 协议上传输 http),若是使用了 https ,须要安装 OpenSSL
yum install -y openssl openssl-devel

Nginx 的下载

官网下载

1.直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html正则表达式

2.使用wget命令下载(推荐放在 /usr/local路径下)swift

wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

 

我下载的是1.12.2版本,这个是目前的稳定版。
vim

Nginx 的安装

  解压

tar -zxvf nginx-1.12.2.tar.gz

进入 解压后的目录
cd /usr/local/nginx-1.12.2
cd /usr/local/nginx-1.12.2

  配置

   默认就能够了。固然,若是你要本身配置目录也是能够的

  1.使用默认配置ruby

     ./configurebash

  2.自定义配置(不推荐)服务器

./configure \
--prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi

 

    注意:使用默认配置时,nginx 被安装到 /usr/local/nginx 下。测试

  编译、安装(若是使用默认配置,则nginx安装在/usr/local/nginx目录下)

make 
make install

  查找安装路径:

whereis nginx

 

  配置环境变量(可选)

vim /etc/profile

    在合适位置添加环境变量ui

export NGINX_HOME=/usr/local/nginx export PATH=$PATH:$NGINX_HOME/sbin

   使配置生效

 source /etc/profile

Nginx 的运行

由于将 Nginx 配置到了环境变量中,所以,在任何路径下均可以直接使用 nginx 命令,而不须要进入 nginx 路径下执行。

若是没有配置环境变量,则须要先进入  cd /usr/local/nginx/sbin

  • 启动nginx
cd  /usr/local/nginx/sbin
./nginx
  • 查看 nginx 进程
ps -ef | grep nginx
  • 关闭nginx

./nginx -s stop:此方式至关于先查出nginx进程id再使用kill命令强制杀掉进程。

./nginx -s quit:此方式中止步骤是待nginx进程处理任务完毕进行中止。 ./nginx -s stop
./nginx -s quit

 

  • 重启nginx    

    1.先中止再启动(推荐):
       对 nginx 进行重启至关于先中止再启动,即先执行中止命令再执行启动命令。以下:

./nginx -s quit ./nginx

     2.修改配置文件,从新加载配置文件:
        当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效须要重启 nginx,使用-s reload不用先中止 ngin x再启动 nginx 便可将配置信息在 nginx 中生效,以下:
         ./nginx -s reload

  • 测试 nginx 配置脚本是否运行正常

       一般能够经过这个命令查看 nginx 配置文件的位置

./nginx -t 

 

  • nginx 开机自启

        即在rc.local增长启动代码就能够了。

vi /etc/rc.local

       增长一行 /usr/local/nginx/sbin/nginx


       设置执行权限:

 chmod 755 rc.local

 

 

 

  • nginx 指定配置文件启动

   nginx -c /usr/local/nginx/conf/nginx.conf

 

 

参考文章:https://blog.csdn.net/qq_30038111/article/details/79410354

相关文章
相关标签/搜索