08@nginx服务搭建及文件配置

Nginx web

PHP

一、Nginx概述

1.nginx 简述
*Nginx* (engine x) 是一个高性能的HTTP、反向代理web服务器以及邮件服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定
2.其他相关的web服务
1>.apache(httpd):早期使用,性能不高,上手很难;

2>.nginx
	Tengine:淘宝基于nginx进行二次开发的产物
	openresty:基于nginx与lua的高性能web平台
	
3>.lighttpd:消耗的内存和cpu较低,性能好,还有很多模块

4>.IIS:windows的web服务

5>.GWS:google web server

6>.BWS:baidu web server
3.nginx特点
1).高性能、高并发:
      在并发特别高的时候,nginx的响应速度比其他的web服务快很多。
      
2).轻量且高扩展性:
      1》.轻量:功能模块少,仅保留http模块和核心模块
      2》.高扩展性:模块需要哪个可以装哪个,还可以集成第三方的模块 
       
3).高可靠性:
       其他的web服务运行一段时间需要重启,nginx不需要
       nginx的宕机时间 9999、99999级别       
   

4).支持热部署

        可以在不停服务的情况下进行升级
        nginx的master管理进程和worker的工作进程是分离的
        
5).互联网公司都在用nginx,应用广泛
        nginx技术成熟,具备企业里所有需要的功能,负载,缓存,安全,web服务
         统一技术栈,降低维护成本,降低技术难度
         
6).Nginx使用Epool网络模型
         select:当用户发起一次请求,select模型就会进行一次遍历扫描,从而导          致性能低下;
         Epoll:当用户发起请求,epoll模型会直接进行处理,效率高效,并无连接          限制

在这里插入图片描述

静态服务器:不需要服务器做特殊处理的代码就是静态资源。(html、图片、音频、视频)
代理服务器:将一个服务的请求转移另一个服务。
安全服务器:nginx做安全需要LUA脚本语言配合使用。

二、Nginx和Apache

Nginx出现以前,Apache几乎是WEB服务器行业的垄断地位,后来逐渐被Nginx给替代了。

Nginx使用网络模型epool
Apache使用的是select

小明  --->  小花   select
小明  --->  小花   epool

Nginx比Apache占有资源少,Nginx支持的插件还比较多,根据不同模块可以自定义Nginx。

三、部署Nginx

部署Nginx常用的三种?

1.epel源安装
# 安装epel源
[root@web01 ~]# vim /etc/yum.repos.d/epel.repo
[epel]
name='this is epel repo'
baseurl=https://repo.huaweicloud.com/epel/7/x86_64/
gpgcheck=0

[root@web01 ~]# yum clean all
[root@web01 ~]# yum makecache

# 安装nginx
[root@web01 ~]# yum install nginx --nogpgcheck
[root@web01 ~]# systemctl start nginx

在这里插入图片描述

2.官方yum源
# 安装官方源
##  nginx.org ---> documentation ---> Installing nginx ---> packages --->   RHEL/CentOS

[root@web02 ~]#  vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

 # 安装启动服务
[root@web02 ~]# yum install nginx -y 
[root@web02 ~]# nginx
[root@web02 ~]# systemctl start nginx



#检查启动

#方式一:
[root@web01 ~]# systemctl status nginx

#方式二:
[root@web01 ~]# ps -ef | grep [n]ginx

#方式三:
[root@web01 ~]# netstat -lntp

#方式四:
访问页面 10.0.0.7

#方式五:
[root@web01 ~]# curl 10.0.0.7

#方式六:
[root@web01 ~]# nginx -v
nginx version: nginx/1.16.1


#nginx常用命令
1.nginx启动
#方法一:
[root@web01 ~]# nginx
#方法二:
[root@web01 ~]# systemctl start nginx

#注意:使用什么方式启动,就是用什么方式管理,不要混着用
2.nginx停止
#方法一:
[root@web01 ~]# nginx -s stop
#方法二:
[root@web01 ~]# systemctl stop nginx

3.nginx重启
[root@web01 ~]# systemctl restart nginx

4.nginx重新加载配置文件
#方法一:
[root@web01 ~]# nginx -s reload
#方法二:
[root@web01 ~]# systemctl reload nginx

