Nginx安装及部署

时间:2022-12-23 16:05:42

Nginx安装及部署

????博客主页: ​​微笑的段嘉许博客主页​

????欢迎关注????点赞????收藏⭐留言????

????本文由微笑的段嘉许原创!

????51CTO首发时间:????2022年12月日22????

✉️坚持和努力一定能换来诗与远方!

????作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!

本文介绍

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。


????理论讲解:

优点

Nginx 可以在大多数 Unix Linux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.20.0稳定版已经于2021年4月20日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license。
Nginx 是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性:
在连接高并发的情况下,Nginx是Apache服务不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

服务器

Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
处理静态文件,索引文件以及自动索引;打开文件描述符缓冲。
无缓存的反向代理加速,简单的负载均衡和容错。
FastCGI,简单的负载均衡和容错。
模块化的结构。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCG或其它代理服务器处理单页中存在的多个 SSI,则这项处理可以并行运行,而不需要相互等待。
支持 SSL 和 TLSSNI。 
代码
Nginx代码完全用C语言从头写成,已经移植到许多体系结构和操作系统,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows。Nginx有自己的函数库,并且除了zlib、PCRE和OpenSSL之外,标准模块只使用系统C库函数。而且,如果不需要或者考虑到潜在的授权冲突,可以不使用这些第三方库。

代理服务器

作为邮件代理服务:Nginx 同时也是一个非常优秀的邮件代理服务(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm​ 描述了成功并且美妙的使用经验。
Nginx 是一个安装非常的简单、配置文件非常简洁(还能够支持perl语法)、Bug非常少的服务。Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。


????实验配置与实现:

一、安装Nginx依赖

1)挂载系统光盘

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL

Nginx安装及部署

2)删除系统自带的yum仓库

[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*     //删除系统自带的yum仓库
[root@centos01 ~]# ls /etc/yum.repos.d/ //查看是否删除成功
local.repo

3)安装依赖程序

[root@centos01 ~]# yum -y install pcre-devel zlib-devel

4)创建管理Nginx用户

[root@centos01 ~]# useradd -M -s /sbin/nologin nginx

5)配置DNS

[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
---
TYPE=Ethernet
BOOTPROTO=static
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
DNS1=192.168.100.20 //DNS指向DNS服务器

6)重新启动网卡服务

[root@centos01 ~]# systemctl restart network        //重新启动网卡
[root@centos01 ~]# cat /etc/resolv.conf //查看DNS是否配置成功
# Generated by NetworkManager
nameserver 192.168.100.20

二、安装配置Nginx

1)卸载系统光盘、挂载云计算光盘

[root@centos01 ~]# umount /mnt/         //卸载系统光盘
[root@centos01 ~]# ls /mnt/ //查看是否卸载成功
[root@centos01 ~]# mount /dev/cdrom /mnt/ //挂载云计算光盘
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/ //查看是否挂载成功
amoeba-mysql-binary-2.2.0.tar.gz nagios-plugins-1.5.tar.gz
apache-tomcat-7.0.54.tar.gz nginx-1.6.0.tar.gz
awstats-7.3.tar.gz nrpe-2.15.tar.gz
cacti-0.8.8b.tar.gz OpenStack.rar
cmake-2.8.6.tar.gz Open.rar
cronolog-1.6.2.tar.gz percona-monitoring-plugins-1.1.4.tar.gz
Discuz_7_SC_UTF8.zip php-5.3.28.tar.gz
Discuz_X3.2_SC_UTF8.zip phpMyAdmin-4.2.5-all-languages.tar.gz
facter-1.7.1.tar.gz postfix+dovecot.rar
fetion20091117-linux.tar.gz puppet-2.7.21.tar.gz
fuse-2.9.2.tar.gz rrdtool-1.4.8.tar.gz
hadoop-2.4.0.tar.gz ruby-1.8.7.352-13.el6.x86_64.rpm
haproxy-1.4.24.tar.gz ruby-irb-1.8.7.352-13.el6.x86_64.rpm
httpd-2.2.17.tar.gz ruby-libs-1.8.7.352-13.el6.x86_64.rpm
inotify-tools-3.14.tar.gz ruby-rdoc-1.8.7.352-13.el6.x86_64.rpm
jdk-6u14-linux-x64.bin sarg-2.3.7.tar.gz
jdk-7u65-linux-x64.gz sendEmail-v1.55.tar.gz
john-1.8.0.tar.gz SKYUC.v3.4.2.SOURCE.zip
keepalived-1.2.13.tar.gz squid-3.4.6.tar.gz
library_linux.tar.gz Xmanager_Enterpris.3987.rar
mfs-1.6.27-5.tar.gz Xshell4_4.0.0.112.exe
mysql-5.5.22.tar.gz ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
nagios-4.0.1.tar.gz

Nginx安装及部署

2)解压Nginx

[root@centos01 ~]# tar zxf /mnt/nginx-1.6.0.tar.gz -C /usr/src/       //解压nginx到/usr/src/目录
[root@centos01 ~]# cd /usr/src/nginx-1.6.0/ //切换到nginx程序目录
[root@centos01 nginx-1.6.0]#

3)配置Nginx

[root@centos01 nginx-1.6.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

4)编译安装Nginx

[root@centos01 nginx-1.6.0]# make && make install

5)优化命令

[root@centos01 nginx-1.6.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos01 nginx-1.6.0]# cd

6)检查配置文件完整性

[root@centos01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

三、Nginx服务控制

1)启动服务

