当前位置:   article > 正文

CentOS7安装nginx_centos7 编译 nginx1.25

centos7 编译 nginx1.25

系统版本:centos7.9-2009

nginx版本:1.20.2

1、去nginx官网下载压缩包,此处选择稳定版本

nginx官网链接

2、上传到服务器指定位置/opt/tools,根据个人习惯来

解压:

tar -zxvf nginx-1.20.2.tar.gz

3、配置,使用默认配置

  • 解压后进入到nginx目录下,

执行:

./configure

 可能会出现一些错误提示

  • 错误1:
./configure: error: C compiler cc is not found

解决:安装编译器

yum -y install gcc gcc-c++ autoconf automake make
  • 错误2:
./configure: error: the HTTP rewrite module requires the PCRE library.

解决:

yum -y install pcre-devel
  • 错误3:
./configure: error: the HTTP gzip module requires the zlib library.

解决:

yum install -y zlib-devel

解决三个问题后配置成功

 4、编译安装

  1. make
  2. make install

5、启动nginx

  1. # 找到nginx所在路径
  2. whereis nginx
  3. # 进入nginx路径
  4. cd /usr/local/nginx/sbin
  5. # 启动nginx
  6. ./nginx

6、验证

访问centos7的ip

直接访问访问不到。需要关闭防火墙的80端口

  1. # 查看防火墙放开的端口
  2. firewall-cmd --zone=public --list-ports
  3. # 放开指定端口
  4. firewall-cmd --zone=public --add-port=80/tcp --permanent
  5. # 配置立即生效
  6. firewall-cmd --reload
  7. # 关闭指定端口
  8. firewall-cmd --zone=public --remove-port=5672/tcp --permanent

出现如下页面即成功

7、其他项配置:自启、环境变量

  • 配置环境变量

  1. # 打开环境变量配置文件
  2. vim /etc/profile
  3. # 在末尾加入nginx的位置
  4. export NGINX_HOME=/usr/local/nginx
  5. export PATH=$PATH:$NGINX_HOME/sbin
  6. # 使配置生效
  7. source /etc/profile
  8. # 校验配置是否成功,查看nginx版本
  9. nginx -v
  •  配置nginx自启

1、进入到cd /lib/systemd/system目录

2、创建nginx.service文件并编辑

  1. [Unit]
  2. Description=nginx
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/usr/local/nginx/sbin/nginx
  7. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  8. ExecStop=/usr/local/nginx/sbin/nginx -s quit
  9. PrivateTmp=true
  10. [Install]
  11. WantedBy=multi-user.target

Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

保存退出

3、加入开机自启

systemctl enable nginx

4、其他一些服务控制命令

  1. systemctl start nginx.service  启动nginx服务
  2. systemctl stop nginx.service  停止服务
  3. systemctl restart nginx.service  重新启动服务
  4. systemctl list-units --type=service 查看所有已启动的服务
  5. systemctl status nginx.service 查看服务当前状态
  6. systemctl enable nginx.service 设置开机自启动
  7. systemctl disable nginx.service 停止开机自启动

5、查看加入开机启动的项目

systemctl list-unit-files

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/661061
推荐阅读
相关标签
  

闽ICP备14008679号