nginx使用的场景比较多,实践出真知,我们一起来看看如何使用。
本文收录于专栏【运维系列-架构与服务】专栏中的《linux基本功-系统服务实战篇》,一起学习,持续精进
官网地址:https://nginx.org/en/download.html

版本说明:
单词注解:
Mainline美[ˈmeɪnlaɪn] 主流的
legcy: 美[ˈleɡəsi] 遗留的,已停产的
下载:wget -c https://nginx.org/download/nginx-1.22.1.tar.gz
wget -c https://nginx.org/download/nginx-1.22.1.tar.gz
编译nginx可能会用到gcc, prce,zlib,opessl,这里使用yum进行安装。
[root@mufeng41 ~]# yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
root@mufeng41 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx-1.22.1.tar.gz 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@mufeng41 ~]# tar xf nginx-1.22.1.tar.gz
[root@mufeng41 ~]# cd nginx-1.22.1/
[root@mufeng41 nginx-1.22.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@mufeng41 nginx-1.22.1]#
[root@mufeng41 nginx-1.22.1]# ./configure --prefix=/usr/local/nginx
接下来使用make 和make install 进行安装
make && make install
到路径 /usr/local/nginx/sbin/下进行启动:
[root@mufeng41 nginx-1.22.1]# cd /usr/local/nginx/sbin/
[root@mufeng41 sbin]# ls
nginx
[root@mufeng41 sbin]# ./nginx
[root@mufeng41 sbin]#
其他操作:
[root@mufeng41 sbin]# ./nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@mufeng41 sbin]#
配置文件路径:
[root@mufeng41 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@mufeng41 nginx]# pwd
/usr/local/nginx
配置文件 vim /etc/profile
[root@mufeng41 sbin]# cat >>/etc/profile<< EOF
> export NGINX_HOME=/usr/local/nginx
> export PATH=$PATH:$NGINX_HOME/sbin
> EOF
刷新配置:
[root@mufeng41 sbin]# source /etc/profile
将Nginx设置为系统服务,创建文件:touch /lib/systemd/system/nginx.service,打开文件vim /lib/systemd/system/nginx.service,添加如下配置:
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
备注:
WantedBy=multi-user.target 修改为图形界面执行
注意:
设置开机自启:systemctl enable nginx
配置好后,就可以使用nginx命令进行管理了:
systemctl start nginx 启动服务
systemctl stop nginx 停止服务
systemctl restart nginx 重新启动服务
systemctl list-units --type=service 查看所有已启动的服务
systemctl status nginx 查看服务当前状态
systemctl enable nginx 设置开机自启动
systemctl disable nginx 停止开机自启动
以上就是编译安装nginx的全过程,希望对你有用。