资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Centos7部署Nginx网站服务-创新互联

一、Nginx服务基础

Nginx专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力(单台物理服务器可支持30000~50000个并发请求)。正因为如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择Nginx来提供Web服务。

创新互联从2013年成立,先为湖北等服务建站,湖北等地企业,进行企业商务咨询服务。为湖北企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

1、Nginx服务的优势

Nginx是一个很牛的高性能Web和反向代理服务器,它具有有很多非常优越的特性:

  • 高并发连接:官方测试能支撑5万并发连接,在实际生产环境中跑到2,~3W并发连;
  • 内存消耗少:在3W并发连接下,开启的10个NGINX进程才消耗150M内存(15M*10=150M);
  • 配置文件非常简单:风格跟程序一样通俗易懂;
  • 成本低廉:Nginx作为开源软件,可以免费使用,而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则需要十多万至几十万人民币;
  • 支持rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分发到不同的后端服务器群组;
  • 内置的健康检查功能:如果Nginx Proxy后端的后台web服务器宕机了,不会影响前端访问;
  • 节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头;
  • 稳定性高:用于反向代理,宕机的概率微乎其微;

二、安装部署Nginx

1、前提准备

Nginx最新的稳定版本为1.12.0,其安装文件可以从官方网站Nginx官方网站/下载。

1)Centos 7服务器一台;
2)Windows客户端一台:
3)Centos 7操作系统镜像;
4)Nginx镜像;
安装Nginx用到的所有镜像及软件包可以访问网盘提取:https://pan.baidu.com/s/18iRCuiMEyGbEFSeBp17uVQ
提取码:qszt

2、开始安装部署Nginx服务器

1)挂载Linux光盘,拷贝nginx依赖程序到/usr/src/目录

Centos 7部署Nginx网站服务

[root@centos02 ~]# mount /dev/cdrom /mnt/    
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# cp /mnt/nginx-1.6.0.tar.gz /usr/src/ 

2)切换LAMP光盘,将mnt目录下所有数据拷贝到/usr/src/目录

[root@centos02 ~]# umount /mnt/ <!--卸载光盘-->
Centos 7部署Nginx网站服务

[root@centos02 ~]# mount /dev/cdrom /mnt/   
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# cp /mnt/* /usr/src/ 

3)切换到操作系统光盘,安装nginx依赖程序

[root@centos02 ~]# umount /mnt/ <!--卸载光盘-->
Centos 7部署Nginx网站服务

[root@centos02 ~]# mount /dev/cdrom /mnt/   
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-* 
[root@centos02 ~]# yum -y install pcre-devel zlib-devel 
[root@centos02 ~]# useradd -M -s /sbin/nologin nginx 
[root@centos02 ~]# tar zxvf /usr/src/nginx-1.6.0.tar.gz -C /usr/src/ 

