一 、二进制安装nginx
1、下载源码包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
2、加压并移动到OPT目录下
tar -xf nginx-1.18.0.tar.gz -C /opt
3.在/opt/nginx-1.18.0目录下 编译安装nginx源代码
[root@2021 opt]# ll
drwxr-xr-x 9 1001 1001 186 Apr 8 22:50 nginx-1.18.0
[root@2021 opt]# cd nginx-1.18.0/
[root@2021 nginx-1.18.0]# ./configure
4.解决发生的错误
# 如果出现对应的错误,则执行下面命令
[root@www nginx-1.18.0]# yum install pcre pcre-devel -y
[root@www nginx-1.18.0]# yum install zlib zlib-devel -y
5. 编译安装并测试
make
-j : 使用多核编译
# 安装
make install PROFIX=/usr/local
# 测试
[root@www ~]# /usr/local/nginx/sbin/nginx
6. 添加环境变量永久生效
## 添加环境变量
/etc/profile # 所用用户的环境变量
/root/.bash_profile # 当前用户的环境变量
NGINX_HOME=/usr/local/nginx/sbin
PATH=$PATH:$NGINX_HOME
export PATH
重新加载 /usr/local/nginx/sbin/nginx
二、搭建小游戏
1.创建用户
[root@2021 ~]# groupadd -g 666 www
[root@2021 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
2.编写脚本
# /usr/local/nginx/conf/下编写
[root@2021 conf]# cat youxi.conf
server {
listen 80;
server_name localhost;
location / {
root /mm/tank;
index index.html;
}
}
3.创建目录
mkdir /mm/tank
在mm目录下上传包并解压
[root@2021 mm]# ll
drwxr-xr-x 6 www www 72 Sep 24 2015 tank
4. 授权目录
chown -R www.www /mm/
5.修改nginx配置文件
[root@2021 ~]# cat /usr/local/nginx/conf/nginx.conf
user www;
worker_processes 1;
...
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root /mm/mofang;
#index index.html index.htm;
}
index index.html ;
}
#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;
#}
6.检查并启动nginx
systemctl reload nginx
方式二搭建
[root@2021 ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /usr/local/nginx/conf/conf.d/*.conf; #增加这一行
}
[root@2021 ~]# vim /usr/local/nginx/conf/conf.d/game.conf
server {
listen 80;
server_name localhost;
location / {
root /mm/html;
index index.html;
}
}
[root@2021~]# systemctl reload nginx