在CentOS上编译安装Nginx+实验环境搭建+测试

0.说明

Nginx作为一款优秀的Web Server软件同时也是一款优秀的负载均衡或前端反向代理、缓存服务软件,很有必要搭建实验环境来对其进行学习。



1.实验环境

本次实验的测试环境使用的宿主机操作系统为Windows 7,在Vmware虚拟机安装CentOS 6.5,说明如下:

  • 宿主机操作系统Windows 7
  • 虚拟机安装的操作系统CentOS 6.5
  • 虚拟机操作系统上网方式NAT

而当使用NAT的方式进行上网时虚拟机、宿主机之间的网络连接关系可如下所示:

关于为什么网络拓扑结构是这样的,这里不展开说明,可以参考博主的另一篇博文《在实践中深入理解VMware虚拟机的上网模式NAT模式》,这篇文章深入地分析了VMware虚拟机使用NAT模式上网时的网络结构细节,相信看完这篇文章后,这里搭建Nginx的实验环境也就很容易理解了。

另外需要注意的是这里安装的CentOS 6.5操作系统使用了最小化安装,并且只定制安装了一些常用的开发工具如gcc等,其版本信息如下:

[[email protected] ~]# cat /etc/redhat-release 
CentOS release 6.5 (Final)
[[email protected] ~]# uname -r
2.6.32-431.el6.x86_64
[[email protected] ~]# uname -m
x86_64


2.编译安装Nginx

(1)安装Nginx依赖函数库pcre

pcre为“perl兼容正则表达式”perl compatible regular expresssions,安装其是为了使Nginx支持具备URI重写功能的rewrite模块,如果不安装Nginx将无法使用rewrite模块功能,但是该功能却十分有用和常用。

检查系统中是否有安装:

[[email protected] ~]# rpm -q pcre pcre-devel

上面可以看到并没有安装使用yum方式安装如下:

[[email protected] ~]# yum install pcre pcre-devel -y
......

Installed:
  pcre-devel.x86_64 0:7.8-7.el6                                                 

Updated:
  pcre.x86_64 0:7.8-7.el6                                                       

Complete!

安装完后检查一下是否已经成功安装:

[[email protected] ~]# rpm -q pcre pcre-devel
pcre-7.8-7.el6.x86_64
pcre-devel-7.8-7.el6.x86_64

可以看到已经安装成功。

(2)安装Nginx依赖函数库openssl-devel

Nginx在使用HTTPS服务的时候要用到此模块,如果不安装openssl相关包,安装过程中是会报错的。

检查系统是否有安装openssl相关包:

[[email protected] ~]# rpm -q openssl openssl-devel 
openssl-1.0.1e-15.el6.x86_64
package openssl-devel is not installed

可以看到只是安装了opensslopenssl-devel还没有安装使用yum安装如下:

[[email protected] ~]# yum install -y openssl-devel
......

Complete!

再次检查:

[[email protected] ~]# rpm -q openssl openssl-devel          
openssl-1.0.1e-48.el6_8.4.x86_64
openssl-devel-1.0.1e-48.el6_8.4.x86_64

可以看到都已经成功安装上。

(3)下载Nginx软件包

这里使用的Nginx版本为1.6.3,下载方式如下:

[[email protected] ~]# pwd
/root
[[email protected] ~]# mkdir tools
[[email protected] ~]# cd tools/
[[email protected] tools]# wget http://nginx.org/download/nginx-1.6.3.tar.gz
......
100%[======================================>] 805,253      220K/s   in 3.6s    

2017-02-24 12:10:26 (220 KB/s) - anginx-1.6.3.tar.gza saved [805253/805253]

查看下载的Nginx软件包:

[[email protected] tools]# ll
total 788
-rw-r--r--. 1 root root 805253 Apr  8  2015 nginx-1.6.3.tar.gz

当然上面的方式是使用wget方式直接下载,前提是已经知道了Nginx的下载地址,也可以到官网下载,然后再上传到我们的CentOS操作系统上。

(4)开始安装Nginx

可以先在根目录下创建一个/application文件夹用来存放我们安装的软件:

[[email protected] ~]# mkdir /application
[[email protected] ~]# ls -d /application/
/application/
  • 解压缩

