nginx在Centos7.5下源码安装和配置

时间:2023-03-09 02:47:00
nginx在Centos7.5下源码安装和配置

安装nginx

安装nginx依赖包

yum install -y pcre-devel zlib-devel openssl-devel wget gcc tree vim

进入目录/root/setup

cd /root/setup

下载nginx安装包到/root/setup目录

wget http://nginx.org/download/nginx-1.14.2.tar.gz

解压nginx安装包

tar -xzvf nginx-1.14.2.tar.gz

在/root/setup/nginx目录进行编译配置nginx

./configure --prefix=/usr/local/nginx --with-http_ssl_module

  参数说明:

    --prefix 指定nginx编译后的安装目录

    --with-module_name 表示启用的nginx模块,如此处启用了http_ssl_module模块

在/root/setup/nginx目录进行编译安装nginx

make && make install

启动nginx

/usr/local/nginx/sbin/nginx

指定nginx.conf启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf;

查看nginx是否启动

ps aux|grep nginx

nginx在Centos7.5下源码安装和配置

查看Nginx占用的端口号

netstat -tlnp

nginx在Centos7.5下源码安装和配置

关闭nginx

# . 立即停止Nginx服务
/usr/local/nginx/sbin/nginx -s stop # .完成当前任务后停止
/usr/local/nginx/sbin/nginx -s quit # .杀死Nginx进程
killall nginx 

开机启动nginx

进入 /etc/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 -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true [Install]
WantedBy=multi-user.target

说明:

 [Service] 里的ExecStart内容,用来指定需要启动nginx的执行文件地址;

 [Service] 里的ExecStop内容,用来指定需要停止nginx的执行文件地址;

 [Service] 里的ExecReload内容,用来指定需要重新加载nginx的执行文件地址;

文件编辑完成后,文件名称就是服务名称

配置服务

#刷新服务配置文件
sudo systemctl daemon-reload #设置开机启动:
sudo systemctl enable nginx.service #查询是否开机启动nginx服务
sudo systemctl is-enable nginx.service #禁止开机启动nginx服务
systemctl disable nginx.service #启动服务
sudo systemctl start nginx.service #停止服务
sudo systemctl stop nginx.service #重新加载nginx服务
sudo systemctl reload nginx.service #查看服务状态
systemctl status nginx.service

参数说明:

[Unit]:服务的说明
Description:描述服务
After:描述服务类别 [Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]服务安装的相关设置,可设置为多用户
 

参考文章

Centos7 Nginx开机启动

Linux下源码安装并配置Nginx

手动编译安装nginx