【Linux】nginx 离线安装

资源下载

nginx-1.20.1.tar.gz(下载链接中包含gcc、g++、pcre、libtool、nginx)

链接:https://pan.baidu.com/s/1Niv33uZa4gj0viYFrnBTBQ

提取码:0b91

安装步骤

安装gcc

1
rpm -Uvh *.rpm --nodeps --force

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@doris54 gcc]# rpm -Uvh *.rpm --nodeps --force
warning: cpp-4.8.5-16.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:zlib-1.2.7-17.el7 ################################# [ 6%]
2:mpfr-3.1.1-4.el7 ################################# [ 13%]
3:libmpc-1.0.1-3.el7 ################################# [ 19%]
4:openssl-libs-1:1.0.2k-8.el7 ################################# [ 25%]
5:cpp-4.8.5-16.el7 ################################# [ 31%]
6:zlib-devel-1.2.7-17.el7 ################################# [ 38%]
7:kernel-headers-3.10.0-693.el7 ################################# [ 44%]
8:glibc-headers-2.17-196.el7 ################################# [ 50%]
9:glibc-devel-2.17-196.el7 ################################# [ 56%]
10:gcc-4.8.5-16.el7 ################################# [ 63%]
11:openssl-devel-1:1.0.2k-8.el7 ################################# [ 69%]
12:openssl-1:1.0.2k-8.el7 ################################# [ 75%]
13:openssl098e-0.9.8e-29.el7.centos.################################# [ 81%]
Cleaning up / removing...
14:openssl-1:1.0.2k-19.el7 ################################# [ 88%]
15:openssl-libs-1:1.0.2k-19.el7 ################################# [ 94%]
16:zlib-1.2.7-18.el7 ################################# [100%]
[root@doris54 gcc]#

安装g++

1
rpm -Uvh *.rpm --nodeps --force

结果

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
[root@doris54 gcc-c++]# rpm -Uvh *.rpm --nodeps --force
warning: autogen-libopts-5.18-5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:pkgconfig-1:0.27.1-4.el7 ################################# [ 5%]
2:zlib-1.2.7-17.el7 ################################# [ 10%]
3:libcom_err-1.42.9-10.el7 ################################# [ 15%]
4:openssl-libs-1:1.0.2k-8.el7 ################################# [ 20%]
5:ntpdate-4.2.6p5-25.el7.centos.2 ################################# [ 25%]
6:zlib-devel-1.2.7-17.el7 ################################# [ 30%]
7:libsepol-devel-2.5-6.el7 ################################# [ 35%]
8:libselinux-devel-2.5-11.el7 ################################# [ 40%]
9:libverto-devel-0.2.5-4.el7 ################################# [ 45%]
10:libstdc++-devel-4.8.5-16.el7 ################################# [ 50%]
11:keyutils-libs-devel-1.5.8-3.el7 ################################# [ 55%]
12:krb5-devel-1.15.1-8.el7 ################################# [ 60%]
13:autogen-libopts-5.18-5.el7 ################################# [ 65%]
14:ntp-4.2.6p5-25.el7.centos.2 ################################# [ 70%]
15:openssl-devel-1:1.0.2k-8.el7 ################################# [ 75%]
16:gcc-c++-4.8.5-16.el7 ################################# [ 80%]
17:openssl-1:1.0.2k-8.el7 ################################# [ 85%]
18:openssl098e-0.9.8e-29.el7.centos.################################# [ 90%]
19:tcl-1:8.5.13-8.el7 ################################# [ 95%]
Cleaning up / removing...
20:libcom_err-1.42.9-17.el7 ################################# [100%]
[root@doris54 gcc-c++]#

验证gcc、g++是否安装成功,执行命令:

1
gcc -v

结果

1
2
3
4
5
6
7
8
9
[root@doris54 gcc-c++]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
[root@doris54 gcc-c++]#

安装PCRE,先把pcre解压出来,****tar -zxvf pcre-8.35.tar.gz,解压出来之后就要开始安装了**

1
2
3
4
5
6
7
cd pcre-8.35

./configure

make

make install

安装libtool,先解压libtool:tar -zxvf libtool-2.4.2.tar.gz,解压出来之后就要开始安装了

1
2
3
4
5
6
7
cd libtool-2.4.2

./configure

make

make install

安装nginx,先把nginx解压出来 tar -zxvf nginx-1.20.1.tar.gz,解压出来之后就要开始安装了**

1
2
3
4
5
6
7
cd nginx-1.20.1

./configure

make

make install

启动nginx: nginx安装目录地址 -c nginx配置文件地址 ,安装后的路径好像会被变到/usr/local里面

常用命令 操作
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 启动nginx
/usr/local/nginx/sbin/nginx -s stop 停止nginx
/usr/local/nginx/sbin/nginx -s reload 重启nginx
netstat -tunlp 查看端口占用
netstat -tunlp grep

nginx的配置文件为安装目录下的nginx目录中的nginx.conf,默认端口为80,启动后出现如下页面即为启动成功

开机自启

编写自启服务

1
vim /usr/lib/systemd/system/nginx.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=nginx
After=network.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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

设置nginx命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@node1 sbin]# vi  ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs
# 在原有的PATH 后加 :/usr/local/nginx/sbin
PATH={mathJaxContainer[0]}HOME/bin:/usr/local/nginx/sbin


export PATH
[root@node1 sbin]# source ~/.bash_profile
[root@node1 sbin]# nginx -v
nginx version: nginx/1.18.0

设置开机自启

1
2
3
4
5
6
root@node1 ~]# systemctl enable --now  nginx.service
[root@node1 ~]# ps -ef |grep nginx
root 879 1 0 16:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 881 879 0 16:17 ? 00:00:00 nginx: worker process
root 1738 1645 0 16:20 pts/0 00:00:00 grep --color=auto nginx
[root@node1 ~]#

启动命令

1
2
3
4
5
6
7
8
# nginx start
sudo systemctl start nginx
# nginx stop
sudo systemctl stop nginx
# nginx reload
sudo systemctl reload nginx
# nginx status
sudo systemctl status nginx