nginx 部署初始

一、Nginx部署

1.epel源安装

[root@web01 ~]# yum install -y nginx

2.官方源安装

1)配置官方源

[root@web02 ~]# 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

2)安装依赖

[root@web02 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

3)安装nginx

[root@web02 ~]# yum install -y nginx

4)创建用户

[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666

5)修改nginx启动用户

[root@web02 ~]# vim /etc/nginx/nginx.conf
user www;

6)启动服务

[root@web02 ~]# systemctl restart nginx

7)验证启动

[root@web02 ~]# ps -ef | grep nginx

3.源码包安装

0)安装依赖

[root@web03 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

1)下载或者上传包

[root@web03 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
#或者
[root@web03 ~]# rz nginx-1.18.0.tar.gz

2)解压

[root@web03 ~]# tar xf nginx-1.18.0.tar.gz

3)创建用户

[root@web03 ~]# groupadd www -g 666
[root@web03 ~]# useradd www -u 666 -g 666

3)生成

[root@web03 ~]# cd nginx-1.18.0/
[root@web03 ~/nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module

4)编译安装

[root@web03 ~/nginx-1.18.0]# make && make install

5)配置system管理

#配置system管理
[root@web03 ~]# vim /etc/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

6)做软链接

[root@web03 ~]# ln -s /usr/local/nginx-1.18.0 /usr/local/nginx
#配置环境变量
[root@web03 ~]# cat /etc/profile.d/nginx.sh 
export PATH=/usr/local/nginx/sbin/:$PATH

#软连接的作用:
1.配置环境变量可以不加版本号
2.配置system启动可以不加版本号
3.升级直接切换软连接的链接文件即可

7)启动

[root@web03 ~]# systemctl daemon-reload
[root@web03 ~]# systemctl start nginx
#配置开机自启
[root@web03 ~]# systemctl enable nginx

4.nginx升级

1.下载新版本的包
[root@web03 ~]# wget http://nginx.org/download/nginx-1.19.2.tar.gz
2.解压
[root@web03 ~]# tar xf nginx-1.19.2.tar.gz
3.生成编译安装
[root@web03 ~]# cd nginx-1.19.2/
[root@web03 nginx-1.19.2]# ./configure --prefix=/usr/local/nginx-1.19.2 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module
[root@web03 nginx-1.19.2]# make && make install
4.替换配置文件
[root@web03 /usr/local]# cp nginx-1.18.0/conf/nginx.conf nginx-1.19.2/conf/
cp: overwrite ‘nginx-1.19.2/conf/nginx.conf’? y
5.重启nginx
[root@web03 /usr/local]# systemctl restart nginx

二、nginx相关配置文件

为了让大家更清晰的了解Nginx软件的全貌,可使用rpm -ql nginx查看整体的目录结构及对应的功能,如下表格整理了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与扩展名,nginx支持的数据类型

4.Nginx管理相关命令

路径 类型 作用
/usr/sbin/nginx 命令 Nginx命令行管理终端工具
/usr/sbin/nginx-debug 命令 Nginx命令行与终端调试工具

5.Nginx日志相关目录与文件

路径 类型 作用
/var/log/nginx 目录 Nginx默认存放日志目录
/etc/logrotate.d/nginx 配置文件 Nginx默认的日志切割

三、nginx主配置文件

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

Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)
[root@web02 ~]# cat /etc/nginx/nginx.conf
#---------------------------核心模块------------------------------
#启动用户
user  www;
#工作进程数
worker_processes  1;
#错误日志		debug/info/notice/warn/error/emeor
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;
	#压缩
    #gzip  on;
	#包含的配置文件
    include /etc/nginx/conf.d/*.conf;
    
	server {
		#服务监听端口
        listen       80;
        #域名
        server_name  localhost;
        #字符集
        charset koi8-r;
        #请求的地址
        location / {
        	#站点目录,当访问/的时候跳转目录
            root   /usr/share/nginx/html;
            #默认访问页面
            index  index.html index.htm;
        }
    }
}

四、搭建小游戏

1.编写nginx配置文件

[root@web02 ~]# vim /etc/nginx/conf.d/default.conf 
server {
    listen 80;
    server_name localhost;
    location / {
        root /code/mario;
        index index.html;
    }
}

2.创建目录

[root@web02 ~]# mkdir /code

3.上传代码

[root@web02 ~]# cd /code
[root@web02 /code]# rz mario.zip

4.解压代码

[root@web02 /code]# unzip mario.zip
#授权
[root@web02 /code]# chown -R www.www /code/

5.重启

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

[root@web02 /code]# systemctl restart nginx

作业:

1.使用官方源和源码包安装nginx
2.搭建小游戏
原文地址:https://www.cnblogs.com/xiaolang666/p/13537071.html