创建安装目录
[root@localhost ~]# mkdir -p /appdata/nginx
安装依赖环境
[root@localhost nginx]# yum -y install wget gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
说明:gcc源码编译, pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式,zlib安装,nginx 使用zlib对http包的内容进行gzip,OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
从官网地址获取安装包:
https://nginx.org/en/download.html
右键复制链接地址,wget方式获取
[root@localhost ~]# wget -c https://nginx.org/download/nginx-1.18.0.tar.gz
解压
[root@localhost ~]# tar -zxvf nginx-1.18.0.tar.gz && cd nginx-1.18.0
新建log日志文件
[root@localhost nginx-1.18.0]# mkdir -p /applog/nginx/ && mkdir -p /var/temp/nginx/clien && touch /applog/nginx/error.log && touch /applog/nginx/access.log
添加配置安装
./configure \
--prefix=/appdata/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/applog/nginx/error.log \
--http-log-path=/applog/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-file-aio \
--with-http_realip_module
编译安装
[root@localhost nginx-1.18.0]# make && make install
切换目录
[root@localhost nginx-1.18.0]# cd /appdata/nginx/sbin
启动
[root@localhost sbin]# ./nginx
防火墙端口添加
[root@localhost sbin]#sudo firewall-cmd --add-port=80/tcp --permanent && sudo firewall-cmd --reload
访问服务器ip地址,出现以下信息
停止
[root@localhost sbin]# ./nginx -s stop
重新启动
[root@localhost sbin]# ./nginx -s reload
添加systemctl启动方式
[root@localhost sbin]# vi /lib/systemd/system/nginx.service
添加如下
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx/nginx.pid
ExecStart=/appdata/nginx/sbin/nginx -c /appdata/nginx/conf/nginx.conf
ExecStartPre=/appdata/nginx/sbin/nginx -t
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重载系统
[root@localhost sbin]# systemctl daemon-reload
添加开机自启动
[root@localhost sbin]# systemctl enable nginx
启动
[root@localhost sbin]# systemctl start nginx
重载配置
[root@localhost sbin]# systemctl reload nginx
关闭
[root@localhost sbin]# systemctl stop nginx