Linux环境下安装Nginx

一、准备工作

  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

  1. 下载 nginx 安装包 下载地址
    这里下载的是 nginx-1.20.1.tar.gz 安装包,并放到 root 目录
  2. /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

  1. 可选自定义配置(二选一)
    1).默认配置

./configure

2).自定义配置

./configure --prefix=/usr/local/nginx
–sbin-path=/usr/local/nginx/sbin/nginx
–modules-path=/usr/local/nginx/modules
–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
–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_gzip_static_module
–with-http_ssl_module
–with-http_v2_module

  1. 编译安装

make && make install

四、启动nginx

  1. 注意其配置⽂件位于

/usr/local/nginx/conf/nginx.conf

  1. 启动 nginx 服务

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

  1. 停⽌ nginx 服务

/usr/local/nginx/sbin/nginx -s stop

  1. 修改了配置⽂件后想重新加载 nginx

/usr/local/nginx/sbin/nginx -s reload

  1. 重启服务

systemctl restart nginx.service

  1. 查看 nginx状态

systemctl status nginx.service

  1. 正常停止或关闭Nginx

nginx -s quit

  1. 设置环境变量

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
  1. 添加完成后如果权限有问题需要进行权限设置

chmod 755 /usr/lib/systemd/system/nginx.service

  1. 使用系统命令来操作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

  1. 执行编译命令

make

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

  • 18
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值