加油,新时代打工人!
官方下载地址http://nginx.org/en/download.html
Mainline version :开发版本,建议用着个人配置
Stable version:稳定版本,上线用
Legacy versions: 旧版本
本教程版本 nginx 1.21.4
1. 在Linux下载以下环境
(1)需要安装 gcc 的环境
gcc
yum install gcc-c++
(2)第三方的开发包。
PCRE
PCRE(Perl Compatible Regular Expressions)是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。
yum install -y pcre pcre-devel
注:pcre-devel 是使用 pcre 开发的一个二次开发库。nginx 也需要此库。
zlib
zlib 库提供了很多种压缩和解压缩的方式,nginx 使用 zlib 对 http 包的内容进行 gzip,所以需要在 linux 上安装 zlib 库。
yum install -y zlib zlib-devel
OpenSSL
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持
将下载的压缩包上传到虚拟机
http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在 linux安装 openssl 库。
yum install -y openssl openssl-devel
2. 在虚拟机解压nginx
下载需要的nginx压缩包 上传到虚拟中解压
tar -zxvf nginx-1.21.4 -C /opt
进入nginx安装路径
[root@slave2 sbin]# cd /opt
[root@slave2 opt]# ls
apache-tomcat-8.0.36 apache-zookeeper-3.6.3 hadoop jdk kafka_2.12-2.8.0 nginx-1.21.4 packet
[root@slave2 opt]# cd nginx-1.21.4/
[root@slave2 nginx-1.21.4]# ll
总用量 804
drwxr-xr-x. 6 1001 1001 4096 12月 6 12:10 auto
-rw-r--r--. 1 1001 1001 316102 11月 2 22:49 CHANGES
-rw-r--r--. 1 1001 1001 482916 11月 2 22:49 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 12月 6 12:10 conf
-rwxr-xr-x. 1 1001 1001 2590 11月 2 22:49 configure
drwxr-xr-x. 4 1001 1001 72 12月 6 12:10 contrib
drwxr-xr-x. 2 1001 1001 40 12月 6 12:10 html
-rw-r--r--. 1 1001 1001 1397 11月 2 22:49 LICENSE
-rw-r--r--. 1 root root 438 12月 6 12:19 Makefile
drwxr-xr-x. 2 1001 1001 21 12月 6 12:10 man
drwxr-xr-x. 3 root root 174 12月 6 12:20 objs
-rw-r--r--. 1 1001 1001 49 11月 2 22:49 README
drwxr-xr-x. 9 1001 1001 91 12月 6 12:10 src
使用 ./configure 检查配置
然后进行编译和安装
make && make install
如果进入 usr/local目录下没有自动生成nginx文件夹,需要在usr/local中创建mkdir nginx文件夹,然后重复-》到nginx解压包下使用 ./configure 检查配置
然后进行编译和安装
这个时候回到我们 usr/local/nginx就可以看到
[root@slave2 opt]# cd /usr/local/nginx/
[root@slave2 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
然后我们到sbin/.nginx启动
[root@slave2 nginx]# cd sbin
[root@slave2 sbin]# ls
nginx
3. 运行截图
我们到自己浏览器输入虚拟机的ip 进行访问,默认端口是80
注意:
本机浏览器访问前查看防火墙是否关闭
centos7 默认是使用firewalld作为防火墙,可以选择使用以下命令进行关闭
- 查看防火墙状态
systemctl status firewalld
service iptables status
- 暂时关闭防火墙(这个重启后防火墙还会自动打开)
systemctl stop firewalld
- 永久关闭防火墙(系统重启后 防火墙自动关闭)
systemctl disable firewalld
- 重启防火墙
systemctl enable firewalld
开端口命令:
打开单个端口:firewall-cmd --zone=public --add-port=80/tcp --permanent
打开多个端口:firewall-cmd --zone=public --add-port=20000-29999/tcp --permanent
–permanent 为永久生效,不加为单次有效(重启失效)
- 查看开启端口:
netstat -ntlp 或:firewall-cmd --list-ports
- 关闭端口命令:
firewall-cmd --zone=public --remove-port=80/tcp --permanent
- 查看端口是否打开
firewall-cmd --zone=public --query-port=80/tcp
4. 常用nginx命令
进入到/usr/local/nginx/sbin
[root@slave2 sbin]# pwd
/usr/local/nginx/sbin
- 查看nginx版本号
命令
./nginx -v
演示
[root@slave2 sbin]# ./nginx -v
nginx version: nginx/1.21.4
- 查看nginx 状态
命令
ps -ef | grep nginx
演示
[root@slave2 sbin]# ps -ef | grep nginx
root 11786 1 0 12:20 ? 00:00:00 nginx: master process ./nginx
nobody 11787 11786 0 12:20 ? 00:00:00 nginx: worker process
root 11829 2610 0 13:05 pts/0 00:00:00 grep --color=auto nginx
- 开启nginx
命令
./nginx
演示
[root@slave2 sbin]# ./nginx
[root@slave2 sbin]# ps -ef | grep nginx
root 11834 1 0 13:09 ? 00:00:00 nginx: master process ./nginx
nobody 11835 11834 0 13:09 ? 00:00:00 nginx: worker process
root 11837 2610 0 13:09 pts/0 00:00:00 grep --color=auto nginx
- 关闭nginx
命令
./nginx -s stop
演示
[root@slave2 sbin]# ./nginx -s stop
[root@slave2 sbin]# ps -ef | grep nginx
root 11832 2610 0 13:08 pts/0 00:00:00 grep --color=auto nginx
- 重新加载
当我们修改在conf 修改 nginx 配置文件后,不会立即生效,我们使用重新加载,不需要重启nginx 就可以生效了
命令
./nginx -s reload