一:nginx yum源安装:
添加nginx的官方源 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
二: 下载nginx软件 yum -y install nginx
三:编写Nginx配置文件
vim /etc/nginx/conf.d/game.conf
server { listen 80; //添加一个没有被占用的端口
server_name game.nebula.com;
// windows 下 hosts 配置文件配置自己的IP地址和域名:例如game.nebula.com
location / { root /code; index index.html index.html; } }
四:根据配置文件,创建目录,上传代码
mkdir /code cd /code 上传一个游戏包到/code目录下 unzip 游戏包html.zip
# 将压缩包解压到/code目录下
五:重启nginx服务
方式一: systemctl restart nginx 立即重启,强制重启
方式二: systemctl reload nginx 平滑重启(等待交互完成之后再重启)
六:配置域名解析
windows用户: C:\Windows\System32\drivers\etc 打开hosts文件,添加: 192.168.230.144 game.nebula.com //此IP为自己的主机IP
//修改hosts文件遇到的问题:
无管理员身份无法保存你修改的hosts文件
解决办法:1.将hosts文件复制粘贴到桌面 修改后复制粘贴到C:\Windows\System32\drivers\etc 下直接替换hosts文件
2.点击此电脑上左上方的文件鼠标左键出现windows shell这个直接在里边输入 notepad hosts 修改文件保存即可
检查域名解析的ip是否是192.168.230.144
打开cmd,输入 C:>ping game.nebula.com
看是否ping通即可
七:通过浏览器访问对应的项目
打开浏览器输入: game.nebula.com
出现相应的网页界面
启动nginx服务报错:
[root@localhost code1]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
解决办法:
1、配置文件语法有误,执行nginx -t
查看输出提示信息 并检查端口是否被占用netstat -tnlp
2、nginx的配置文件nginx.conf中监听了其他端口,
这些端口的子进程仍然运行,导致端口占用。需要首先关闭子进程,才能使用该命令。
可以kill -9 pid
出现上图问题
1.vim nginx.conf --->将user nginx注释掉 添加一个user root (登录用户)
重新启动 nginx systemctl restart nginx (centos7版本)
nginx 启动成功