linux之Nginx安装部署


目录
  • Nginx web 使用
    • 一、Nginx介绍
      • 1.什么是nginx
      • 2.nginx的特点
    • 二、Nginx部署
      • 1.官方源安装部署
      • 2.编译安装部署
    • 三、平滑增加Nginx模块
    • 四、Nginx的命令
    • 五、Nginx配置文件
    • 六、搭建超级玛丽和象棋小游戏
      • 1.准备工作
      • 2.编辑配置文件
      • 3.测试配置文件是否正常
      • 4.重启Nginx
      • 5.域名解析
      • 6.开玩!

Nginx web 使用

一、Nginx介绍

1.什么是nginx

Nginx是一个开源且高性能、可靠的http web服务、代理服务

开源:直接获取源代码
高性能:支持海量开发
可靠:服务稳定

2.nginx的特点

  • nginx支持很高的并发,nginx在处理大量并发的情况下比其他web服务要快
  • 功能模块少,只保留核心模块,其他代码模块化 (易读,便于二次开发,对于开发人员非常友好)
  • 需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块
  • 其他的web服务需要每隔一段时间进行重启,nginx不需要
  • nginx可以再运行期间,更新迭代,代码部署
  • 大多数公司都在用nginx
  • Nginx使用的是Epool网络模型(当用户发起一次请求,epoll模型会直接进行处理,效率高效,并无连接限制.)



二、Nginx部署

1.官方源安装部署

[root@web01 ~]# 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@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl start nginx  # 如果有httpd服务开启的话在这之前要执行systemctl stop httpd

2.编译安装部署

[root@web01 ~]#  wget https://nginx.org/download/nginx-1.20.2.tar.gz  # 下载安装包
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz  # 解压源码包
[root@web01 nginx-1.20.2]# ./configure
[root@web01 nginx-1.20.2]# make
[root@web01 nginx-1.20.2]# make install
[root@web01 nginx-1.20.2]# /usr/local/nginx/sbin/nginx  # 启动nginx服务

三、平滑增加Nginx模块

[root@web01 nginx-1.20.2]# yum -y install openssl openssl-devel  # 安装依赖
在上面./configure的时候,可以加--help查看有哪些模块  然后根据需要在后面添加模块名
[root@web01 nginx-1.20.2]#./configure  --with-http_ssl_module
[root@web01 nginx-1.20.2]#make 
[root@web01 nginx-1.20.2]#make install 

四、Nginx的命令

nginx #启动nginx。 等价于systemctl start nginx
 
nginx -s reopen #重启Nginx。   等价于systemctl restart nginx
 
nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx。 等价于systemctl reload nginx
 
nginx -s stop #强制停止Nginx服务。 等价于systemctl stop nginx
 
nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
 
nginx -t #检测配置文件是否有语法错误,然后退出
 
nginx -?,-h #打开帮助信息
 
nginx -v #显示版本信息并退出
 
nginx -V #显示版本和配置选项信息,然后退出
 
nginx -V 2>&1 | sed "s/\s\+--/\n --/g" #模块分行输出,格式化输出
 
killall nginx #杀死所有nginx进程
 
systemctl enable nginx  #加入开机自启
    Centos6:
        启动:nginx
            service nginx start
            /etc/init.d/nginx start
        加入开机自启:
            chkconfig nginx on
 
nginx -T #检测配置文件是否有语法错误,转储并退出
 
nginx -q #在检测配置文件期间屏蔽非错误信息
 
nginx -p prefix #设置前缀路径(默认是:/usr/share/nginx/)
 
nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)
 
nginx -g directives #设置配置文件外的全局指令

五、Nginx配置文件

[root@web01 ~]# cat /etc/nginx/nginx.conf 
#########################核心模块####################
#指定启动的用户
user  www;
#nginx的worker进程的数量
worker_processes  1;
#指定错误日志存放的路径以及记录的级别 debug/info/notice/warn/error/emerg
error_log  /var/log/nginx/error.log warn;
#指定pid文件
pid        /var/run/nginx.pid;
 
########################事件驱动模块#################
events {
    #每个worker工作进程的最大连接数
    worker_connections  1024;
}
 
######################http内核模块###################
# web服务的模块
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"';
                      # 可以替换成json格式的日志,把下面代码替换上去就行了
                      log_format json '{"@timestamp":"$time_iso8601",'
                      '"host":"$server_addr",'
                      '"service":"nginxTest",'
                      '"trace":"$upstream_http_ctx_transaction_id",'
                      '"log":"log",'
                      '"clientip":"$remote_addr",'
                      '"remote_user":"$remote_user",'
                      '"request":"$request",'
                      '"http_user_agent":"$http_user_agent",'
                      '"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 json ;
    #指定访问日志存储路径与格式
    access_log  /var/log/nginx/access.log  main;
    #高效读取文件
    sendfile        on;
    #高效传输
    #tcp_nopush     on;
    #开启长连接
    keepalive_timeout  65;
    #开启压缩
    #gzip  on;
    #包含网站的配置文件
    include /etc/nginx/conf.d/*.conf;
    ###############打开上面的文件就是下面的内容###############
 
    #一个server表示一个网站,可以有多个
    server {
        #监听的端口
        listen       80;
        #网站提供的域名
        server_name  localhost;
        #字符集
        charset utf8;
        #控制访问的网站路径
        location / {
            #指定指定网站路径
            root   /usr/share/nginx/html;
            #指定网址的索引文件
            index  index.html index.htm;
        }
    }
}

六、搭建超级玛丽和象棋小游戏

1.准备工作

1、上传代码
	mkdir /opt/Super_Mary创建游戏目录
	把游戏压缩包解压

2.编辑配置文件

[root@web01 conf.d]# vim /etc/nginx/conf.d/supermary.conf
server {
    listen 80;
    server_name game001.com;
    location / {
        root /opt/Super_Mary;
        index index.html;
    }
}

3.测试配置文件是否正常

[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4.重启Nginx

[root@web01 conf.d]# systemctl restart nginx 

5.域名解析

C:\Windows\System32\drivers\etc\hosts
172.16.1.7  game001.com   # ip是服务器的ip

6.开玩!

在网站上输入game001.com开始你的表演!!!