nginx的编译安装
服务器信息:centos7
安装步骤:
- 下载安装nginx源码包
curl -O http://nginx.org/download/nginx-1.17.8.tar.gz
- 安装依赖关系包
yum install gcc pcre-devel openssl-devel -y
- 解压
- 进入解压后的目录
tar xf nginx-1.17.8.tar.gz
cd nginx-1.17.8
- 相关配置
- 编译然后再安装
./configure --prefix=/usr/local/nginx2 --with-http_realip_module --with-http_ssl_module --with-pcre
make && make install
- 启动和设置开机启动 ,加权限
/usr/local/nginx2/sbin/nginx
echo "/usr/local/nginx2/sbin/nginx" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local
- 添加环境变量
PATH=$PATH:/usr/local/nginx2/sbin
echo "PATH=$PATH:/usr/local/nginx2/sbin" >>/etc/bashrc
- 关闭防火墙
service firewalld stop
systemctl disable firewalld
- 启动nginx服务
cd /etc/local/nginx2/sbin
./nginx
- 编译安装的不能使用systemctl enable nginx去设置开机启动,因为没有相关的脚本和配置,编写脚本
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx9/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /usr/local/nginx9/logs/nginx.pid
ExecStartPre=/usr/local/nginx9/sbin/nginx -t
ExecStart=/usr/local/nginx9/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 验证
nginx -s stop
lsof -i:80
systemctl start nginx
lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 69933 root 8u IPv4 17909556 0t0 TCP *:http (LISTEN)
nginx 69934 lihu 8u IPv4 17909556 0t0 TCP *:http (LISTEN)
systemctl stop nginx
lsof -i:80