[root@centos01 ~]# nginx

2)监听服务运行状态

[root@centos01 ~]# netstat -anptu | grep nginx 
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3913/nginx: master

3)停止Nginx服务

[root@centos01 ~]# killall -s QUIT nginx
[root@centos01 ~]# netstat -anptu | grep nginx

4)重新启动服务

[root@centos01 ~]# killall -s HUP nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3955/nginx: master

四、虚拟主机网站根目录

1)创建网站根目录

[root@centos01 ~]# mkdir -p /www/benetcom
[root@centos01 ~]# mkdir -p /www/accpcom

2)使用WinSCP软件上传网站源代码设置网站主页

Nginx安装及部署

Nginx安装及部署

Nginx安装及部署

五、修改Ngixn主配置文件虚拟主机配置

1)修改Nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf         
---
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.com.access.log;
location / {
root /www/benetcom;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log;
location / {
root /www/accpcom;
index index.html index.htm;
}
}
}

删除以#开头的行

Nginx安装及部署

删除空行

Nginx安装及部署

2)检查主配置文件是否错误

[root@centos01 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

3)重启Nginx服务

root@centos01 ~]# killall nginx
[root@centos01 ~]# nginx

六、搭建DNS服务器

1)挂载系统光盘

[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# ls /mnt/
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL

2)安装DNS程序包

[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm 
警告:/mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:bind-32:9.9.4-50.el7 ################################# [100%]
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-utils-9.9.4-50.el7.x86_64.rpm
警告:/mnt/Packages/bind-utils-9.9.4-50.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
软件包 bind-utils-32:9.9.4-50.el7.x86_64 已经安装

2)设置开机自动启动

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named

七、配置DNS主配置文件和区域配置文件

1)备份主配置文件防止修改错误源文件丢失

[root@centos02 ~]# cp /etc/named.conf /etc/named.conf.bak
[root@centos02 ~]# ls /etc/named.conf.bak
/etc/named.conf.bak

2)清空源配置文件的内容手动配置

[root@centos02 ~]# echo "" > /etc/named.conf
[root@centos02 ~]# cat /etc/named.conf

3)编辑主配置文件

[root@centos02 ~]# vim /etc/named.conf
---
options{
listen-on port 53 { 192.168.100.20; };
directory "/var/named/";
};
zone "benet.com" IN {
type master;
file "/var/named/benet.com.zone";
};
zone "accp.com" IN {
type master;
file "/var/named/accp.com.zone";
};

4)检查配置文件语法结构是否错误

[root@centos02 ~]# named-checkconf /etc/named.conf

5)编辑区域配置文件

[root@centos02 ~]# vim /var/named/benet.com.zone
---
$TTL 86400
@ SOA benet.com. root.benet.com. (
2022122210
1H
15M
1W
1D
)
@ NS centos02.benet.com.
centos02 A 192.168.100.20
www A 192.168.100.10

6)检查区域配置文件是否错误

[root@centos02 ~]# named-checkzone benet.com /var/named/benet.com.zone
zone benet.com/IN: loaded serial 2022122210
OK

7)编辑accp区域配置文件

[root@centos02 ~]# cp /var/named/benet.com.zone /var/named/accp.com.zone
[root@centos02 ~]# vim /var/named/accp.com.zone
:%s/benet.com/accp.com/g //进入accp区域配置文件里面在末行模式输入命令把benet.com替换位accp.com

8)检查accp区域配置文件是否错误

[root@centos02 ~]# named-checkzone accp.com /var/named/accp.com.zone 
zone accp.com/IN: loaded serial 2022122210
OK

9)配置DNS

[root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
---
TYPE=Ethernet
BOOTPROTO=static
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.20
NETMASK=255.255.255.0
DNS1=192.168.100.20 //DNS指向自己的IP地址

10)重新启动网卡服务

[root@centos02 ~]# systemctl restart network        //重新启动网卡服务
[root@centos02 ~]# cat /etc/resolv.conf //查看DNS是否配置成功
# Generated by NetworkManager
nameserver 192.168.100.20

11)启动DNS服务

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# netstat -anptu | grep named
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 2802/named
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN 2802/named
tcp6 0 0 ::1:53 :::* LISTEN 2802/named
tcp6 0 0 ::1:953 :::* LISTEN 2802/named
udp 0 0 127.0.0.1:53 0.0.0.0:* 2802/named
udp6 0 0 ::1:53 :::* 2802/named

12)查看是否能够成功解析

[root@centos02 ~]# nslookup www.accp.com
Server: 192.168.100.20
Address: 192.168.100.20#53

Name: www.accp.com
Address: 192.168.100.20

[root@centos02 ~]# nslookup www.benet.com
Server: 192.168.100.20
Address: 192.168.100.20#53

Name: www.benet.com
Address: 192.168.100.20

八、验证

1)使用Linux验证配置IP地址和DNS

[root@centos03 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
---
TYPE=Ethernet
BOOTPROTO=static
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.30
NETMASK=255.255.255.0
DNS1=192.168.100.20 //DNS指向DNS服务器

2)重新启动网卡服务

[root@centos03 ~]# systemctl restart network        //重新启动网卡服务
[root@centos03 ~]# cat /etc/resolv.conf //查看DNS是否配置成功
# Generated by NetworkManager
nameserver 192.168.100.20

3)切换到图形界面

[root@centos03 ~]# init 5

4)验证

访问www.benet.com

Nginx安装及部署

访问www.accp.com

Nginx安装及部署


????作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!

Nginx安装及部署