centos7如何安装nginx

1.下载nginx

yum install wget -y && wget http://nginx.org/download/nginx-1.16.1.tar.gz

2.解压

tar -zxf nginx-1.16.1.tar.gz

3.切换到nginx解压目录下

cd nginx-1.16.1

4.创建nginx所属用户组

groupadd www

5.创建nginx所属用户

useradd -g www www -M -s /sbin/nologin

6.安装nginx依赖的软件,如果不安装编译的时候会报错。为了避免编译的时候报错,我们可以先安装一下这几个软件再进行编译
如果编译的时候报错:C compiler cc is not found,就安装一下gcc-c++

yum install gcc gcc-c++ -y

如果编译的时候报错:the HTTP rewrite module requires the PCRE library,就安装一下pcre-devel

yum install pcre-devel -y

如果编译的时候报错:SSL modules require the OpenSSL library,就安装一下openssl-devel

yum install openssl-devel -y

7.编译参数

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_sub_module --with-pcre

8.安装

make && make install

9.启动nginx

//启动
/usr/local/nginx/sbin/nginx
//重启
/usr/local/nginx/sbin/nginx -s reload
//停止
/usr/local/nginx/sbin/nginx -s stop

10.设置nginx开机自启动

在/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 stop
PrivateTmp=true
   
[Install]
WantedBy=multi-user.target

加入开机自启动

systemctl enable nginx

如果想关掉开机自启动

systemctl disable nginx

11.用systemctl命令管理nginx

//启动
systemctl start nginx
//重启
systemctl restart nginx
//停止
systemctl stop nginx

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: