nginx安装

1 安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 进入到home文件夹
cd /home
# 安装必要的依赖
yum install gcc pcre-devel zlib-devel openssl-devel
# 下载nginx
wget https://nginx.org/download/nginx-1.24.0.tar.gz
# 解压
tar -zxvf nginx-1.24.0.tar.gz
# 进入目录
cd /home/nginx-1.24.0
# 配置安装目录
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# 进行安装
make && make install

2 将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
# 新建文件 /usr/lib/systemd/system/nginx.service
vim /usr/lib/systemd/system/nginx.service
# 内容如下
[Unit]
Description=The nginx web and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

# 修改权限
chmod 755 /usr/lib/systemd/system/nginx.service
# 刷新系统服务
systemctl daemon-reload
# 设置开机启动
systemctl enable --now nginx

3 自动安装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
#!/bin/bash

#新建用户,用来启动nginx
useradd -s /sbin/nologin nginx
#解决依赖关系,以及安装常用工具
yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ automake autoconf make

#下载nginx压缩包
mkdir -p /nginx
cd /nginx
curl -O http://nginx.org/download/nginx-1.24.0.tar.gz

#解压文件
tar xf nginx-1.24.0.tar.gz
cd nginx-1.24.0

#编译前的配置工作,根据实际需要选择常用模块
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
--http-log-path=/usr/local/nginx/logs/access.log --error-log-path=/usr/local/nginx/logs/error.log \
--with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module \
--with-stream --with-stream_realip_module --with-http_gzip_static_module --with-file-aio --with-threads

#允许多个编译命令同时执行,可根据CPU核心数适量增加(linux环境下nproc命令可查看)
make -j 12

#编译安装
make install

#添加到系统服务,设置开机自启
echo '[Unit]
Description=The nginx web and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target' > /usr/lib/systemd/system/nginx.service

chmod 755 /usr/lib/systemd/system/nginx.service

#将nginx配置到系统环境变量中
echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> /etc/profile

#nginx配置文件高亮显示
mkdir -p ~/.vim/syntax
cd ~/.vim/syntax
wget -O nginx.vim https://www.vim.org/scripts/download_script.php?src_id=19394
echo "au BufRead,BufNewFile /usr/local/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif " >> ~/.vim/filetype.vim

systemctl daemon-reload
systemctl enable --now nginx
systemctl status nginx