将我们刚刚下载的Nginx软件包解压缩:

[[email protected] tools]# tar -zxvf nginx-1.6.3.tar.gz
......
[[email protected] tools]# ls
nginx-1.6.3  nginx-1.6.3.tar.gz
  • 使用./configure指定编译参数

先创建一个nginx用户用来安装完成后运行nginx使用:

[[email protected] tools]# useradd nginx -s /sbin/nologin -M
[[email protected] tools]# tail -1 /etc/passwd
nginx:x:500:500::/home/nginx:/sbin/nologin

# -s参数后的/sbin/nologin指定不允许nginx进行登陆
# -M参数则是在创建该用户时不创建用户家目录

使用configure命令指定编译参数:

[[email protected] nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module

对于配置时使用的参数可以通过./configure --help来进行查询,上面使用的参数解析如下:

--prefix=PATH       # 指定安装路径
--user=USER         # 设置用户进程权限
--group=GROUP       # 设置用户组进程权限
--with-http_stub_status_module  #  激活状态信息
--with-http_ssl_module          #  激活ssl功能
  • 使用make进行编译
[[email protected] nginx-1.6.3]# make
......

检查编译是否成功:

[[email protected] nginx-1.6.3]# echo $?
0

返回0即说明编译成功。

  • 使用make install安装
[[email protected] nginx-1.6.3]# make install
......

检查安装是否成功:

[[email protected] nginx-1.6.3]# echo $?     
0

返回0即说明安装成功。

  • 建立安装目录的软链接
[[email protected] nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx
[[email protected] nginx-1.6.3]# ls -l /application/
total 4
lrwxrwxrwx. 1 root root   25 Feb 24 12:32 nginx -> /application/nginx-1.6.3/
drwxr-xr-x. 6 root root 4096 Feb 24 12:28 nginx-1.6.3

到此Nginx的编译安装工作已经全部完成了,下面就需要对安装结果进行验证了即验证Nginx是否可以正常提供服务。



3.测试Nginx服务

(1)启动Nginx服务前检查配置文件语法

如下:

[[email protected] ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful

(2)启动Nginx服务

[[email protected] ~]# /application/nginx/sbin/nginx

如果在启动Nginx服务时出现了问题可以查看Nginx的日志/application/nginx/logs/error.log,再根据日志提供的信息来进行解决。

(3)验证Nginx服务是否正常

  • 查看已开启的端口信息
[[email protected] ~]# netstat -lnp | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      6772/nginx          
unix  2      [ ACC ]     STREAM     LISTENING     9180   1/init              @/com/ubuntu/upstart

可以看到Nginx已经在侦听80端口。

  • 查看Nginx进程
[[email protected] ~]# ps aux | grep nginx
root       6772  0.0  0.1  45028  1140 ?        Ss   12:34   0:00 nginx: master process /application/nginx/sbin/nginx
nginx      6773  0.0  0.1  45460  1716 ?        S    12:34   0:00 nginx: worker process        
root       6777  0.0  0.0 103256   832 pts/1    S+   12:36   0:00 grep nginx
  • 在宿主机上使用浏览器进行测试

在我们宿主机的浏览器上输入http://10.0.0.101/,查看测试结果

可以正常访问,当然前提是CentOS上的防火墙功能已经关闭。

  • 使用wget命令和curl命令测试

wget命令:

[[email protected] tools]# wget 127.0.0.1
--2017-02-24 12:41:05--  http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: aindex.htmla

100%[======================================>] 612         --.-K/s   in 0s      

2017-02-24 12:41:05 (44.1 MB/s) - aindex.htmla saved [612/612]

currl命令:

[[email protected] tools]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

从上面的结果可以说明Nginx已经正常部署并运行。



4.进一步测试修改Nginx显示的页面

通过修改/application/nginx/html下的index.html文件,我们就可以改变Nginx主页显示的内容,操作如下:

[[email protected] tools]# cd /application/nginx/html/
[[email protected] html]# ls
50x.html  index.html
[[email protected] html]# mv index.html index.html.source
[[email protected] html]# echo "<h1>Hello, I‘m xpleaf.</h1>">index.html
[[email protected] html]# ls
50x.html  index.html  index.html.source
[[email protected] html]# cat index.html
<h1>Hello, I‘m xpleaf.</h1>

这时在宿主机操作系统上访问http://10.0.0.101/

可以看到已经显示我们编辑的页面了。



5.在实际场景中的应用

不管是用于学习还是在生产环境中使用,Nginx都十分重要,而好的开始是成功的一半,所以第一步当然是要把Nginx服务搭建好。



6.参考资料

《跟老男孩学Linux运维Web集群实战》

http://nginx.org

时间: 2024-08-01 16:43:55

在CentOS上编译安装Nginx+实验环境搭建+测试的相关文章

在Centos上编译安装nginx

实验环境: OS: CentOS 6.6 nginx:nginx-1.6.2.tar.gz 前期准备:安装开发包组件[[email protected] tmp]# yum -y groupinstall "Development tools" "Server Platform Development"[[email protected] tmp]# yum -y install pcre-devel 一. 编译安装: [[email protected] tmp]

Nginx配置多个基于域名的虚拟主机+实验环境搭建+测试

标签:Linux 域名 Nginx 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xpleaf.blog.51cto.com/9315560/1901284 0.说明 使用Nginx可以配置基于域名的虚拟主机.基于端口的虚拟主机和基于端口的虚拟主机,比较常用的是基于域名的虚拟主机,这里要做的配置是基于域名的虚拟主机,并且是配置多个基于域名的虚拟主机. 关于Nginx配置文件的说明可以参考官方文档,同时也可以参考老男孩老师的书

CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2

已测试环境: CentOS 6.3 32bit CentOS 6.5 64bit 曾经在CentOS 6.3 32bit安装过OpenCV,参见CentOS 6.3中安装OpenCV2.3.1,如今换了64bit系统,大刀阔斧,重新来一遍. 检查并安装相关程序,确保gtk安装成功,否则无法显示图片 yum install gcc-c++ # g++编译 yum install gtk-devel # 反正是gtk神马的,不一定就是gtk-devel,可以使用*gtk-devel*匹配 yum i

【apache http server安装】CentOS上编译安装Aapche Http Server详细过程

下载apache httpd # wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.4.10.tar.gz 2. 解压 apache httpd # tar xzvf httpd-2.4.10.tar.gz 编译apache httpd [[email protected]]# ./configure checkingfor chosen layout... Apache checkingfor working mkdir -p... yes

centos 7编译安装nginx

禁用防火墙 systemctl disable firewalld systemctl stop firewalld setenforce 0 安装pcre库 yum install pcre* 安装zlib库 yum install zlib* 增加nginx用户:useradd nginx -G nginx 编译安装nginx: ./configure --prefix=/usr/local/nginx --error-log-path=/var/log/nginx/error.log --

CentOS下编译安装Nginx

1.什么是Nginx Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用. 其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪.网易. 腾讯,另外知名的微网志Plurk也使用

Linux的CentOS上如何安装nginx

1. 安装nginx前,首先要装好gcc和g++环境: 2. 在centOS上装nginx,需要PCRE.zlib和ssl的支持,出ssl外其他都需要从其官网上下载好,上传至服务器: 3. 接着将上传的PCRE.zlib.nginx包释放,安装ssl: tar -xvzf pcre-8.3.8.tar.gz tar -xvzf zlib-1.2.8.tar.gz tar -xvzf nginx-1.9.8.tar.gz yum -y install openssl -devel 4. 进入ngi

在CentOS上编译安装MySQL+安装问题解决+安全优化

0.说明 当然,MySQL的安装方法多种多样,在CentOS上,你可以采用YUM的方式安装,这样的好处是:快速方便.基本上,它会帮你解决所有的函数库依赖问题,正常情况下,只要YUM执行完成,那么MySQL也就可以使用了. 但我更倾向于使用编译的方式来安装MySQL,原因也很简单:除了有详细的官方文档外,你还可以非常清楚地知道你自己在做什么,这点在以后MySQL运行出现问题时将会有很大的帮助! 但即便是按照官方文档来安装,你也会遇到各种各样的问题,这里,我将呈现一个完整的过程给大家,直到完成下面的

CentOS 7编译安装Nginx+MySQL+PHP

一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动 2.安装iptables防火墙 yum install iptables-services #安装 vi /etc/sysconfig/ip