Windows
1. Nginx下载
下载地址:http://nginx.org/en/download.html
下载稳定版本
下载后解压,并运行exe文件即可开启(双击一次即可打开,控制台一闪而过)
2. 部署
将打包后的dist文件夹下的文件放到html中即可
html文件夹(例):
然后访问localhost(本机的ip)就能访问到部署的项目了
3. 配置
在 conf 中的 nginx.conf 中进行配置
Linux
在 Linux 下安装 Nginx
第一种方法用yum安装:
直接在终端执行一下三个步骤即可
yum install nginx
systemctl start nginx.service # 开启 nginx 服务
systemctl enable nginx.service # 跟随系统启动
安装过程中遇到的错误:
错误:
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
没有可用软件包 nginx。
错误:无须任何处理
错误原因:
很多软件包在yum里面没有的,解决的方法,就是使用epel源,也就是安装epel-release软件包。EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL、CentOS等系统。可以在下面的网址上找到对应的系统版本,架构的软件包。
解决方案,安装epel
sudo yum install epel-release
更新(更新时间稍微长一些,一路 yes 即可)
yum update
重新试一下:
yum install -y nginx
安装完毕
第二种方式
在官方下载,用 Xftp 7 上传到Linux服务器然后解压
确保没有所有的与 nginx 相关的文件与命令
1. 卸载
如果是使用yum安装:
yum remove nginx
如果已经不记得安装方式了,那么可以使用以下步骤进行全删除:
rm -rf /etc/nginx/
rm -rf /usr/sbin/nginx
rm /usr/share/man/man1/nginx.1.gz
apt-get remove nginx*
whereis nginx #查看是否有含nginx的文件夹
nginx 是否有nginx命令
2.然后解压下载下来的gz压缩包
tar -zxvf nginx-xxx.tar.gz
3. 进入解压后的文件执行configure文件
./configure
4. 然后make并安装
make
make install
5. 配置nginx环境变量
- 运行命令:
vim /etc/profile
在文件末尾添加如下内容:
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
-
让配置生效,运行命令:
source /etc/profile
-
设置nginx为系统服务。
在/lib/systemd/system目录下创建nginx.service文件,
运行命令:vim /lib/systemd/system/nginx.service
输入如下内容,nginx启动程序路径根据自身实际情况调整:
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
以后就可使用如下命令,来管理nginx了:
# systemctl start nginx 启动服务
# systemctl stop nginx 停止服务
# systemctl restart nginx 重新启动服务
# systemctl list-units --type=service 查看所有已启动的服务
# systemctl status nginx 查看服务当前状态
# systemctl enable nginx 设置开机自启动
# systemctl disable nginx 停止开机自启动
6. 关闭防火墙!!!!
systemctl stop firewalld
7. 注意
最后注意要将项目部署在 /usr/local/nginx/html 下,或者修改 nginx.conf 文件调整部署路径
完结
8. 遇到的问题:
问题一:./configure 时报错
原因:Nginx是高度自由化的Web服务器,它的功能是由许多模块来支持。如果使用了某个模块,这个模块使用了一些类似zlib或OpenSSL等的第三方库,那么就必须先安装这些软件。
解决:安装第三方库
yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel
问题二:首先我的压缩包解压之后的文件目录如下,但是我们要执行sbin下面的的nginx文件。(找不到sbin目录):如下
在make之后可见:
可见sbin文件 usr/local/nginx下面
问题三:防火墙啊,一点记得关