CentOS7.6安装Nginx并配置自动启动

CentOS7.6安装Nginx并配置自动启动

 

1、官网下载安装包
http://nginx.org/en/download.html,选择适合Linux的版本,这里选择最新的版本,下载到本地后上传到服务器或者centos下直接wget命令下载。
[root@localhost /]# cd /usr/
[root@localhost /]# wget http://nginx.org/download/nginx-1.18.0.tar.gz

 

2、安装nginx
先执行以下命令,安装nginx依赖库,如果缺少依赖库,可能会安装失败,具体可以参考文章后面的错误提示信息。

[root@localhost /]# yum -y install gcc-c++
[root@localhost /]# yum -y install pcre
[root@localhost /]# yum -y install pcre-devel
[root@localhost /]# yum -y install zlib
[root@localhost /]# yum -y install zlib-devel
[root@localhost /]# yum -y install openssl
[root@localhost /]# yum -y install openssl-devel

#也可以直接一条命令代替
[root@localhost /]# yum -y install gcc-c++ pcre-devel zlib zlib-devel openssl openssl-devel

 

3、解压nginx安装包,并安装
[root@localhost usr]# tar -zxvf nginx-1.18.0.tar.gz
[root@localhost usr]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]./configure
#不报错误的话继续下面命令
[root@localhost nginx-1.18.0]make
[root@localhost nginx-1.18.0]make install
#没有出错的话,表示nginx已经成功安装完成,默认安装位置为/usr/local/nginx,之前的/usr/nginx-1.18.0/可以删除掉了。

如果出现cp: ‘conf/koi-win‘ and ‘/usr/local/nginx/conf/koi-win‘ are the same file,可能是你把安装包解压到了/usr/local/nginx目录,解决办法是将该目录重命名为其他名称后再执行make,make install.

 

4、配置nginx开机启动
切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service

[root@localhost /]# cd /lib/systemd/system/
[root@localhost system]# vim nginx.service

文件内容如下:
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

退出并保存文件

CentOS7.6安装Nginx并配置自动启动

 

 

[root@localhost system]# systemctl enable nginx.service

执行systemctl enable nginx.service使nginx开机启动

 

systemctl start nginx.service 启动nginx

systemctl stop nginx.service 结束nginx

systemctl restart nginx.service 重启nginx

systemctl status nginx.service 查看nginx状态


输入http://服务器IP/ 如果能看到nginx的界面,就表示安装成功了。记得先关防火墙哦。

CentOS7.6安装Nginx并配置自动启动

上一篇:linux


下一篇:ArrayList和LinkedList