Centos7一键安装nginx
# 添加脚本
vi /home/install-nginx.sh
1
#!/bin/bash
# nginx简易安装脚本,版本以及nginx安装模块可修改。
echo "start"
# pcre_version="8.36"
# openssl_version="1.0.1j"
# zlib_version="1.2.11"
# nginx_version="1.8.0"
pcre_version="8.36"
openssl_version="1.1.1"
zlib_version="1.2.12"
nginx_version="1.20.2"
echo "安装:gcc gcc-c++"
yum install -y gcc gcc-c++
echo "进入目录:/usr/local/"
cd /usr/local/
echo ""
echo "下载:pcre-"$pcre_version""
pcre_url="http://jaist.dl.sourceforge.net/project/pcre/pcre/"$pcre_version"/pcre-"$pcre_version".tar.gz"
wget $pcre_url
echo "解压:pcre-"$pcre_version".tar.gz"
tar -zxvf pcre-"$pcre_version".tar.gz
echo "进入目录:/usr/local/pcre-"$pcre_version""
cd pcre-"$pcre_version"
echo "编译安装:pcre-"$pcre_version""
./configure
make && make install
echo "返回到目录:/usr/local/"
cd /usr/local/
echo ""
echo "下载:openssl-"$openssl_version""
openssl_url="http://www.openssl.org/source/openssl-"$openssl_version".tar.gz"
wget $openssl_url
echo "解压:openssl-"$openssl_version".tar.gz"
tar -zxvf openssl-"$openssl_version".tar.gz
echo "进入目录:openssl-"$openssl_version""
cd openssl-"$openssl_version"
echo "编译安装:openssl-"$openssl_version""
./config
make && make install
echo "返回到目录:/usr/local/"
cd /usr/local/
echo ""
echo "下载:zlib-"$zlib_version""
zlib_url="http://zlib.net/zlib-"$zlib_version".tar.gz"
wget $zlib_url
echo "解压:zlib-"$zlib_version".tar.gz"
tar -zxvf zlib-"$zlib_version".tar.gz
echo "进入目录:zlib-"$zlib_version""
cd zlib-"$zlib_version"
echo "编译安装:zlib-"$zlib_version""
./configure
make && make install
echo "返回到目录:/usr/local/"
cd /usr/local/
echo ""
echo "下载:nginx-"$nginx_version""
nginx_url="http://nginx.org/download/nginx-"$nginx_version".tar.gz"
wget $nginx_url
echo "解压:nginx-"$nginx_version".tar.gz"
tar -zxvf nginx-"$nginx_version".tar.gz
echo "重命名nginx-"$nginx_version"为nginx"
mv nginx-"$nginx_version" nginx
echo "进入目录:nginx"
cd nginx
echo "编译安装:nginx-"$nginx_version""
./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-pcre=/usr/local/pcre-"$pcre_version" --with-zlib=/usr/local/zlib-"$zlib_version" --with-openssl=/usr/local/openssl-"$openssl_version"
make && make install
echo "创建目录:/usr/local/nginx/logs"
mkdir logs
echo "返回到目录:/usr/local/"
cd /usr/local/
echo ""
read -p "是否需要删除下载的安装(输入y/Y删除,其他不删除):" inputMsg
if [ "$inputMsg" == 'y' ] || [ "$inputMsg" == 'Y' ]
then
rm -rf nginx-"$nginx_version".tar.gz pcre-"$pcre_version".tar.gz openssl-"$openssl_version".tar.gz zlib-"$zlib_version".tar.gz
echo "删除完成"
else
echo "不删除"
fi
echo ""
read -p "是否需要启动nginx(输入y/Y启动,其他不启动):" startNginxMsg
if [ "$startNginxMsg" == 'y' ] || [ "$startNginxMsg" == 'Y' ]
then
/usr/local/nginx/sbin/nginx
if [ $? -eq 0 ]
# 获取本机ip,需要根据实际修改主机ip域。两端需要加反引号以获取命令返回信息。如果不设置ip域则可能会取出包含127.0.0.1等多个地址信息
then
localIp=`/sbin/ifconfig -a|grep inet|grep 172.17.*.*|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
echo "启动成功,请访问: http://$localIp"
else
echo "启动失败,请查看异常信息确定失败原因"
fi
else
echo "不启动"
fi
echo ""
echo "版本信息:"
echo "pcre:"$pcre_version
echo "openssl:"$openssl_version
echo "zlib:"$zlib_version
echo "nginx:"$nginx_version
echo ""
echo "安装路径: /usr/local/"
echo "end"
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
完成后保存退出
# 相关依赖包版本可以根据实际修改,修改如下位置即可:
pcre_version="8.36"
openssl_version="1.1.1"
zlib_version="1.2.11"
nginx_version="1.14.0"
1
2
3
4
2
3
4
# 赋予文件执行权限
chmod +x /home/install-nginx.sh
1
执行
/home/install-nginx.sh
1
等待完成
上次更新: 2022/06/22, 17:26:08