将nginx安装包上传到服务器上
解压
tar -zxvf nginx-1.22.1.tar.gz
进入
cd nginx-1.22.1 [root@192 nginx-1.22.1]# ll 总用量 804 drwxr-xr-x. 6 1001 1001 4096 5月 10 20:33 auto -rw-r--r--. 1 1001 1001 317399 10月 19 2022 CHANGES -rw-r--r--. 1 1001 1001 485035 10月 19 2022 CHANGES.ru drwxr-xr-x. 2 1001 1001 168 5月 10 20:33 conf -rwxr-xr-x. 1 1001 1001 2590 10月 19 2022 configure drwxr-xr-x. 4 1001 1001 72 5月 10 20:33 contrib drwxr-xr-x. 2 1001 1001 40 5月 10 20:33 html -rw-r--r--. 1 1001 1001 1397 10月 19 2022 LICENSE drwxr-xr-x. 2 1001 1001 21 5月 10 20:33 man -rw-r--r--. 1 1001 1001 49 10月 19 2022 README drwxr-xr-x. 9 1001 1001 91 5月 10 20:33 src
使用编译方式进行安装
[root@192 nginx-1.22.1]# ./configure --prefix=/usr/local/nginx checking for OS + Linux 3.10.0-862.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found
报错一是缺少C的编译环境
解决:
安装gcc相关依赖 yum install -y gcc 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.
解决:安装pcre库
yum -y install pcre pcre-devel #yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-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.
解决:安装zlib库
yum -y install zlib zlib-devel
不报错后
make && make install
启动
进入目录
/usr/local/nginx/sbin ./nginx #启动 ./nginx -s stop #立即停止 ./nginx -s quit #优雅停止,退出前完成已经接收的连接 ./nginx -s reload #重新加载配置
测试,浏览器输入虚拟机ip地址
关于防火墙
关闭防火墙
systemctl stop firewalld.service
禁止防火墙开机启动
systemctl disable firewalld.service
放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
将nginx配置成系统服务
vi /usr/lib/systemd/system/nginx.service
将下列配置粘贴
[Unit] Description=nginx web service 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
生效文件
systemctl daemon-reload
开机自启
systemctl enable nginx