一、准备工作

  1. 安装依赖项

    • gcc: 安装gcc环境
    • pcre库: nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库,pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
    • zlib库: zlib提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在Centos上安装zlib库。
    • OpenSSL: OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在Centos安装OpenSSL库。
     yum install -y gcc-c++
     yum install -y pcre-devel 
     yum install -y zlib zlib-devel
     yum install -y openssl openssl-devel
    
  2. 下载 nginx 安装包 下载地址
    这里下载的是 nginx-1.20.1.tar.gz 安装包,并放到 root 目录
  3. /usr/local/ 下创建 nginx 文件夹

    cd /usr/local/ 
    mkdir nginx 
    cd /usr/local/ 

二、解压

  1. nginx 安装包解压到 /usr/local/nginx

    tar -zxvf /root/nginx-1.20.1.tar.gz -C ./ 

    解压完之后, /usr/local/nginx ⽬录中会出现⼀个 nginx-1.20.1 目录

三、编译安装

  1. 进入 nginx 目录,执行下面命令,编译安装

    cd /usr/local/nginx-1.20.1 
  2. 可选自定义配置(二选一)
    1).默认配置

    ./configure

    2).自定义配置

    ./configure --prefix=/usr/local/nginx \
    --sbin-path=/usr/local/nginx/sbin/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --pid-path=/usr/local/nginx/logs/nginx.pid  \
    --lock-path=/usr/local/nginx/logs/nginx.lock \
    --error-log-path=/usr/local/nginx/logs/error.log \
    --http-log-path=/usr/local/nginx/logs/access.log \
    --with-http_gzip_static_module \
    --http-client-body-temp-path=/usr/local/nginx/client_body_temp \
    --http-proxy-temp-path=/usr/local/nginx/proxy_temp \
    --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \
    --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \
    --http-scgi-temp-path=/usr/local/nginx/proxy_temp \
    --with-http_ssl_module \
    --with-http_v2_module
  3. 编译安装

    make && make install

四、启动nginx

  1. 注意其配置⽂件位于

    /usr/local/nginx/conf/nginx.conf
  2. 启动 nginx 服务

    /usr/local/nginx/sbin/nginx

    访问 http://192.168.10.103/ 看到Welcome to nginx!

  3. 停⽌ nginx 服务

    /usr/local/nginx/sbin/nginx -s stop
  4. 修改了配置⽂件后想重新加载 nginx

    /usr/local/nginx/sbin/nginx -s reload
  5. 重启服务

    systemctl restart nginx.service
  6. 查看 nginx状态

    systemctl status nginx.service
  7. 正常停止或关闭Nginx

    nginx -s quit
  8. 设置环境变量

    whereis nginx
    ln -s /usr/local/nginx/sbin/nginx  /usr/bin/nginx
    nginx -v

五、nginx服务开机自启

先停止nginx,在设置自动重启,最后用systemctl restart nginx 启动nginx服务

  1. /usr/lib/systemd/system目录下添加nginx.service,内容如下:

    vim /usr/lib/systemd/system/nginx.service

    注意看自己的配置文件的路径,根据自己情况修改

    [Unit]
    Description=nginx web service
    Documentation=http://nginx.org/en/docs/
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    PrivateTmp=true
    
    [Install]
    WantedBy=default.target
  2. 添加完成后如果权限有问题需要进行权限设置

    chmod 755 /usr/lib/systemd/system/nginx.service
  3. 使用系统命令来操作Nginx服务

    systemctl start nginx         #启动
    systemctl stop nginx          #停止
    systemctl restart nginx       #重启
    systemctl reload nginx        #重新加载配置文件
    systemctl status nginx        #查看nginx状态
    systemctl enable nginx        #开机启动

nginx升级

  1. 如果之前没有安装 nginx 的某个模块,重新进行编译

    ./configure 
    --with-http_v2_module
  2. 执行make编译

    make
  3. 如果是首次安装Nginx,执行 make install ,如果是升级,执行 make upgrade

大伟
4 声望0 粉丝