Nginx之Centos7下安装

一. gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install -y gcc-c++

二. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,

所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

三. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

四. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

汇总安装命令

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

官网下载地址

https://nginx.org/en/download.html

使用wget命令下载(推荐)。确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装

wget https://nginx.org/download/nginx-1.12.0.tar.gz  -P /usr/local/software/

cd /usr/local/software/

tar -zxvf nginx-1.12.0.tar.gz

cd nginx-1.12.0

使用默认配置

./configure

编译和安装

./make.sh

./make.sh install

查找安装路径

whereis nginx

启动 停止 重启nginx

cd /usr/local/nginx/sbin/

./nginx 

./nginx -s stop

./nginx -s quit

./nginx -s reload

新建开机自启动服务

vim /lib/systemd/system/nginx.service

内容为:

#!/bin/sh

[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 stop

PrivateTmp=true  

[Install]  

WantedBy=multi-user.target

设置开机启动

systemctl enable nginx 

取消开机启动

systemctl disable nginx 

启动

systemctl start nginx 

停止

systemctl stop nginx 

重启

systemctl restart nginx 

重新载入配置

systemctl reload nginx 

重装nginx服务,在启动的时候报80端口被占用了

首先我们查一下占用80端口的有哪些服务,

netstat -lnp|grep 80 

我们会发现其实就是nginx自己占用了80端口

服务名称前面是他的pid号

kill -9 pid号

再重启

评论

暂无评论