Skip to content
章节导航

Linux 安装 Nginx

环境

gcc 安装

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

shell
yum install gcc-c++

PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。

shell
yum install -y pcre pcre-devel

zlib 安装

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

shell
yum install -y zlib zlib-devel

OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

shell
yum install -y openssl openssl-devel

安装 Nginx

上传并解压

上传至对应文件目录

解压

shell
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0

配置(默认配置)

默认配置命令:./configure
添加 https 模块 ( 安全组需要打开 80 和 443 端口 ):

shell
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

编译安装

shell
make
make install

查找安装路径:

shell
whereis nginx

启动、停止nginx( 根据第八步,查看得到安装路径,假如安装路径为: /usr/local/nginx/sbin/ )

shell
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload

启动时报80端口被占用: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 解决办法:安装net-tool 包:

shell
yum install net-tools

开机自启动

即在rc.local增加启动代码就可以了。

shell
vi /etc/rc.local

增加一行

shell
/usr/local/nginx/sbin/nginx

设置执行权限:

shell
chmod 755 rc.local

添加 ngx_http_ssl_module 模块

查看已有模板

shell
/usr/local/nginx/sbin/nginx -V

进入 nginx 安装目录:

shell
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

完成之后执行:

shell
make

然后备份原有已安装好的nginx

shell
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

卸载 Nginx

检查nginx服务启动情况

  • 查看nginx服务情况
shell
ps -ef | grep nginx
  • 关闭服务
shell
/usr/local/nginx/sbin/nginx -s quit

如果服务加入了自启动,则删除服务

shell
chkconfig nginx off

删除nginx文件

  • 使用find命令搜索文件
shell
find / -name nginx
  • 按照返回删除目录
shell
rm -rf 文件

如果使用yum按照的则使用命令删除依赖

shell
yum remove nginx