安装nginx
1 Linux四种安装方式
- yum 安装:简单方便,不易出错
- rpm安装包安装:便捷,方便
- 源码包安装:有点繁琐,服务性能好
- docker 安装:方便快捷
其中yum、源码安装、docker安装依赖互联网接入,rpm可以离线安装。
2 安装前注意事项
安装之前查看端口,默认nginx使用的80端口,如果80端口已经占用启动过程中可能会报错,针对端口占用情况,可以停止占用端口的服务或者nginx改用其他端口启动具体方案需要防火墙设置
netstat -nltp | grep 80
是因为没有安装net-tools
工具(CentOS7,Ubnutu16.04,Debian9,openSUSE15等以后版本系统已经默认不再集成这个命令,需要安装相应的软件net-tools)
yum install net-tools -y
3 安装所需要的环境
- gcc环境
Nginx依赖于gcc的编译环境,所以,需要安装编译环境来使Nginx能够编译起来
yum install gcc-c++
- pcre
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式
pcre-devel是使用pcre开发的一个二次开发库,故nginx也需要此库。
yum install -y pcre pcre-devel
- zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel
- openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http)用于通信加密
yum install -y openssl openssl-devel
4 开始安装
# 创建一个文件夹
cd /usr
mkdir nginx
cd nginx
# 下载nginx包
wget http://nginx.org/download/nginx-1.24.0.tar.gz
# wget 是一个从网络上自动下载文件的自由工具, 可以在用户退出系统的之后在后台继续执行, 直到下载任务完成
4.1 解压nginx包
tar -zxvf nginx-1.24.0.tar.gz
4.2 配置nginx
# 进入目录
cd nginx-1.13.7
# 执行命令 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
# 执行make命令-编译
make
# 执行make install命令-安装
make install
4.3 启动nginx
# 找到nginx的安装目录
whereis nginx
# 进入nginx安装目录的sbin目录下
cd /usr/local/nginx/sbin
# 找到sbin文件夹,启动nginx
/nginx
# /usr/local/nginx/sbin/nginx
4.4 重新加载服务
/usr/local/nginx/sbin/nginx -s reload
4.5 停止服务
/usr/local/nginx/sbin/nginx -s stop
4.6 查看进程
ps -ef | grep nginx
4.7 验证
# curl IP
curl http://localhost:80
4.8 windows端访问nginx
# 防火墙将80端口设置为永久开放
firewall-cmd --zone=public --add-port=80/tcp --permanent
4.9 重启防火墙
systemctl restart firewalld
4.10 查看IP
ip addr
4.11 windows浏览器IP+端口访问
卸载nginx
1 查看nginx服务是否在运行。
[root@localhost ~]# ps -ef |grep nginx
root 1628 1 0 Mar20 ? 00:00:00 nginx: master process ./sbin/nginx
nginx 1629 1628 0 Mar20 ? 00:00:00 nginx: worker process
root 1683 1544 0 01:18 pts/1 00:00:00 vi nginx.conf
root 1734 1712 0 02:55 pts/0 00:00:00 grep --color=auto nginx
2 停止Nginx服务
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
3 查找、删除Nginx相关文件
- 查看Nginx相关文件:whereis nginx
[root@localhost ~]# whereis nginx
nginx: /usr/local/nginx
- find查找相关文件:
[root@localhost ~]# find / -name nginx
/var/spool/mail/nginx
/usr/local/nginx
/usr/local/nginx/sbin/nginx
- 依次删除find查找到的所有目录:
[root@localhost ~]# rm -rf /usr/local/nginx
[root@localhost ~]# rm -rf /usr/local/nginx/sbin/nginx
[root@localhost ~]# rm -rf /var/spool/mail/nginx
4 再使用yum清理:
[root@localhost ~]# yum remove nginx
Loaded plugins: fastestmirror
No Match for argument: nginx
No Packages marked for removal
- 再次查看Nginx相关文件:
[root@localhost ~]# which nginx
/usr/bin/which: no nginx in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]# whereis nginx
[root@localhost ~]#