Linux Nginx 安装与卸载
一、安装Nginx
1、环境说明
- Linux:CentOS Linux release 8.2.2004 (Core)
- Nginx:nginx-1.20.0.tar.gz
2、下载Nginx安装包
官网:http://nginx.org/en/download.html
版本:nginx-1.20.0 Stable version 稳定版本
Linux下载:wget http://nginx.org/download/nginx-1.20.0.tar.gz
3、安装依赖包
# 一键安装上面四个依赖
$ yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
很多人都说先装依赖,也对,不过也可以放在后面,看报错,需要那个装那个,这样你还能印象深刻点~~
如果想要先安装依赖的话主要有以下四个
gcc、gcc-c++: 主要用来进行编译相关使用
openssl、openssl-devel: 一般当配置https服务的时候就需要这个了
zlib、zlib-devel:主要用于文件的解压缩
pcre、pcre-devel: Nginx的rewrite模块和HTTP核心模块会用到PCRE正则表达式语法
4、上传解压安装包
# 创建nginx源文件目录
$ mkdir -p /usr/local/nginx/server
$ cd /usr/local/nginx/server
# rz上传 或 ftp工具上传
$ rz
# 解压
$ tar -zxvf nginx-1.20.0.tar.gz
# 删除安装包
$ rm -rf nginx-1.20.0.tar.gz
5、配置、编译及安装
$ cd nginx-1.20.0/
# 配置
$ ./configure
# 编译
$ make
# 安装
make install
5、测试启动
# 进去运行目录
$ cd /usr/local/nginx/sbin
# 运行测试
$ ./nginx -t
# 出现如下表示配置正常
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
过程中可能会碰到依赖缺失问题,少了就转,yum install XXX,妥妥的。到这配置就结束啦~~~
6、配置nginx.conf
# vim 编辑 nginx.conf
$ vi /usr/local/nginx/conf/nginx.conf
6.1、配置文件的结构
# ...
events {
# ...
}
http {
# ...
server{
# ...
}
# ...
server{
# ...
}
}
6.2、配置虚拟主机
# 启动进程,通常设置成和 CPU 的数量相等
user nginx;
worker_processes 1;
events {
# epoll 是多路复用 IO(I/O Multiplexing) 中的一种方式
# 但是仅用于 linux2.6 以上内核,可以大大提高 nginx 的性能
use epoll;
# 单个后台 worker process 进程的最大并发链接数
worker_connections 1024;
}
http {
# 设定 mime 类型,类型由 mime.type 文件定义
include mime.types;
default_type application/octet-stream;
# sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
# 必须设为 on,如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,以平衡磁盘与网络 I/O 处理速度,降低系统的 uptime.
sendfile on;
# 连接超时时间
keepalive_timeout 65;
# 设定请求缓冲
client_header_buffer_size 2k;
# 配置虚拟主机 119.45.36.46
server {
# 监听的ip和端口,配置 119.45.36.46:80
listen 80;
# 虚拟主机名称这里配置ip地址
server_name 119.45.36.46;
# 所有的请求都以 / 开始,所有的请求都可以匹配此 location
location / {
# 使用 root 指令指定虚拟主机目录即网页存放目录
# 比如访问 http://ip/index.html 将找到 /usr/local/nginx/wwwroot/html80/index.html
# 比如访问 http://ip/item/index.html 将找到 /usr/local/nginx/wwwroot/html80/item/index.html
root /usr/local/nginx/wwwroot/html80;
# 指定欢迎页面,按从左到右顺序查找
index index.html index.htm;
}
}
# 配置虚拟主机 192.168.75.245
server {
# 监听的ip和端口,配置 119.45.36.46:8080
listen 8080;
# 虚拟主机名称这里配置ip地址
server_name 119.45.36.46;
location / {
root /usr/local/nginx/wwwroot/html8080;
index index.html index.htm;
}
}
}
6.3、配置 Nginx 反向代理
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 配置一个代理即 tomcat1 服务器
upstream tomcatAdmin {
server 119.45.36.46:9090;
}
# 配置一个代理即 tomcat2 服务器
upstream tomcatSystem {
server 119.45.36.46:9091;
}
# 配置一个虚拟主机
server {
listen 80;
server_name admin.tangsm.com;
location / {
# 域名 admin.tangsm.com 的请求全部转发到 tomcatAdmin 即 tomcat1 服务上
proxy_pass http://tomcatAdmin;
# 欢迎页面,按照从左到右的顺序查找页面
index index.jsp index.html index.htm;
}
}
server {
listen 80;
server_name system.tangsm.com;
location / {
# 域名 system.tangsm.com 的请求全部转发到 tomcatSystem 即 tomcat2 服务上
proxy_pass http://tomcatSystem;
index index.jsp index.html index.htm;
}
}
}
此示例需要配置 Windows Hosts 文件
- 通过 host 文件指定 admin.tangsm.com 和 system.tangsm.com 对应 119.45.36.46 虚拟机:
- 修改 window 的 hosts 文件:(C:\Windows\System32\drivers\etc)
119.45.36.46 admin.tangsm.com 119.45.36.46 system.tangsm.com
6.4、配置负载均衡
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 配置负载均衡规则 weight为权重系数
upstream myapp {
server 119.45.36.46:9090 weight=10;
server 119.45.36.46:9091 weight=10;
}
server {
listen 80;
server_name nginx.tangsm.com;
location / {
proxy_pass http://myapp;
index index.jsp index.html index.htm;
}
}
}
此示例需要配置 Windows Hosts 文件
- 通过 host 文件指定 nginx.tangsm.com 对应 119.45.36.46 虚拟机:
- 修改 window 的 hosts 文件:(C:\Windows\System32\drivers\etc)
119.45.36.46 nginx.tangsm.com
相关配置说明
# 定义负载均衡设备的 Ip及设备状态
upstream myServer {
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
# 在需要使用负载的 Server 节点下添加
proxy_pass http://myServer;
upstream
:每个设备的状态:down
:表示当前的server
暂时不参与负载weight
:默认为 1weight
越大,负载的权重就越大。max_fails
:允许请求失败的次数默认为 1 当超过最大次数时,返回proxy_next_upstream
模块定义的错误fail_timeout
:max_fails
次失败后,暂停的时间。backup
:其它所有的非backup
机器down
或者忙的时候,请求backup
机器。所以这台机器压力会最轻
7、启动
# 初次启动指定Nginx配置文件启动
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 查看nginx进程是否启动
$ ps -ef | grep nginx
# 正常启动如下
root 3652696 1 0 19:16 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 3652697 3652696 0 19:16 ? 00:00:00 nginx: worker process
root 3663676 3467849 0 19:22 pts/2 00:00:00 grep --color=auto nginx
安装完成一般常用命令
# 进入执行目录
$ cd /usr/local/nginx/sbin
# 启动
$ ./nginx
# 关闭
$ ./nginx -s stop
# 重新加载配置
$ ./nginx -s reload
8、测试
若想使用外部主机连接上虚拟机访问端口119.45.36.46,需要关闭虚拟机的防火墙
# centOS6及以前版本使用命令
$ systemctl stop iptables.service
# centOS7关闭防火墙命令
$ systemctl stop firewalld.service
访问服务器ip查看(备注,由于我监听的仍是80端口,所以ip后面的端口号被省略)
二、卸载Nginx
演示环境:centos7使用yum安装的Nginx
1、检查nginx服务是否在运行
输入命令 ps -ef | grep nginx 检查一下nginx服务是否在运行
[root@localhost /]# ps -ef |grep nginx
root 3163 2643 0 14:08 tty1 00:00:00 man nginx
root 5427 1 0 14:50 ? 00:00:00 nginx: master process nginx
nginx 5428 5427 0 14:50 ? 00:00:00 nginx: worker process
root 5532 2746 0 14:52 pts/0 00:00:00 grep --color=auto nginx
2、停止Nginx服务
[root@localhost /]# /usr/sbin/nginx -s stop
[root@localhost /]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1261/sshd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1261/sshd
3、查找、删除Nginx相关文件
-
查看Nginx相关文件:whereis nginx
[root@localhost /]# whereis nginx nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz /usr/share/man/man3/nginx.3pm.gz # find查找相关文件 [root@localhost /]# find / -name nginx /usr/lib64/perl5/vendor_perl/auto/nginx /usr/lib64/nginx /usr/share/nginx /usr/sbin/nginx /etc/logrotate.d/nginx /etc/nginx /var/lib/nginx /var/log/nginx
-
依次删除find查找到的所有目录:rm -rf /usr/sbin/nginx
4、再使用yum清理
[root@localhost /]# yum remove nginx
依赖关系解决
======================================================================================================
Package 架构 版本 源 大小
======================================================================================================
正在删除:
nginx x86_64 1:1.12.2-3.el7 @epel 1.5 M
为依赖而移除:
nginx-all-modules noarch 1:1.12.2-3.el7 @epel 0.0
nginx-mod-http-geoip x86_64 1:1.12.2-3.el7 @epel 21 k
nginx-mod-http-image-filter x86_64 1:1.12.2-3.el7 @epel 24 k
nginx-mod-http-perl x86_64 1:1.12.2-3.el7 @epel 54 k
nginx-mod-http-xslt-filter x86_64 1:1.12.2-3.el7 @epel 24 k
nginx-mod-mail x86_64 1:1.12.2-3.el7 @epel 99 k
nginx-mod-stream x86_64 1:1.12.2-3.el7 @epel 157 k
事务概要
======================================================================================================
移除 1 软件包 (+7 依赖软件包)
安装大小:1.9 M
是否继续?[y/N]:y
- ok nginx 卸载完成!
参考文档