Linux CentOS 7.9 安装 Nginx "1.24.0"

226 阅读2分钟

Nginx是什么?

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。

官网

nginx.org/en/download…

image.png

下载安装

##创建nginx安装目录
[root@lavm-kixcjxxxx opt]# mkdir nginx
##进入目录
[root@lavm-kixcjxxxx opt]# cd nginx
##下载nginx
[root@lavm-kixcjxxxx nginx]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
##解压到当前目录
    [root@lavm-kixcjxxxx nginx]# tar -zxvf nginx-1.24.0.tar.gz
[root@lavm-kixcjxxxx nginx]# ls
nginx-1.24.0  nginx-1.24.0.tar.gz
[root@lavm-kixcjxxxx nginx]# cd nginx-1.24.0
[root@lavm-kixcjxxxx nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
##配置configure 
##--prefix 代表安装的路径
##--with-http_ssl_module 安装ssl
##--with-http_stub_status_module查看nginx的客户端状态
[root@lavm-kixcjxxxx nginx-1.24.0]# ./configure --prefix=/opt/web/nginx --with-http_ssl_module --with-http_stub_status_module
##编译安装nginx
[root@lavm-kixcjxxxx nginx-1.24.0]# make & make install 
##进入nginx安装目录
[root@lavm-kixcjxxxx nginx-1.24.0]# cd /opt/web/nginx

Nginx启停

#启动脚本所在目录
# /opt/web/nginx/sbin/nginx
#指定配置文件启动
/opt/web/nginx/sbin/nginx -c /opt/web/nginx/conf/nginx.conf
#停止
/opt/web/nginx/sbin/nginx -s stop
#重载
/opt/web/nginx/sbin/nginx -s reload
#杀掉nginx
/opt/web/nginx/nginx -s quit

## 检查是否启动成功
[root@lavm-kixcjxxxx nginx]# ps -ef | grep nginx
root      2486     1  0 23:36 ?        00:00:00 nginx: master process /opt/web/nginx/sbin/nginx -c /opt/web/nginx/conf/nginx.conf
nobody    2487  2486  0 23:36 ?        00:00:00 nginx: worker process

访问Nginx

117.72.xx.xx:80

image.png

Nginx配置环境变量

1.vi ~/.bashrc
2.export PATH=$PATH:/opt/web/nginx/sbin
3.source ~/.bashrc

配置Nginx开机自启动

进入配置文件

sudo vim /etc/systemd/system/nginx.service

写入以下内容,nginx目录需要替换成你自己的

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/web/nginx/logs/nginx.pid
ExecStartPre=/opt/web/nginx/sbin/nginx -t
ExecStart=/opt/web/nginx/sbin/nginx
ExecReload=/opt/web/nginx/sbin/nginx -s reload
ExecStop=/opt/web/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

# esc -> :wq! 保存退出
#重新加载 Systemd 配置以使新的服务文件生效:
[root@lavm-kixcjxxxx conf]# sudo systemctl daemon-reload
#启用 Nginx 开机自启动:
[root@lavm-kixcjxxxx conf]# sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /etc/systemd/system/nginx.service.