CentOS 7 安装Nginx

8 篇文章 0 订阅
1 篇文章 0 订阅

CentOS 7 安装Nginx

1.安装nginx依赖包
安装gcc、gcc-c++

[root@cnetos7 xiongyingli]# yum install -y gcc gcc-c++

安装其它依赖包

[root@cnetos7 xiongyingli]#  yum install -y  make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.下载nginx安装包
获取nginx-1.19.4.tar.gz安装包

[root@cnetos7 local]# wget http://nginx.org/download/nginx-1.19.4.tar.gz

3.编译安装 Nginx
解压Nginx

[root@cnetos7 local]# tar -zxvf nginx-1.19.4.tar.gz

检查当前的环境是否满足要安装软件的依赖关系

[root@cnetos7 local]#  cd nginx-1.19.4/
[root@cnetos7 nginx-1.19.4]#  ./configure --prefix=/usr/local/nginx

make是用来编译的,它从Makefile中读取指令,然后编译。make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。

[root@cnetos7 nginx-1.19.4]# make && make install

4.配置 Nginx
备份nginx.conf文件

[root@cnetos7 nginx]# cd conf/
[root@cnetos7 conf]# cp nginx.conf nginx.conf.bak

覆盖nginx.conf文件

[root@cnetos7 nginx]#  vim /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
	worker_connections 1024;
}
http {
	include mime.types;
	default_type application/octet-stream;
	#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
	# '$status $body_bytes_sent "$http_referer" '
	# '"$http_user_agent" "$http_x_forwarded_for"';
	#access_log logs/access.log main;
	sendfile on;
	#tcp_nopush on;
	#keepalive_timeout 0;
	keepalive_timeout 65;
	#gzip on;
	server {
		listen 88;
		server_name localhost;
		#charset koi8-r;
		#access_log logs/host.access.log main;
		location / {
			root html;
			index index.html index.htm;
		}
		#error_page 404 /404.html;
		# redirect server error pages to the static page /50x.html
		error_page 500 502 503 504 /50x.html;
		location = /50x.html {
			root html;
		}
	}
}

5.设置nginx开机自启动
添加开机启动服务

[root@cnetos7 conf]# 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

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

加载提供某种服务的应用程序

[root@cnetos7 ~]# systemctl daemon-reload

6.服务的启动/停止/刷新配置文件/查看状态
启动nginx服务

[root@cnetos7 ~]# systemctl start nginx.service 

停止服务

[root@cnetos7 ~]# systemctl stop nginx.service 

重新启动服务

[root@cnetos7 ~]# systemctl restart nginx.service 

查看所有已启动的服务

[root@cnetos7 ~]# systemctl list-units --type=service

查看服务当前状态

[root@cnetos7 ~]# systemctl status nginx.service 

设置开机自启动

[root@cnetos7 ~]# systemctl enable nginx.service 

停止开机自启动

[root@cnetos7 ~]# systemctl disable nginx.service
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiongyingli

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值