5.检查nginx配置
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6.加入开机自启
[root@web01 ~]# systemctl enable nginx



#Centos6
nginx
/etc/init.d/nginx start
service nginx start
chkconfig nginx on

在这里插入图片描述

3.源码包编译安装
# 下载源码包
# nginx.org ---> download ---> 下载Mainline version
[root@web03 opt]# wget http://nginx.org/download/nginx-1.19.10.tar.gz

# 解压安装包
[root@web03 opt]# tar -xf nginx-1.19.10.tar.gz

#创建用户
[root@web03 opt] # groupadd www -g 666
[root@web03 opt]# useradd www -u 666 -g 666


#检测生成
[root@web03 opt]# cd nginx-1.19.10/
[root@web03 nginx-1.19.10]# ./configure --prefix=/usr/local/nginx-1.19.10



# 编译二进制文件并安装
[root@web03 nginx-1.19.10]# make -j
[root@web03 nginx-1.19.10]# make install 
[root@web03 nginx-1.19.10]# ln -s /usr/local/nginx-1.19.10 /usr/local/nginx

[root@web03 nginx-1.19.10]# cd /usr/local/
[root@web03 local]# ll
total 0
drwxr-xr-x. 6 root root 54 Apr 26 23:55 nginx-1.19.10



# 优化——在/etc/profile中添加变量
export NGINX_HOME=/usr/local/nginx-1.19.10
PATH=$PATH:$NGINX_HOME/sbin
export PATH


#启动nginx
#启动的时候没有办法使用systemctl管理

[root@web03 local]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target




三、nginx升级

#升级版本
[root@web01 ~]# wget http://nginx.org/download/nginx-1.17.8.tar.gz
[root@web01 ~]# tar xf nginx-1.17.8.tar.gz 
[root@web01 ~]# cd nginx-1.17.8/
[root@web01 nginx-1.17.8]# ./configure --prefix=/usr/local/nginx-1.17.8 --user=www --group=www --with-http_addition_module --with-http_auth_request_module
[root@web01 nginx-1.17.8]# make && make install
[root@web01 nginx-1.17.8]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.17.8 /usr/local/nginx

------------------------------------------------------------

