Nginx环境搭建与部署
文章目录
1、Nginx是什么?
Nginx是一个高性能的HTTP和反向代理服务器。
是一款轻量级的web服务器/反向代理服务器/电子邮件(IMAP/POP3)代理服务器。
单台物理服务器可支持30 000~50 000个并发请求。
2、Nginx与Apache的优点:
(1) nginx相对于apache的优点:
-
轻量级,同样是web服务,比apache占用更少的内存及资源;
-
抗并发,nginx处理请求是异步非阻塞的,而apache是阻塞型的,高并发下nginx能保持低资源低消耗高性能;
-
高度模块化的设计,编写模块相对简单;
(2) apache相对于nginx的优点:
-
Rewrite比nginx的rewrite强大 ###rewrite的主要功能就是实现统一资源定位符(URL)的跳转;
-
模块多,基本想到的都可以找到;
-
少bug,nginx的bug相对较多;
-
超稳定;
注意:一般来说,需要性能的web服务,用nginx。若不需要性能只求稳定,就选用apache。
3、Nginx与Apache的区别:
(1)作为web服务器:
相比apache,nginx使用更少的资源,支持更多的并发连接,体现更高的效率。
- Nginx作为负载均衡服务器:nginx既可以在内部直接支持rails和php程序对外进行服务,也可以支持http代理服务器对外进行服务。
- Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比较好。
- 作为邮件代理服务器:最早开发这个产品的目的之一也是作为邮件代理服务器。
(2)nginx配置简洁, apache较复杂;
(3)最核心的区别:
-
apache是同步多进程模型,一个连接对应一个进程,nginx是异步的,多个连接可以对应一个进程。
-
Nginx处理静态文件好,耗费内存少,只适合静态和反向。
-
Apache在处理动态有优势,
-
nginx并发性比较好,CPU占用内存低,如果rewrite频繁,选用apache最佳。
-
总的来说,apache依然是大部分公司的首选。
4、同步和异步的区别:
(1)共同点:
关注的是消息通信机制,即调用者在等待一件事情的处理结果时,被调用者是否提供完成状态的通知。
(2)同步:
被调用者并不提供事件的处理结果相关的通知消息,需要调用者主动询问事情是否处理完成。
(3)异步:
被调用者通过状态、通知或回调机制主动通知调用者被调用者的运行状态。
举个例子:
假如在学校,老师给你布置了一道数学题,叫你课后琢磨。之后,你都没主动跟老师说这道题
的完成情况,而是当老师问起时你才回答他写完了或没写完。这就是同步。
而异步就是,你主动向老师汇报这题你写完了,或者这题太难了不会写。
5、阻塞与非阻塞:
作用:关注调用者在等待结果返回之前所处的状态。
(1)阻塞:
指IO操作需要彻底完成后才返回到用户空间,调用结果返回之前,调用者被挂起,干不了别的事情。
(2)非阻塞:
指IO操作被调用后立即返回给用户一个状态值,而无需等到IO操作彻底完成,在最终的调用结果返回之前,调用者不会被挂起,可以去做别的事情。
异步非阻塞I/O模型
6、Nginx环境搭建:
(1)关闭防火墙和SELinux:
#关闭防火墙
systemctl stop firewalld
#关闭SELinux
setenfore 0
(2)上传软件安装包:
百度网盘分享:
链接:https://pan.baidu.com/s/1nCClD1UUZ8WMnr9PNTjq8A
提取码:swl0
直接从win10 拖到Linux中的 /opt目录下
cd /opt
#解压软件包
tar zxf nginx-1.12.0.tar.gz
[root@localhost html]#ls /opt
nginx-1.12.0 nginx-1.12.0.tar.gz rh
(3)安装依赖关系包:
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
#新建用户 和组便于管理(nginx 服务程序默认 以 nobody 身份运行,建议为其创建专门的用户账户,以便更准确的控制访问权限)
useradd -M -s /sbin/nologin nginx
#编译安装Nginx
[root@localhost opt]#cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
#make编译
make && make install -j4
#建立软连接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
(4)Nginx系统服务:
#查看80端口
[root@localhost sbin]#ss -natp | grep 80
#启动Nginx
[root@localhost sbin]#nginx
[root@localhost sbin]#ss -natp | grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=55273,fd=6),("nginx",pid=55272,fd=6))
#返回上一级目录
[root@localhost sbin]#cd ..
#这些文件要在Nginx启动后才有
[root@localhost nginx]#ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
#查看Nginx的ID信息,用kill -3 ID号 去停止Nginx
[root@localhost nginx]#cd logs/
[root@localhost logs]#ls
access.log error.log nginx.pid
#查看ID信息
[root@localhost logs]#cat nginx.pid
55272
#停止Nginx服务
[root@localhost logs]#kill -3 55272
[root@localhost logs]#ss -natp | grep 80
#这里也可以用 lsof -i :80 查看
[root@localhost logs]#lsof -i :80
注意:这里还有还可以通过kill信号来重载和停止Nginx;
[root@localhost ~]# killall -s HUP nginx ###选项 -s HUP 等同于 -1 重新加载
[root@localhost ~]# killall -s QUIT nginx ###选项 -s QUIT 等同于 -3 停止服务
#查看版本信息
[root@localhost nginx-1.12.0]#nginx -v
nginx version: nginx/1.12.0
这里 kill 的相关信号编号;
这里我们编写脚本来添加 Nginx 系统服务:
方法一:
#cd 到 /etc/init.d/ 文件下
cd /etc/init.d/
#编写脚本
vim nginx
#!/bin/bash
#chkconfig: 35 99 20
#description:Nginx Service Control Script
#Nginx 服务的启动文件位置
cmd="/usr/local/nginx/sbin/nginx"
#Nginx 服务的pid文件位置
pid="/usr/local/nginx/logs/nginx.pid"
case $1 in
start)
$cmd
;;
stop)
kill -3 `cat $pid`
;;
reload)
kill -1 `cat $pid`
;;
restart)
$0 stop
$0 start
;;
*)
echo "plaese input start,stop,reload,restart"
exit 1
esac
exit 0
#添加可执行权限
[root@localhost init.d]#chmod +x nginx
#加入开机启动项
[root@localhost init.d]#chkconfig --add nginx
#开启Nginx
[root@localhost init.d]#service nginx start
[root@localhost init.d]#ss -natp | grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=55273,fd=6),("nginx",pid=55272,fd=6))
#停止 Nginx 服务
[root@localhost init.d]#service nginx stop
方法二:
#先将第一种方法写的nginx文件移走
[root@localhost init.d]#mv nginx /root
[root@localhost init.d]#cd /lib/systemd/system/
#写服务项文件
[root@localhost system]#vim nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#重新加载一下
[root@localhost system]#systemctl daemon-reload
#启动服务
[root@localhost system]#systemctl start nginx.service
#查看端口
[root@localhost system]#ss -natp | grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=57492,fd=6),("nginx",pid=57491,fd=6))
#停止服务
systemctl stop nginx.service
(5)修改配置文件:
#配置文件位置
[root@localhost system]#cd /usr/local/nginx/conf/
[root@localhost conf]#ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@localhost conf]#cp -p nginx.conf nginx.conf.bak
[root@localhost conf]#vim nginx.conf
events {
use epoll; #使用 epoll 模型以提高性能,2.6 以上版本建议使用
worker_connections 1024; #每个进程处理1024个连接(默认)
}
http 配置:
使用“http { }”界定标记,包括访问日志、HTTP 端口、网页目录、默认字符集、连接保
持,以及后面要讲到的虚拟 Web 主机、PHP 解析等一系列设置
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;
#此选项允许或禁止使用socketde TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用 sendfile时使用
keepalive_timeout 0;
keepalive_timeout 65;
#连接保持超时时间,单位是秒
gzip on;
#gzip模块设置,设置是否开启gzip压缩输出
###web服务的监听设置
server {
listen 80;
#监听地址及端口
server_name localhost;
#站点域名,可以有多个,用空格隔开
#charset koi8-r;
#网页的默认字符集
#access_log logs/host.access.log main;
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;
}
http 实验:
[root@localhost html]#cd /usr/local/nginx/html
[root@localhost html]#vim test.html
<h1>hello tets...<h1>
#添加配置
[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm test.html;
}
(6)访问状态统计配置:
修改nginx.conf 配置文件,指定访问位置并添加 stub_status配置。
[root@localhost conf]#vim nginx.conf
location /status {
stub_status on;
access_log off;
}
(7)基于授权密码的访问控制:
生成用户密码认证文件
#安装工具包
[root@localhost conf]#yum install -y httpd-tools.x86_64
[root@localhost conf]#htpasswd -c /usr/local/nginx/passwd.db zz
New password:
Re-type new password:
Adding password for user ww
[root@localhost conf]#htpasswd /usr/local/nginx/passwd.db ww
New password:
Re-type new password:
Adding password for user zz
[root@localhost conf]#cat /usr/local/nginx/passwd.db
zz:$apr1$E9CBn4bA$99TtA9W.GwBUloxiqHbWK1
ww:$apr1$dMfNWk1D$2fUtRx6AgtSweZ6YF1VeC1
#修改配置文件
[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
#查看语法是否错误
[root@localhost conf]#nginx -t
#重启 Nginx服务
[root@localhost conf]#systemctl restart nginx.service
#设置文件属主和权限
[root@localhost conf]#chown nginx /usr/local/nginx/passwd.db
[root@localhost conf]#chmod 400 /usr/local/nginx/passwd.db
(8)基于客户端的访问控制:
客户端的访问控制要比 Apache 简单,规则如下:
1)deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。
2)allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。
3)规则从上往下执行,如匹配则停止,不再往下匹配。
[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf
location / {
#auth_basic "secret";
#auth_basic_user_file /usr/local/nginx/passwd.db;
deny 192.168.111.100; ###拒绝客户端IP访问
allow all;
root html;
index index.html index.htm;
}
(9)基于域名的nginx 虚拟主机:
使用 Nginx 搭建虚拟主机服务器时,每个虚拟 Web 站点拥有独立的“server{}”配置段,各自监听的 IP 地址、端口号可以单独指定,当然网站名称也是不同的。
#编写域名文件中的内容
[root@localhost var]#cd /var
[root@localhost var]#mkdir -p www/html/{kgc,accp}
[root@localhost var]#cd www/html/
[root@localhost html]#echo "this is kgc web" > kgc/index.html
[root@localhost html]#echo "this is accp web" > accp/index.html
#修改配置文件
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
#这里从35行开始,复制31行
server {
listen 80;
server_name www.accp.com;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.kgc.com;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
#用win10 测试一下,先在文件中添加相应域名
#文件位置
C:\Windows\System32\drivers\etc
#打开hotst文件,往里面添加域名
192.168.111.128 www.kgc.com www.accp.com
(10)基于IP地址:
#在配置文件中设置IP
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.111.128:80;
server_name www.kgc.com;
server {
listen 192.168.111.111:80;
server_name www.kgc.com;
#这里需要添加虚拟网卡
[root@localhost conf]#ifconfig ens33:0 192.168.111.111
[root@localhost conf]#systemctl restart nginx.service
(11)基于端口:
#更改配置文件中的端口
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.111.128:80;
server_name www.kgc.com;
server {
listen 192.168.111.128:66;
server_name www.kgc.com;
[root@localhost conf]#systemctl restart nginx.service
到这里,Nginx的部署已完成,各部分的实验情况已详细的操作解释。
总结:
项目实验要善于思考,勤动手做实验,这样才能记忆深刻。