Liunx上安装nginx详细步骤以及nginx配置前端页面

发布于:2022-08-02 ⋅ 阅读:(257) ⋅ 点赞:(0)

1.安装依赖包

安装wget

 yum install wget


yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2.下载并解压安装包

//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
3.安装nginx

//进入nginx目录
cd /usr/local/nginx
//进入目录
cd nginx-1.13.7
//执行命令 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
//执行make命令
make
//执行make install命令
make install
4.启动nginx服务

 ​​​​​​​/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4.配置nginx.conf

# 打开配置文件
vi /usr/local/nginx/conf/nginx.conf
将端口号改成8089(随便挑个端口),因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口。

将localhost修改为你服务器的公网ip地址。

5.重启nginx

/usr/local/nginx/sbin/nginx -s reload

查看nginx进程是否启动:

ps -ef | grep nginx

 

6.若想使用外部主机访问nginx,需要关闭服务器防火墙或开放nginx服务端口,端口为上一步nginx.conf的配置端口:

centOS6及以前版本使用命令: systemctl stop iptables.service

centOS7关闭防火墙命令: systemctl stop firewalld.service

关闭防火墙会导致服务器有一定风险,所以建议是单独开放服务端口 :

开放80端口:

firewall-cmd --zone=public --add-port=80/tcp --permanent

查询端口号80 是否开启:

firewall-cmd --query-port=80/tcp

重启防火墙:

firewall-cmd --reload
 

随后访问该ip:端口 即可看到nginx界面。

7.访问服务器ip查看(备注,由于我监听的仍是80端口,所以ip后面的端口号被省略)

安装完成一般常用命令

 

进入安装目录中,

命令: cd /usr/local/nginx/sbin

启动,关闭,重启,命令:

./nginx 启动

./nginx -s stop 关闭

./nginx -s reload 重启

nginx安装完毕,接下来通过配置前端页面

部署单个前端项目

1、首先将前端项目打包放在服务器上
2、修改nginx 配置文件
在这里插入图片描述

 

在一台nginx服务器部署多个前端项目

1、将需要部署的项目放在指定位置
2、修改conf配置
在这里插入图片描述

 

location后面 跟上访问地址
配置中 要用alias 而不是root 同时路径以/结尾 同样的 路径为index的同级目录
设置index 页面

搞定之后 重启nginx 让conf文件重新加载
sbin/nginx -s quit

注: 如果是vue项目 部署到非root路径下时 需要修改index.html中资源的引入 以及css文件下图标资源的引入
在这里插入图片描述

 核心点:只需要关注location /{ }的配置即可 其他不用修改


网站公告

今日签到

点亮在社区的每一天
去签到