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