Skip to main content

ubuntu/debian编译安装nginx

install.sh

#!/bin/bash

#更新依赖
sudo apt-get update -y

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

#下载指定版本nginx
echo -n "entry nginx version: (default:1.25.3)"

read nginx_version

if [ -z $nginx_version]; then
	nginx_version="1.25.3"
fi

curl -o /tmp/nginx-$nginx_version.tar.gz https://nginx.org/download/nginx-$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

make

sudo make install

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

/usr/local/lib/nginx/conf/nginx.conf

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/nginx.pid;


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;
}

/lib/systemd/system/nginx.service

# 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:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
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/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

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服务状态