1、下载
nginx官网:https://nginx.org/en/download.html
2、依赖安装
1> 安装gcc环境
编译时依赖gcc环境
yum -y install gcc gcc-c++ autoconf automake make
2> 安装 pcre
提供nginx支持重写功能
yum -y install pcre pcre-devel
3> 安装zlib
zlib 库提供了很多压缩和解压缩的方式,nginx 使用 zlib 对 http 包内容进行 gzip 压缩
yum -y install zlib zlib-devel make libtool
4> 安装openssl
安全套接字层密码库,用于通信加密
yum -y install openssl openssl-devel
3、nginx安装
1> 创建用户和用户组
groupadd nginx
useradd nginx -g nginx -s /sbin/nologin -M
2> 解压
将nginx解压在/usr/local/目录下
[root@localhost tools]# tar -xvf nginx-1.23.1.tar.gz -C /usr/local/
3> 编译目录
切换到源码目录下
cd /usr/local/nginx-1.23.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx
参数说明:
#编译安装目录
–prefix=/usr/local/nginx
#所属用户nginx
–user=nginx
#所属组nginx
–group=nginx
#该模块提供nginx的基本状态信息
–with-http_stub_status_module
# 支持HTTPS
–with-http_ssl_module
4> 编译并安装
make && make install
5> 修改配置
[root@localhost local]# cd nginx/conf/
[root@localhost conf]# vim nginx.conf
user nginx nginx;
worker_processes auto;
6> 启动nginx服务
启动服务
/usr/local/nginx/sbin/nginx
重启服务
/usr/local/nginx/sbin/nginx -s reload
停止服务
/usr/local/nginx/sbin/nginx -s stop
查看进程
ps aux|grep nginx
5、配置环境变量
编辑profile文件
vim /etc/profile
在文件末尾添加
export NGINX_HOME=/usr/local/nginx/sbin
export PATH=$PATH:$NGINX_HOME
重新编译profile文件
source /etc/profile
验证是否配置成功
6、设置开机自启动
配置服务文件
vim /lib/systemd/system/nginx.service
配置信息
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
创建服务
systemctl enable nginx.service
查看nginx状态
systemctl status nginx
启动nginx
systemctl start nginx
停止nginx
systemctl stop nginx