Web服务:Nginx与LNMP架构部署及应用

一、Nginx服务基础
●Nginx安装
●Nginx配置
●访问状态统计
二、Nginx访问控制
●基于授权的访问控制
●基于客户端的访问控制
三、Nginx虚拟主机
●基于ip的虚拟主机
●基于端口的虚拟主机
四、LNMP架构部署及应用
●MySQL安装配置
●PHP安装配置
●配置Nginx支持PHP环境

关于Nginx
●一款高性能轻量级Web服务软件
(1)稳定性高
(2)系统资源消耗低
(3)对HTTP并发连接的处理能力高(单台物理服务器可支持30 000~50 000个并发请求)

1.1手工编译安装Nginx

1、安装支持软件
yum -y install gcc gcc-c++ pcre-devel zlib-devel (安装这些软件的开发包devel)
2、创建运行用户、组
useradd -M -s/sbin/nologin nginx
3、编译安装Nginx
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@localhost ~]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \           (指定安装路径)
--user=nginx \                        (指定用户管理)
--group=nginx \                        (指定组)
--with-http_stub_status_module          (开启这个模块以支持状态统计)
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost nginx-1.12.2]# 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

4、添加Nginx系统服务
方式一:
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20	                        # chkcofig - “-” 表示不启用开机启动管理 (同时 若不加“#”, chkconfig add nginx 会加载不到配置)
# description: Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"				#命令程序文件位置(nginx)
PID="/usr/local/nginx/logs/nginx.pid"			#pid文件
case "$1" in
start)
   $COM
   ;;
stop)
   kill -s QUIT $(cat $PID)
   ;;
restart)
   $0 stop
   $0 start
   ;;
reload)
   kill -s HUP $(cat $PID)             #根据进程号重载配置
   ;;
*)
       echo "Usage: $0 {start|stop|restart|reload}"
       exit 1
esac
exit 0
保存退出
[root@localhost ~]# chmod +x /etc/init.d/nginx    添加权限
[root@localhost init.d]# chkconfig --add nginx    添加为系统服务
方式二:
vim /usr/lib/systemd/system/nginx.service
[Unit]	
Description=nginx							#描述
After=network.target						#描述服务类别
[Service]
Type=forking								#后台运行类型
PIDFile =/usr/local/nginx/logs/nginx.pid	#PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx		#启动服务
ExecrReload=/bin/kill -s HUP $MAINPID		#根据PID重载配置
ExecrStop=/bin/kill -s QUIT $MAINPID		#根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target					#启动级别

chmod 754 /lib/systemd/system/nginx.service		#设置754权限是一种安全优化
退出保存
systemctl start nginx.service
systemctl enable nginx.service

1.2、Nginx配置文件

[root@localhost init.d]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@localhost init.d]# cd /usr/local/nginx/conf
打开配置文件
[root@localhost conf]# vim nginx.conf

#user  nobody;                      #默认管理nginx的用户
worker_processes  1;            #工作进程运行数量,可配置成服务器内核数*2,如果网站访问量不大,一般设为1

#error_log  logs/error.log;        #错误日志文件路径/级别
#error_log  logs/error.log  notice;  
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;        #pid文件位置


