linux/centOS 下安装 ngnix

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

Nginx 需要的依赖

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

  • 安装 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路径下)

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

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

Nginx 的安装

  解压

tar -zxvf nginx-1.12.2.tar.gz

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

  配置

   默认就可以了。当然,如果你要自己配置目录也是可以的

  1.使用默认配置

     ./configure

  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

    在合适位置添加环境变量

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

   使配置生效

 source /etc/profile

Nginx 的运行

因为将 Nginx 配置到了环境变量中,因此,在任何路径下都可以直接使用 nginx 命令,而不需要进入 nginx 路径下执行。

如果没有配置环境变量,则需要先进入  cd /usr/local/nginx

  • 启动nginx
 cd  /usr/local/nginx
./nginx
  • 查看 nginx 进程
ps -ef | grep nginx
  • 关闭nginx
./nginx -s stop
./nginx -s quit
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
  • 重启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

猜你喜欢

转载自www.cnblogs.com/tyhj-zxp/p/9450242.html