NGINX部署及配置

前期准备

关闭seLinux

  vi /etc/selinux/config

  #SELINUX=enforcing
  SELINUX=disabled
  
  
  #重启
  reboot
服务器名称IP
centos 7Web01192.168.15.33

修改源,复制不能有空格

vim /etc/yum.repos.d/nginx.repo

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

nginx源码安装

yum -y install wget

wget https://nginx.org/download/nginx-1.20.1.tar.gz

#解压
tar -xf nginx-1.20.1.tar.gz

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YgwZPy81-1635241804943)(/Users/raoxurou/Library/Application Support/typora-user-images/image-20211026112120990.png)]

需要编译的配置

./configure --help

--with-http_ssl_module		# 配置HTTPS时使用
--with-http_v2_module		# 配置GOLANG语言时使用
--with-stream				# 启用TCP/UDP代理服务

设置系统配置参数

 cd nginx-1.20.1
 
./configure --with-http_ssl_module --with-http_v2_module --with-stream

安装过程中的报错

./configure: error: SSL modules require the OpenSSL library.

缺少OpenSSL文件

下载此文件

yum -y install openssl openssl-devel

再运行编译命令

编译

make

编译安装

make install 

配置环境变量

vi /etc/profile

#文件最后添加
export PATH=$PATH:/usr/local/nginx/sbin

#重载
source /etc/profile

system系统管理配置

vi /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
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

#重载
systemctl daemon-reload
systemctl start nginx

nginx常用命令

nginx -v   :  展示nginx的版本号
nginx -V	:展示nginx版本号和配置参数
nginx -t   :  测试配置文件
nginx -T   :  测试配置文件并打印
nginx -q   :  静默输出错误信息
nginx -s   :  向nginx发送信号
nginx -p   :  指定运行的家目录
nginx -e   :  设置错误日志的路径
nginx -g   :  临时设置配置项 

Nginx的配置文件

Nginx的进程模型
角色:
	master	: 负责监控worker进程
	worker	:负责处理HTTP请求
	
 vi /etc/nginx/nginx.conf 

配置详解

# 工作进程启动用户
user  nginx;
# 启动的worker进程数
worker_processes  auto;
# 指定错误日志的路径
error_log  /var/log/nginx/error.log notice;
# 指定nginx进程的PID
pid        /var/run/nginx.pid;
# 配置事件
events {
    # worker进程中最大的连接数
    worker_connections  1024;
}

# http配置模块
http {
	# include 是讲其他文件导入进来
    include       /etc/nginx/mime.types;
	
	# default_type 指定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;

	# 设置GZIP压缩
    #gzip  on;

	# 加载其他的配置文件
    include /etc/nginx/conf.d/*.conf;
}







game.conf 
# 每一个server都代表一个网站(nginx是支持多个网站)
server {
	# 指定当前网站的域名
	server_name www.baidu.com;
	
	# 指定当前网站的端口
	listen 80;
	
	# 指定一个访问路径
	location / {
		
		# 指定站点目录
		root /opt/html;
		# 指定默认访问的文件
		index index.html;
	}
}

案例:nginx搭建小游戏

上传游戏代码

scp -r  /Users/raoxurou/Desktop/mario.zip root@192.168.15.33:~ 

解压

unzip mario.zip 

将文件移动到/usr/share/nginx/

mv jiaoben1765,html5-mario /usr/share/nginx/

编写游戏配置文件

cd /etc/nginx/conf.d/game.conf


server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
    #相对应的发布文件目录
        root   /usr/share/nginx/html;
        #文件里要有默认的index.html文件
        index  index.html index.htm;
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kvkph49u-1635241804946)(/Users/raoxurou/Library/Application Support/typora-user-images/image-20211026174444761.png)]

测试

nginx -t

启动

systemctl start nginx

在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值