ubuntu centos nginx安装编译部署

环境: centos7 ubuntu18.04

概览:

  1. ubuntu编译部署nginx
  2. centos编译部署nginx
  3. 建议托管文件参考传送门

一. ubuntu部署nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
#软件:nginx
#环境:centos7
#/usr/local/src/nginx-1.13.0/configure --prefix=/usr/local/nginx
set -e
echo -e "\033[33m
1.此脚本只适用于首次安装部署,多次执行此脚本会覆盖nginx配置文件 \n
2.想部署其他版本的需要自行更改链接:http://nginx.org/download/nginx-1.13.0.tar.gz 找到指定版本即可 \n \033[0m"
echo -e "\033[36m ===========================start -初始化 \033[0m"
sleep 2
nginx_v="nginx-1.16.1.tar.gz" #"nginx-1.13.0.tar.gz"
dir="nginx-1.16.1" #"nginx-1.13.0"
#yilai="gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel"
nginx_install_app() {
echo -e "\033[36m 下载安装编译的依赖包 \033[0m"
#sudo apt-get update
#安装依赖:gcc、g++依赖库
sudo apt-get install build-essential libtool -y
#安装 pcre依赖库(http://www.pcre.org/)
sudo apt-get install libpcre3 libpcre3-dev -y
#安装 zlib依赖库(http://www.zlib.net)
sudo apt-get install zlib1g-dev -y
#安装ssl依赖库
sudo apt-get install openssl -y
sudo apt install zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev -y
[ $? = 0 ] && echo -e "\033[36m 依赖:${yilai} 安装完成 \033[0m" || echo -e "\033[33m 依赖安装失败 \033[0m"
echo -e "\033[36m =============开始下载nginx压缩包 \033[0m"
wget http://nginx.org/download/${nginx_v}
tar -zxvf ./${nginx_v} -C /usr/local/src/
}
nginx_make() {
echo -e "\033[36m 开始编译安装:${nginx_v} \033[0m"
cd /usr/local/src/${dir} && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module --with-stream
echo -e "\033[36m make \033[0m"
cd /usr/local/src/${dir} && make
echo -e "\033[36m make install \033[0m"
cd /usr/local/src/${dir} && make install
[ $? = 0 ] && echo -e "\033[36m 依赖:编译安装完成全路径启动命令"/usr/local/nginx/sbin/nginx" \033[0m" || echo -e "\033[33m 编译安装失败 \033[0m"
/usr/local/nginx/sbin/nginx -t
}
nginx_system() {
echo -e "\033[36m ==============================开始建立'nginx'托管文件 \033[0m"
cat <<EOF >/etc/systemd/system/nginx.service
[Unit]
Description=nginx server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
#Type=simple
#PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -SIGHUP \$MAINPID
ExecStop=/bin/kill -SIGINT \$MAINPID
Restart=always
RestartSec=5
#StartLimitInterval=0

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start nginx.service
systemctl status nginx.service
echo -e "\033[36m
概览
nginx安装完成访问方式-----------------------------------浏览器访问: http://localhost
1. nginx执行文件全路径为:"/usr/local/nginx/sbin/nginx" 若要使用nginx命令需要以 全路径方式
2. nginx 常用命令:
/usr/local/nginx/sbin/nginx -v # -V 查看编译模块
/usr/local/nginx/sbin/nginx -V
/usr/local/nginx/sbin/nginx -t 等
3. ----------------------------------------------------nginx版本为:
\033[0m"
/usr/local/nginx/sbin/nginx -v
}
main(){
nginx_install_app
nginx_make
nginx_system
}
main

image-20230610144100669

二. centos部署nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
#软件:nginx
#环境:centos7
#/usr/local/src/nginx-1.13.0/configure --prefix=/usr/local/nginx
set -e
echo -e "\033[33m
1.此脚本只适用于首次安装部署,多次执行此脚本会覆盖nginx配置文件 \n
2.想部署其他版本的需要自行更改链接:http://nginx.org/download/nginx-1.13.0.tar.gz 找到指定版本即可 \n \033[0m"
echo -e "\033[36m ===========================start -初始化 \033[0m"
sleep 8
nginx_v="nginx-1.16.1.tar.gz" #"nginx-1.13.0.tar.gz"
dir="nginx-1.16.1" # "nginx-1.13.0"
yilai="gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel"
nginx_install_app() {
echo -e "\033[36m 下载安装编译的依赖包 \033[0m"
yum install ${yilai} -y
[ $? = 0 ] && echo -e "\033[36m 依赖:${yilai} 安装完成 \033[0m" || echo -e "\033[33m 依赖安装失败 \033[0m"
echo -e "\033[36m =============开始下载nginx压缩包 \033[0m"
wget http://nginx.org/download/${nginx_v}
tar -zxvf ./${nginx_v} -C /usr/local/src/
}
nginx_make() {
echo -e "\033[36m 开始编译安装:${nginx_v} \033[0m"
cd /usr/local/src/${dir} && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_slice_module --with-stream
echo -e "\033[36m make \033[0m"
cd /usr/local/src/${dir} && make
echo -e "\033[36m make install \033[0m"
cd /usr/local/src/${dir} && make install
[ $? = 0 ] && echo -e "\033[36m 依赖:编译安装完成全路径启动命令"/usr/local/nginx/sbin/nginx" \033[0m" || echo -e "\033[33m 编译安装失败 \033[0m"
/usr/local/nginx/sbin/nginx -t
}
nginx_system() {
echo -e "\033[36m ==============================开始建立'nginx'托管文件 \033[0m"
cat <<EOF >/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start nginx.service
systemctl status nginx.service
nginx_cmd=`/usr/local/nginx/sbin/nginx -v`
echo -e "\033[36m
概览
nginx安装完成访问方式-----------------------------------浏览器访问: http://localhost
1. 自动检查nginx安装成功--------------------------------nginx版本为:${nginx_cmd}
2. nginx执行文件全路径为:"/usr/local/nginx/sbin/nginx" 若要使用nginx命令需要以 全路径方式
3. nginx 常用命令:
/usr/local/nginx/sbin/nginx -v
/usr/local/nginx/sbin/nginx -t 等
\033[0m"
/usr/local/nginx/sbin/nginx -v
}
main(){
nginx_install_app
nginx_make
nginx_system
}
main

结语fighting!