1.如果你的centos系统没有wget就先安装wget
yum install wget
2.下载nginx
wget -c https://nginx.org/download/nginx-1.21.4.tar.gz
3.解压压缩包
tar -zxvf nginx-1.21.4.tar.gz
4.进入解压出来的文件夹nginx-1.21.4 执行编译 --prefix=来指定程序存放路径
./configure --prefix=/usr/local/nginx
如果报错
./configure: error: C compiler cc is not found
说明没有gcc-c++ 的包,安装gcc-c++
yum install -y gcc-c++
如果报错
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
安装
yum install -y pcre pcre-devel
如果报错
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
安装
yum install -y zlib zlib-devel
编译完成后
make
如果报错 command not found
yum install make
make完成后
make install
启动Nginx
进入安装好的目录/usr/local/nginx/sbin
./nginx 启动 ./nginx -s stop 快速停止 ./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求 ./nginx -s reload 重新加载配置
如果访问不到,可能是防火墙的问题
关于防火墙
关闭防火墙
systemctl stop firewalld.service
禁止防火墙开机启动
systemctl disable firewalld.service
或者放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
下面的不是必须
安装成系统服务
创建服务脚本
vi /usr/lib/systemd/system/nginx.service
服务脚本内容
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.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 -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加载系统服务
systemctl daemon-reload
启动服务
systemctl start nginx.service
开机启动
systemctl enable firewalld.service