Skip to main content


1. 一键脚本

bash <(curl -Lso-

2. 脚本详情

echo -n "entry nginx version: (default:1.25.3)"

read nginx_version

sudo apt-get update -y

sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev libssl-dev -y


if [ -z $nginx_version]; then

curl -o /tmp/nginx-$nginx_version.tar.gz$nginx_version.tar.gz

echo "download completed"

tar zxvf /tmp/nginx-$nginx_version.tar.gz -C /tmp

cd /tmp/nginx-$nginx_version
./configure --prefix=/usr/local/lib/nginx --with-http_ssl_module


sudo make install

sudo rm /usr/bin/nginx
sudo ln -s /usr/local/lib/nginx/sbin/nginx /usr/bin/nginx

sudo rm -rf /tmp/nginx-$nginx_version

sudo rm -rf /tmp/nginx-$nginx_version.tar.gz
useradd www
sudo mkdir -p /home/www/nginx/conf.d

sudo chmod 777 /home/www/nginx/conf.d
curl -o /tmp/nginx.conf
curl -o /tmp/example.conf

sudo mv /tmp/nginx.conf /usr/local/lib/nginx/conf
mv /tmp/example.conf /home/www/nginx/conf.d

curl -o /tmp/nginx.service
sudo mv /tmp/nginx.service /lib/systemd/system/nginx.service

sudo mkdir -p /var/log/nginx

sudo systemctl enable nginx
sudo systemctl start nginx


user  www;
worker_processes  auto;

error_log  /var/log/nginx/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /run/;

events {
    worker_connections  1024;

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    include /home/www/nginx/conf.d/*.conf;


# Stop dance for nginx
# =======================
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
# nginx signals reference doc:
Description=A high performance web server and a reverse proxy server

ExecStartPre=/usr/bin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/bin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/bin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/


3. nginx服务指令

sudo systemctl enable nginx #开机启动nginx
sudo systemctl restart nginx #重启nginx
sudo systemctl reload nginx #重载nginx配置
sudo systemctl restart nginx #启动nginx
sudo systemctl status nginx #查看nginx服务状态