Centos7安装nginx
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7安装nginx相关的知识,希望对你有一定的参考价值。
1. Nginx简介
nginx (发音为[engine x])专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对并发连接的高处理能力(单台物理服务器可支持30000~50000个并发连接), 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服。
2. 安装准备
2.1 gcc安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
[[email protected] ~]# yum -y install gcc-c++
2.2 pcre安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
[[email protected] ~]# yum -y install pcre pcre-devel
2.3 zlib安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
[[email protected] ~]# yum -y install zlib zlib-devel
2.4 OpenSSL安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
[[email protected] ~]# yum -y install openssl openssl-devel
3. Nginx安装
3.1 Nginx版本
下载网址:https://nginx.org/en/download.html
选择最新的稳定版nginx-1.12.2
版本说明:
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
Stable version:最新稳定版,生产环境上建议使用的版本
Legacy versions:遗留的老版本的稳定版
3.2 Nginx下载
使用wget命令下载
[[email protected] ~]# wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
如没有wget命令则安装:
[[email protected] ~]# yum -y install wget
3.3 解压
[[email protected] ~]# tar -zxvf nginx-1.12.2.tar.gz
3.4 安装配置
3.4.1 新建nginx用户和组
[[email protected] include]# groupadd nginx [[email protected] include]# useradd -g nginx -d /home/nginx nginx [[email protected] include]# passwd nginx
3.4.2 配置
默认配置(推荐):
[[email protected] nginx-1.12.2]# ./configure
指定用户、路径和模块配置:
./configure --user=nginx --group=nginx \ #安装的用户组 --prefix=/usr/local/nginx \ #指定安装路径 --with-http_stub_status_module \ #监控nginx状态,需在nginx.conf配置 --with-http_ssl_module \ #支持HTTPS --with-http_sub_module \ #支持URL重定向 --with-http_gzip_static_module #静态压缩
3.5 编译
[[email protected] nginx-1.12.2]# make && make install
3.6 nginx命令全局执行设置
[[email protected] bin]# cd /usr/local/nginx/sbin/ [[email protected] sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
4. Nginx相关命令
4.1 版本查看
[[email protected] ~]# nginx -v nginx version: nginx/1.12.2
4.2 查看加载的模块
[[email protected] ~]# nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --user=nginx --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module
4.3 启停命令
4.3.1 启动
[[email protected] nginx-1.12.2]# nginx
4.3.2 停止
[[email protected] nginx-1.12.2]# nginx -s stop [[email protected] nginx-1.12.2]# nginx -s quit
4.3.3 动态加载
[[email protected] nginx-1.12.2]# ngins -s reload
4.3.4 测试配置文件nginx.conf正确性
[[email protected] ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
nginx -s reload:动态加载,当配置文件nginx.conf有变化时执行该命令动态加载。
4.4 开机自启动
编辑/etc/rc.d/rc.local文件,新增一行/usr/local/nginx/sbin/nginx
[[email protected] rc.d]# cd /etc/rc.d [[email protected] rc.d]# view /etc/rc.d/rc.local /usr/local/nginx/sbin/nginx [[email protected] rc.d]# chmod u+x rc.local
5. 更改默认端口
编辑配置文件/usr/local/nginx/conf/nginx.conf,将默认端口80修改为81:
[[email protected] ~]# view /usr/local/nginx/conf/nginx.conf
加载配置:
[[email protected] ~]# nginx -s reload
6. 访问Nginx
6.1 关闭防火墙
[[email protected] ~]# firewall-cmd --state running [[email protected] ~]# systemctl stop firewalld.service [[email protected] ~]# firewall-cmd --state not running
6.2 访问Nginx
以上是关于Centos7安装nginx的主要内容,如果未能解决你的问题,请参考以下文章