Linux编译安装nginx
Linux编译安装nginx
下载nginx包
官方下载地址:https://nginx.org/en/download.html

下载后上传至Linux opt目录下

解压安装:
1 tar zxvf nginx-1.21.6.tar.gz
2 cd nginx-1.21.6
3 ./configure --prefix=/usr/local/nginx # --prefix:选择nginx要安装的目录
指安装路径是/usr/local/nginx 4 make 5 make install
./configure后的报错和解决办法
1 checking for OS 2 + Linux 3.10.0-693.el7.x86_64 x86_64 3 checking for C compiler ... not found 4 ./configure: error: C compile
提示:
安装gcc(C语言编译器)
yum install -y gcc
提示:
1 /configure: error: the HTTP rewrite module requires the PCRE library. 2 You can either disable the module by using --without-http_rewrite_module 3 option, or install the PCRE library into the system, or build the PCRE library 4 statically from the source with nginx by using --with-pcre=<path> option.
安装perl库
yum install -y pcre pcre-devel
提示:
1 ./configure: error: the HTTP gzip module requires the zlib library. 2 You can either disable the module by using --without-http_gzip_module 3 option, or install the zlib library into the system, or build the zlib library 4 statically from the source with nginx by using --with-zlib=<path> option.
安装zlib库:
yum install -y zlib zlib-devel
进行编译
1 make 2 make install
启动nginx
1 [root@bogon /]# cd /usr/local/nginx/sbin 2 [root@bogon sbin]# ll 3 total 3804 4 -rwxr-xr-x 1 root root 3892008 Feb 14 14:17 nginx 5 [root@bogon sbin]# ./nginx
关闭防火墙
systemctl stop firewalld
测试是否成功运行
ip地址:80端口

安装成系统服务
在如下位置创建服务脚本nginx.service
vi /usr/lib/systemd/system/nginx.service
服务脚本内容如下(注意路径要对应安装路径,这里的路径是/usr/local/nginx/sbin):
[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 nginx.service
控制命令
启动Nginx 在/usr/local/nginx/sbin目录下: ./nginx #启动nginx ./nginx -s reload #启动nginx,并重新载入配置 ./nginx -s reopen #重启nginx 关闭Nginx ./nginx -s stop #关闭nginx 杀进程 pkill -9 nginx #关闭Nginx 查看Nginx状态 ps -ef | grep nginx #查看nginx状态

浙公网安备 33010602011771号