#添加模块
[root@web01 ~]# cd nginx-1.16.1/
[root@web01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-new-1.16.1 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --with-http_mp4_module
[root@web01 nginx-1.16.1]# make && make install
[root@web01 nginx-1.16.1]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-new-1.16.1 /usr/local/nginx

四、Nginx的使用

# 配置文件/etc/nginx.conf
## yum安装:/etc/nginx
## 源码包安装:安装家目录的conf文件夹中
# ---------- 全局配置(全局生效) ------------------
user  nginx;							# 启动nginx work进程的用户名
worker_processes  auto;					# 启动的worker进程数(auto默认跟cpu数量相同)

error_log  /var/log/nginx/error.log notice;	# 错误日志路径
pid        /var/run/nginx.pid;			# PID 文件路径

# ---------- 系统事件配置模块(全局生效)-----------------------
events {								# 事件配置模块
    worker_connections  1024;			# 最大连接数
    use epool;							# 指定网络模型(select、pool、epool)
}

# ---------- HTTP 请求模块(处理HTTP请求的模块)
http {											# 模块名称	
    include       /etc/nginx/mime.types;		# nginx可以处理的文件类型
    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  /var/log/nginx/access.log  main;			# 访问日志

	# TCP连接配置
    sendfile        on;
    #tcp_nopush     on;

	# 长链接配置
    keepalive_timeout  65;

	# gzip压缩
    #gzip  on;

	# 包含其他文件
    include /etc/nginx/conf.d/*.conf;
    
    server { # 每一个Server就是一个网站
        listen       80;						# 监听的端口
        server_name  localhost;					# 域名

        #access_log  /var/log/nginx/host.access.log  main; # 访问日志

        location / {							# 位置(指定访问的站点)
            root   /usr/share/nginx/html;		# 指定的站点目录
            index  index.html index.htm;		# 指定索引文件
        }
    }
}


# 基本命令
nginx -h : 帮助信息
		-v : 展示版本信息
		-V :展示版本信息及模块配置信息
		-t : 检查配置文件是否正确
		-q : 指定配置项,并启动
		-s : 指定nginx启动方式
		-c : 指定配置文件路径
		-e : 指定错误日志路径
		-p :指定nginx安装路径

五、nginx相关配置文件

#查看nginx相关配置文件
[root@web01 ~]# rpm -ql nginx
1.Nginx主配置文件
路径类型作用
/etc/nginx/nginx.conf配置文件nginx主配置文件
/etc/nginx/conf.d/default.conf配置文件默认网站配置文件
2.Nginx代理相关参数文件
路径类型作用
/etc/nginx/fastcgi_params配置文件Fastcgi代理配置文件(php)
/etc/nginx/scgi_params配置文件scgi代理配置文件
/etc/nginx/uwsgi_params配置文件uwsgi代理配置文件(python)
3.Nginx编码相关配置文件
路径类型作用
/etc/nginx/win-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-win配置文件Nginx编码转换映射文件
/etc/nginx/mime.types配置文件Content-Type与扩展名
4.Nginx管理相关命令
路径类型作用
/usr/sbin/nginx命令Nginx命令行管理终端工具
/usr/sbin/nginx-debug命令Nginx命令行与终端调试工具
5.Nginx日志相关目录与文件
路径类型作用
/var/log/nginx目录Nginx默认存放日志目录
/etc/logrotate.d/nginx配置文件Nginx默认的日志切割

六、Nginx的配置文件详解

web网站,nginx是支持多站点的服务的。

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束,{} 内每行以 ; 结尾。

Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)
#---------------------------------核心模块-----------------------------
#启动用
user  nginx;
#工作进程数量
worker_processes  1;
#错误日志						debug/info/notice/warn/error/crit/alter/emerg
error_log  /var/log/nginx/error.log warn;
#pid文件
pid        /var/run/nginx.pid;

#-----------------------------事件驱动模块-------------------------
events {
    worker_connections  1024;
}

#-----------------------------http内核模块--------------------------
http {
	#nginx包含的文件类型
    include       /etc/nginx/mime.types;
    #nginx默认不识别的文件类型直接下载
    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  /var/log/nginx/access.log  main;
	#优化部分
    sendfile        on;
    #tcp_nopush     on;
	
	#保持长连接
    keepalive_timeout  65;
    tcp_nodelay on;
     
	#是否开启压缩
    #gzip  on;
	
	#包含配置文件
    include /etc/nginx/conf.d/*.conf;
    
    #使用server配置网站,每个server代表一个网站(我们称之为虚拟主机)
    server {
    	#监听端口
        listen       80;
        #提供的域名
        server_name  _;
		
		#控制网站访问路径
		location / {
			#站点目录,指定存放页面的地址
			root /code;
			#默认访问的页面
			index index.html
		}
    }
    ...
    server {
    ...
    }
}    

七、Nginx模块配置设定详解

1.Nginx基于域名代理多个站点
# web01: /etc/nginx/conf.d/web01.conf
server {

	listen 80;
	server_name www.web01.com;
	location / {
		root /usr/share/nginx/web01/;
		index index.html;	
	}
}

# web02:/etc/nginx/conf.d/web02.conf
server {

	listen 80;
	server_name www.web02.com;
	location / {
		root /usr/share/nginx/web02/;
		index index.html;
	}

}

在这里插入图片描述

2.线上域名
# 购买域名 ---> 备案域名 ---> 解析

在这里插入图片描述

# 域名解析
A记录:将域名解析到服务器上

CNAME:将一个域名解析到另一个域名上(域名转发)
3.Nginx基于IP的多个站点
# web01: /etc/nginx/conf.d/web01.conf
server {

	listen 80;
	server_name 192.168.1.8:80;
	location / {
		root /usr/share/nginx/web01/;
		index index.html;	
	}
}

# web02:/etc/nginx/conf.d/web02.conf
server {

	listen 80;
	server_name 172.16.1.8:80;
	location / {
		root /usr/share/nginx/web02/;
		index index.html;
	}

}
4.基于多端口的多站点
# web01: /etc/nginx/conf.d/web01.conf
server {

	listen 8080;
	server_name localhost;
	location / {
		root /usr/share/nginx/web01/;
		index index.html;	
	}
}

# web02:/etc/nginx/conf.d/web02.conf
server {

	listen 8090;
	server_name localhost;
	location / {
		root /usr/share/nginx/web02/;
		index index.html;
	}

}

在这里插入图片描述

5、Nginx排错检查

在这里插入图片描述

6、Nginx日志

nginx运行过程中,会留下很多日志。每访问一次就会生成一条日志。

log_format access_json '{"@timestamp":"$time_iso8601",'
                           '"host":"$server_addr",'
                           '"clientip":"$remote_addr",'
                           '"size":$body_bytes_sent,'
                           '"responsetime":$request_time,'
                           '"upstreamtime":"$upstream_response_time",'
                           '"upstreamhost":"$upstream_addr",'
                           '"http_host":"$host",'
                           '"url":"$uri",'
                           '"domain":"$host",'
                           '"xff":"$http_x_forwarded_for",'
                           '"referer":"$http_referer",'
                           '"status":"$status"}';

access_log  /var/log/nginx/access.log  access_json;



$remote_addr        # 记录客户端IP地址
$remote_user        # 记录客户端用户名
$time_local         # 记录通用的本地时间
$time_iso8601       # 记录ISO8601标准格式下的本地时间
$request            # 记录请求的方法以及请求的http协议
$status             # 记录请求状态码(用于定位错误信息)
$body_bytes_sent    # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent         # 发送给客户端的总字节数
$msec               # 日志写入时间。单位为秒,精度是毫秒。
$http_referer       # 记录从哪个页面链接访问过来的
$http_user_agent    # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录经过的所有服务器的IP地址
$X-Real-IP		   #记录起始的客户端IP地址和上一层客户端的IP地址
$request_length     # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time       # 请求花费的时间,单位为秒,精度毫秒
# 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客 户端真实的IP地址。
# $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
# 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。
7、日志的切割

logrotate:/etc/logrotate.d/

[root@web03 local]# pwd
/etc/logrotate.d/
[root@web03 local]# cat nginx
/var/log/nginx/*.log {
        daily				# 切割日志的时间				
        missingok			# 忽略错误
        rotate 52			# 日志最多存放52次
        compress			# 使用gzip压缩
        delaycompress		# 延时压缩
        notifempty			# 不处理空文件
        create 640 nginx adm# 定义日志的权限
        sharedscripts		# 开始执行脚本
        postrotate			# 标注脚本内容
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript			# 结束
}

八、nginx + php 编译安装

#1、下载安装包
wget https://www.php.net/distributions/php-5.6.40.tar.gz

#2、解压并检查和设置参数
[root@web02 opt]# tar -xf php-5.6.40.tar.gz 
[root@web02 php-5.6.40]# ./configure  --with-mysql  --enable-fpm   

#3、编译
[root@web02 opt]# make -j

#4、安装
[root@web02 opt]# make install

#5、处理配置文件
[root@web02 php-5.6.40]# mv php.ini-development /usr/local/lib/php.ini
[root@web02 php-5.6.40]# php --ini   
Configuration File (php.ini) Path: /usr/local/lib
Loaded Configuration File:         /usr/local/lib/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

#6、检查测试安装状态
[root@web02 php-5.6.40]# php -m  或者  php-fpm -m   
[PHP Modules]
mysql
bcmath
cgi-fcgi
Core
ctype
curl
date
dom
ereg
filter

....
..
.

九、nginx web页面搭建小游戏

#1.编写配置文件

[root@web01 ~]# vim /etc/nginx/conf.d/www.hzl.conf 
server {
    listen 80;
    server_name www.hzl.com;

    location / {
        root /code;
        index index.html;
    }
}


#2.创建站点目录
[root@web01 ~]# mkdir /code
[root@web01 ~]# chown www.www  -R /code

#3.上传代码包
[root@web01 ~]# cd /code/
[root@web01 code]# rz mario.zip
[root@web01 code]# unzip mario.zip
[root@web01 code]# cp -r html/* ./



#4.重载nginx
[root@web01 code]# nginx -s reload		   注:以什么方式启动的就以什么方式管理
[root@web01 code]# systemctl restart nginx     (建议使用system管理)
#到这咯就结束拉!

#拜拜啦!

#感谢查阅!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值