目录
- Nginx介绍
- yum安装和编译安装
- Nginx编译安装
- Nginx关闭、重新启动和卸载
Nginx介绍
概述:Nginx是一个 HTTP 和反向代理服务器, 邮件代理服务器, 和通用的 TCP/UDP 代理服务器。
yum安装和编译安装
简单来说,yum安装就是安装别人已经写好的包或模块,会自动解决相关的依赖,简单方便。
编译安装是自己下载源码包,进行指定参数的设定,指定模块的安装,需要自己解决相关的包依赖。
Nginx编译安装
- 安装epel源,解决软件之间的相关依赖
yum install epel-release -y
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim wget
- 新建一个用户,用它来启动一个nginx进程
useradd shengxia -s /sbin/nologin
- 新建文件夹,进入文件夹,下载软件安装包
mkdir /lianxi/nginx/shengxia -p
cd /lianxi/nginx/shengxia
wget http://nginx.org/download/nginx-1.22.1.tar.gz
[root@localhost shengxia]# pwd
/lianxi/nginx/shengxia
- 解压进入解压目录,执行编译前的配置,指定安装路径,自己需要的安装模块
必须要在nginx-1.22.1目录下才能执行./configure命令
#解压
tar xf nginx-1.22.1.tar.gz
cd nginx-1.22.1
#指定安装路径和模块
./configure --prefix=/usr/local/shengxia --user=shengxia --group=shengxia --with-http_ssl_module --with-threads --with-http_v2_module --with-http_stub_status_module --with-stream --with-http_gunzip_module
- 编译安装
#同时启动两个进程执行
make -j 2
#安装
make install
- 进入之间指定安装的目录,如果能发现以下文件夹,则说明安装成功
- 启动nginx,进入/usr/local/shengxia/sbin,执行命令
[root@localhost sbin]# pwd
/usr/local/shengxia/sbin
[root@localhost sbin]# ./nginx
- 查看nginx是否启动成功
a.看端口,看是是否有80端口的进程
[root@localhost sbin]# netstat -anplut
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4658/nginx: master
b.看进程
[root@localhost sbin]# ps aux | grep nginx
root 4658 0.0 0.2 46372 2008 ? Ss 22:16 0:00 nginx: master process /usr/local/shengxia/sbin/nginx
shengxia 4721 0.0 0.2 46812 2044 ? S 22:29 0:00 nginx: worker process
root 7096 0.0 0.0 112824 980 pts/0 R+ 23:39 0:00 grep --color=auto nginx
c.看日志
[root@localhost logs]# pwd
/usr/local/shengxia/logs
[root@localhost logs]# tail access.log
d.直接访问
9. 修改PATH环境变量,方便在任何目录下都能使用nginx命令,同时关闭防火墙和selinux服务
echo "PATH=$PATH:/usr/local/shengxia/sbin" >>/root/.bashrc
#当前终端执行修改环境变量的脚本
source /root/.bashrc
#关闭防火墙,设置开机不启动
service firewalld stop
systemctl disable firewalld
#临时停止selinux和永久停止selinux
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
- 设置开机启动
chmod +x /etc/rc.d/rc.local
echo "/usr/local/shengxia/sbin/nginx" >>/etc/rc.local
Nginx关闭、重新启动和卸载
关闭
- 使用kill命令强制杀死nginx进程
[root@localhost conf]# ps aux | grep nginx
root 4658 0.0 0.2 46372 2008 ? Ss 13:23 0:00 nginx: master process /usr/local/shengxia/sbin/nginx
shengxia 4721 0.0 0.2 46812 2284 ? S 13:37 0:00 nginx: worker process
root 8861 0.0 0.0 112824 980 pts/0 R+ 15:16 0:00 grep --color=auto nginx
[root@localhost conf]# kill -9 4658
- 使用nginx的-s参数关闭nginx,其实就是给nginx发送一个信号,然后执行
#快速关闭nginx
nginx -s stop
#优雅关闭nginx
nginx -s quit
重新启动
当修改nginx配置文件时,可以使用nginx -t
查看配置文件是否有错误,然后使用nginx -s reload
重新加载配置文件,使用nginx -s reopen
重新生成日志文件等。
卸载
卸载主要删除之前指定的--prefix安装路径
,然后删除写入PATH中的环境变量即可。