ubuntu/debian编译安装nginx

1. 一键脚本

bash <(curl -Lso- https://files.kkktu.com/nginx/install.sh)

2. 脚本详情

install.sh

#!/bin/bash
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

#下载指定版本nginx

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 /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 https://files.kkktu.com/nginx/nginx.conf
curl -o /tmp/example.conf https://files.kkktu.com/nginx/example.conf

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

#nginx服务
curl -o /tmp/nginx.service https://files.kkktu.com/nginx/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

/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

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

Revision #21
Created 21 November 2023 08:08:47 by Admin
Updated 22 November 2023 08:16:38 by Admin