[root@centos02 ~]# cd /usr/src/nginx-1.6.0/ 
[root@centos02 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx
--user=nginx --group=nginx --with-http_stub_status_module

[root@centos02 nginx-1.6.0]# make && make install  
[root@centos02 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ 

3、Nginx服务的基本管理

[root@centos02 ~]# nginx    
[root@centos02 ~]# netstat -anptu | grep nginx 
                     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4663/nginx: master  
[root@centos02 ~]# killall -s QUIT nginx   
[root@centos02 ~]# killall -3 nginx       
[root@centos02 ~]# killall -1 nginx     
[root@centos02 ~]# killall -s HUP nginx   
[root@centos02 ~]# vim /etc/init.d/nginx 
#!/bin/bash
#chkconfig: 35 90 30
#description:nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill 0s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0 (start|stop|restart|reload)"
exit 1
esac
exit 0
[root@centos02 ~]# chmod +x /etc/init.d/nginx 
[root@centos02 ~]# chkconfig --add nginx 
[root@centos02 ~]# chkconfig --level 35 nginx on 
[root@centos02 ~]# /etc/init.d/nginx stop
[root@centos02 ~]# /etc/init.d/nginx start
[root@centos02 ~]# /etc/init.d/nginx restart

客户端配置和Nginx服务器同一块网卡同网段,设置网关即可访问Nginx网站服务器
Centos 7部署Nginx网站服务

4、Nginx常见的配置文件

[root@centos02 ~]# ls -ld /usr/local/nginx/conf/nginx.conf
-rw-r--r-- 1 root root 2656 11月 28 17:22
/usr/local/nginx/conf/nginx.conf       
[root@centos02 ~]# ls -ld /usr/local/nginx/sbin/
drwxr-xr-x 2 root root 19 11月 28 17:22
/usr/local/nginx/sbin/      
[root@centos02 ~]# ls -ld /usr/local/nginx/html/
drwxr-xr-x 2 root root 40 11月 28 17:22
/usr/local/nginx/html/      
[root@centos02 ~]# ls -ld /usr/local/nginx/logs/
drwxr-xr-x 2 root root 58 11月 28 17:47 
/usr/local/nginx/logs/   

5、修改Nginx主配置文件

[root@centos02 ~]# cp /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.bak   
[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf 
                            
  3 user  nginx;       
  4 worker_processes  1;     
  6 error_log  logs/error.log;   
12 pid        logs/nginx.pid;     
16     use epoll;        
17     worker_connections  1024; 
29     #access_log  logs/access.log  main; 
31     sendfile        on;           
35     keepalive_timeout  65;       
39     server { 
40         listen       80;     
41         server_name  localhost; 
44         charset utf-8;    
48         location / {     
49             root   html; 
50             index  index.html index.html;
51         }
84     }            
  • listen:限定端口的同时允许限定IP地址,采用“IP地址:端口号”形式,root语句用来设置特定访问位置的网页文档路径,默认为Nginx安装目录下的html/目录,根据需要可改为/var/www/html等其他路径,但更改后需保证nginx用户对其具有读取权限。

  • worker_processes :表示工作进程的数量,若服务器由多块CPU或者使用多核处理器,可以参考CPU核心总数来指定工作进程数。具体含义在worker_connections配置项中体现出来。

  • worker_connections:这个配置项指定的是每个进程处理的连接,一般在10000以下(默认为1024),与上面工作进程数量的配置项关联,例如:若工作进程数为8,每个进程处理4096个连接,则允许Nginx正常提供服务的连接数已经超过了3万个(4096*8=32768)。当然,具体还要看服务器硬件、网络带宽等物理条件的性能表现。

6、配置Nginx的访问状态统计

[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf 
 
 52         location /status {
 53                 stub_status     on;
 54                 access_log off;
 55         }
[root@centos02 ~]# /etc/init.d/nginx restart

客户端访问状态统计页:
Centos 7部署Nginx网站服务

  • Active connections:表示当前的活动连接数;

  • server accepts handled requests:表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、已经处理的请求数。

三、配置虚拟主机

1、安装DNS服务器

[root@centos02 ~]# yum -y install bind bind-chroot bind-utils 
                              
[root@centos02 ~]# echo "" > /etc/named.conf 
           
[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    "benet.com.zone";
}
zone    "accp.com"      IN      {
        type    master;
        file    "accp.com.zone";
}
[root@centos02 ~]# named-checkconf -z /etc/named.conf
              
[root@centos02 ~]# vim /var/named/benet.com.zone  
               
$TTL    86400
@       SOA     benet.com.      root.benet.com(
        2019112801
        1H
        15M
        1W
        1D
)
@       NS      centos02.benet.com.
centos02 A      192.168.100.20
www      A      192.168.100.20
[root@centos02 ~]# chmod +x /var/named/benet.com.zone  
                    
[root@centos02 ~]# chown named:named
/var/named/benet.com.zone 
[root@centos02 ~]# named-checkzone benet.com
/var/named/benet.com.zone   
         
zone benet.com/IN: loaded serial 2019112801
OK
[root@centos02 ~]# cp /var/named/benet.com.zone
/var/named/accp.com.zone  
   
[root@centos02 ~]# vim /var/named/accp.com.zone  
                    
$TTL    86400
@       SOA     accp.com.       root.accp.com(
        2019112801
        1H
        15M
        1W
        1D
)
@       NS      centos02.accp.com.
centos02 A      192.168.100.20
www      A      192.168.100.20
[root@centos02 ~]# named-checkzone accp.com
/var/named/accp.com.zone  
           
[root@centos02 ~]# vim /etc/sysconfig/network-scripts/
ifcfg-ens32  
DNS1=192.168.100.20   
[root@centos02 ~]# systemctl restart network
[root@centos02 ~]# systemctl start named 
[root@centos02 ~]# systemctl enable named
[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

[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

2、配置基于域名的虚拟主机

Nginx的配置文件使用“http { }”界定标记用于设定HTTP服务器,包括访问日志、http端口、网页目录、默认字符集、连接保持,以及虚拟web主机、php解析等网站全局设置,其中大部分包含在子界定标记 “ server { }”内。“ server { }”代表一个具体的网站设置。

             
[root@centos02 ~]# mkdir -p /var/www/benetcom  
[root@centos02 ~]# mkdir -p /var/www/accpcom  
               
[root@centos02 ~]# echo "www.benet.com" > 
/var/www/benetcom/index.html
[root@centos02 ~]# echo "www.accp.com" > 
/var/www/accpcom/index.html
[root@centos02 ~]# vim /usr/local/nginx/conf/nginx.conf  
                     
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
        server  {           
                listen www.benet.com:80;  
                server_name www.benet.com;     
                charset utf-8;          
                access_log      logs/www.benet.com.access.log; 
                                                    
                error_log       logs/www.benet.com.error.log;
                                                       
                location / {       
                                root /var/www/benetcom/; 
                                index index.html;       
                        }
                }       


        server  {
                listen www.accp.com:80;
                server_name www.accp.com;
                charset utf-8;
                access_log      logs/www.accp.com.access.log;
                error_log       logs/www.accp.com.error.log;
                location / {
                                root /var/www/accpcom/;
                                index index.html;
                        }
                }
[root@centos02 ~]# 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
[root@centos02 ~]# systemctl restart named
[root@centos02 ~]# /etc/init.d/nginx restart

客户端添加DNS地址,访问域名测试是否成功
Centos 7部署Nginx网站服务

Centos 7部署Nginx网站服务

—————— 本文至此结束,感谢阅读 ——————

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:Centos7部署Nginx网站服务-创新互联
链接URL:http://cdkjz.cn/article/hjhgg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220