events {
    worker_connections  1024;      #每个进程最多连接数量(如提高每个进程的连接数还需执行"ulimit -n 65535"命令临时修改本地每个进程可以同时打开的最大文件数为65535}


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;      #访问日志位置

    sendfile        on;                     #支持文件发送(下载)
    #tcp_nopush     on;  #此项允许或禁止使用socket的TcP_cORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
    keepalive_timeout  0;   #连接保持超时时间,单位:秒
    keepalive_timeout  65;  

    #gzip  on;             #压缩模块on表示开启

    server {                #web服务的监听配置
        listen       80;    #默认监听端口
        **server_name  localhost;  #站点域名,需要修改www . ky11.com**

        **#charset koi8-r;    #字符集支持(修改为中文)UTF-8**
        #access_log  logs/host.access.log  main;    #此web服务的主访问日志

        location / {           #"/”根目录配置
            root   html;      #网站根目录的位置/usr/ local/nginx/html(相对路径)
            index  index.html index.htm;    #支持的首页文件格式

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html; #当发生错误的时候能够显示一个预定义的错误页面

        location = /50x.html {          #错误页面配置
            root   html;
        }

# proxy(代理) the PHP scripts to Apache listening on 127.0.0.1:80   #以下是支持PHP及跳转的配置

开始配置本地映射
[root@localhost conf]# echo "192.168.153.220 www.ky11.com" >> /etc/hosts
[root@localhost conf]# service nginx restart

报错处理
在这里插入图片描述

在这里插入图片描述

检查、启用、添加系统管理
nginx -t   检查配置文件是否正确
nginx      启动
kill -s OUIT <PID号>   退出
kill -s HUP <PID号>    重载

ulimit -a   查看系统允许当前用户进程打开的文件数限制
watch -n 1  间隔一秒刷新一次结果
systemctl status nginx network     查看多个

3.1、访问状态统计
(1)、先使用命令/usr/local/nginx/sbin/nginx- V查看已安装的Nginx是否包含HTTP_STUB_STATUS模块
(2)、修改nginx.conf 配置文件,指定访问位置并添加 stub status配置

[root@localhost conf]# nginx -V   #查看已安装的Nginx是否包含HTTP_STUB_STATUS模块         
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost ~]# cd /usr/local/nginx/conf
打开配置文件vim nginx.conf
添加stub_status配置代码
    server {
        listen       80;
        server_name  www.ky11.com;

        #charset UTF-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        **location /status {         #访问位置为 /status
            stub_status on;        #打开状态统计功能
            access_log off;        #关闭此位置的日志记录**
}
保存退出
[root@localhost conf]# 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@localhost conf]# systemctl restart nginx
[root@localhost conf]# netstat -natp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      16964/nginx: master 
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      8809/sshd: root@pts 
tcp        0      0 192.168.153.220:22      192.168.153.1:62578     ESTABLISHED 8809/sshd: root@pts 
tcp6       0      0 ::1:6010                :::*                    LISTEN      8809/sshd: root@pts 

重启一下服务
在这里插入图片描述

4.1、访问控制
基于授权的访问控制
htpasswd : htpasswd是一个用于目录访问权限认证的一个工具。
-c:创建密码文件,如果文件存在,那么内容被清空重写

1、生成用户密码认证文件
[root@localhost conf]# yum -y install httpd-tools
[root@localhost conf]# htpasswd -c /usr/local/nginx/passwd.db zhangsan(db代表数据文件,给他一个用户zhangsan)
New password: 123123
Re-type new password: 123123
Adding password for user zhangsan
[root@localhost conf]# cd /usr/local/nginx
[root@localhost nginx]# chown nginx /usr/local/nginx/passwd.db  #添加nginx管理
[root@localhost nginx]# chmod 400 passwd.db                     #给予400权限
 2、修改主配置文件相对于目录,添加认证配置项
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
location / {
        **auth_basic "secret";         #主页配置项添加认证
        auth_basic_user_file /usr/local/nginx/passwd.db;**   #主页配置项添加认证
            root   html;
            index  index.html index.htm;
        }  
location /status {
stub_status on;   
access_log off;
} 
3、重启,访问测试
[root@localhost nginx]# 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

在这里插入图片描述
4 、location设置
我们在不同的location位置可以做不同的功能限制,比如我们可以让用户可以直接访问首页但不允许用户直接访问状态统计模块,如何配置?
l##基于客户端的访问控制
(1)、访问控制规则如下:
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端的访问
规则从上往下执行,如匹配则停止,不再往下匹配

location / {
           #auth_basic "secret";
           #auth_basic_user_file /usr/local/nginx/passwd.db;
            root   html;
            index  index.html index.htm;
            **deny 192.168.153.150;   #添加拒绝访问的客户端的IP
            allow all;**              #添加允许其他IP客户端访问

        }

此时通过IP为192.168.153.150的虚拟window:10主机访问192.168.153.220时就不能访问。

5.1、虚拟主机
1、基于域名的 Nginx虚拟主机

原先配置删除
(1)添加域名解析
echo "192.168.153.220 www.accp. com www.benet.com" >> /etc/hosts  (可以添加多个映射指向同一个ip地址)
(2)准备虚拟站点网页文档
[root@localhost conf]# mkdir -p /var/www/html/accp
[root@localhost conf]# mkdir -p /var/www/html/benet
[root@localhost html]# cd accp/
[root@localhost accp]# echo "<h1> www.accp.com</h1>" >/var/www/html/accp/index.html
[root@localhost accp]# cat index.html
<h1> www.accp.com</h1>
[root@localhost conf]# cd /var/www/html
[root@localhost html]# cd benet/
[root@localhost benet]# echo "<h1>www.benet.com</h1>" >/var/www/html/benet/index.html
[root@localhost benet]# cat index.html
<h1>www.benet.com</h1>

修改主配置文件
[root@localhost conf]# vim nginx.conf
 server {
        listen       80;
        server_name  www.accp.com;       改成accp
        charset UTF-8;                    注释取消同时改成中文显示UTF-8
        access_log  logs/accp.access.log;   注释取消main代表主日志删除
location / {
            root   /var/www/html/accp/;   修改
            index  index.html index.htm;
        }

another前面多余内容都删除
server {
        listen       80;
        server_name  www.benet.com;
        charset UTF-8;
        access_log  logs/benent.access.log;
      location / {
            root   /var/www/html/benent/;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
保存退出
[root@localhost conf]# nginx -t
[root@localhost conf]# cd ..
[root@localhost nginx]# cd logs/
[root@localhost logs]# ls
access.log  accp.access.log  benet.access.log  error.log  nginx.pid
[root@localhost logs]# rm -rf accp.access.log
[root@localhost logs]# rm -rf benent.access.log
[root@localhost logs]# systemctl restart nginx
[root@localhost logs]# ls
access.log  accp.access.log  benent.access.log  error.log  nginx.pid

在这里插入图片描述
2、基于端口的虚拟主机

创建8080端口网页文件
[root@localhost conf]# cd /var/www/
[root@localhost www]# cd html/
[root@localhost html]# ls
accp  benet
[root@localhost html]# mkdir accp8080
[root@localhost html]# ls
accp  accp8080  benet
[root@localhost html]# cd accp8080/
[root@localhost accp8080]# echo "<h1> www.accp8080.com </h1>" >/var/www/html/accp8080/index.html
[root@localhost accp8080]# cat index.html
<h1> www.accp8080.com </h1>
[root@localhost accp8080]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
复制www .accp.com的配置14,修改端口号
 server {                                     #原accp配置
        listen       192.168.153.220:80;     #指向监听端口
        server_name  www.accp.com;
        charset UTF-8;
        access_log  logs/accp.access.log;
      location / {
            root   /var/www/html/accp/;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

    server {                                        新accp配置
        listen       192.168.153.220:8080;          #指向8080端口
        server_name  www.accp.com;
        charset UTF-8;
        access_log  logs/accp8080.access.log;       #便于区分,指定生成不同日志
      location / {
            root   /var/www/html/accp8080/;         #指定8080端口的站点首页
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# netstat -natp | grep nginx   此时两个端口
tcp        0      0 192.168.153.220:8080    0.0.0.0:*               LISTEN      40072/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      40072/nginx: master 

在这里插入图片描述
3、基于不同IP访问

[root@localhost conf]# cd /var/www/html/
[root@localhost html]# ls
accp  accp8080  benet
[root@localhost html]# mkdir benet100
[root@localhost html]# ls
accp  accp8080  benet  benet100
[root@localhost html]# cd benet
[root@localhost benet]# cd ..
[root@localhost html]# cd benet100/
[root@localhost benet100]# vim index.html
<h1> www.benet100.com </h1>
保存退出
[root@localhost benet100]# cat index.html
<h1> www.benet100.com </h1>
[root@localhost benet100]# vim /etc/hosts
192.168.153.100 www.benet.com      添加一条映射(重叠指向ip的映射需要删除)
临时创建虚拟网卡
[root@localhost benet100]# ifconfig ens33:0 192.168.153.100 netmask 255.255.255.0
[root@localhost benet100]# ifconfig
修改配置文件
[root@localhost benet100]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
删除8080端口14行
server {
        listen       192.168.153.100:80;
        server_name  www.benet.com;
        charset UTF-8;
        access_log  logs/benet100.access.log;
      location / {
            root   /var/www/html/benet100/;
            index  index.html index.htm;
        }
保存退出
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# netstat -natp | grep nginx
tcp        0      0 192.168.153.100:80      0.0.0.0:*               LISTEN      42189/nginx: master 
tcp        0      0 192.168.153.220:80      0.0.0.0:*               LISTEN      42189/nginx:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值