nginx编译和启动脚本

Nginx下载地址

  • CentOS7.4
#!/bin/bash
yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ wget
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -xzf nginx-1.8.1.tar.gz -C /usr/src/ && cd /usr/src/nginx-1.8.1
useradd -s /sbin/nologin www
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install
#测试配置
/usr/local/nginx/sbin/nginx -t 
firewall-cmd --add-port=80/tcp --permanent
firewall--cmd --reload
#启动主程序
/usr/local/nginx/sbin/nginx
ps -ef | grep nginx
echo "nginx instlled"
  • Ubuntu18.04
apt update
#安装依赖:gcc、g++依赖库
apt install build-essential libtool
#安装 pcre依赖库(http://www.pcre.org/)
apt install libpcre3 libpcre3-dev
#安装 zlib依赖库(http://www.zlib.net)
apt install zlib1g-dev
apt install openssl
wget http://nginx.org/download/nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --prefix=/usr/local/nginx
make && make install

编写Nginx的systemd启动脚本,将脚本放在/lib/systemd/system/目录下

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs
#After参数设置项用来确认启动的顺序
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
#ExecStartPre参数可确保ExecStart参数启动之前执行的命令,这里是在启动之前进行配置文件正确性的检测
ExecStartPre=/usr/local/nginx/sbin/nginx -t
#ExecStart参数用来启动Nginx服务
ExecStart=/usr/local/nginx/sbin/nginx
#ExecReload参数指定重新加载时执行的命令
ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop参数指定停止Nginx服务时执行的命令
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=True
[Install]
WantedBy=multi-user.target

你可能感兴趣的:(nginx编译和启动脚本)