博客是我工作的好帮手,遇到困难就来博客找资料
分类:系统运维
2017-03-0514:07:48
Nginx作为一款优秀的WebServer软件同时也是一款优秀的负载均衡或前端反向代理、缓存服务软件
2.编译安装Nginx
(1)安装Nginx依赖函数库pcre
pcre为“perl兼容正则表达式”perlcompatibleregularexpresssions,安装其是为了使Nginx支持具备URI重写功能的rewrite模块,如果不安装Nginx将无法使用rewrite模块功能,但是该功能却十分有用和常用。
检查系统中是否有安装:
[root@leaf~]#rpm-qpcrepcre-devel
上面可以看到并没有安装使用yum方式安装如下:
[root@leaf~]#yuminstallpcrepcre-devel-y
......
Installed:
pcre-devel.x86_640:7.8-7.el6
Updated:
pcre.x86_640:7.8-7.el6
Complete!
安装完后检查一下是否已经成功安装:
pcre-7.8-7.el6.x86_64
pcre-devel-7.8-7.el6.x86_64
可以看到已经安装成功。
(2)安装Nginx依赖函数库openssl-devel
[root@leaf~]#rpm-qopensslopenssl-devel
openssl-1.0.1e-15.el6.x86_64
packageopenssl-develisnotinstalled
可以看到只是安装了opensslopenssl-devel还没有安装使用yum安装如下:
[root@leaf~]#yuminstall-yopenssl-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,下载方式如下:
[root@leaf~]#pwd
/root
[root@leaf~]#mkdirtools
[root@leaf~]#cdtools/
[root@leaftools]#wget
100%[======================================>]805,253220K/sin3.6s
2017-02-2412:10:26(220KB/s)-anginx-1.6.3.tar.gzasaved[805253/805253]
查看下载的Nginx软件包:
[root@leaftools]#ll
total788
-rw-r--r--.1rootroot805253Apr82015nginx-1.6.3.tar.gz
当然上面的方式是使用wget方式直接下载,前提是已经知道了Nginx的下载地址,也可以到官网下载,然后再上传到我们的CentOS操作系统上。
(4)开始安装Nginx
可以先在根目录下创建一个/application文件夹用来存放我们安装的软件:
[root@leaf~]#mkdir/application
[root@leaf~]#ls-d/application/
/application/
解压缩
将我们刚刚下载的Nginx软件包解压缩:
[root@leaftools]#tar-zxvfnginx-1.6.3.tar.gz
[root@leaftools]#ls
nginx-1.6.3nginx-1.6.3.tar.gz
使用./configure指定编译参数
先创建一个nginx用户用来安装完成后运行nginx使用:
[root@leaftools]#useraddnginx-s/sbin/nologin-M
[root@leaftools]#tail-1/etc/passwd
nginx:x:500:500::/home/nginx:/sbin/nologin
#-s参数后的/sbin/nologin指定不允许nginx进行登陆
#-M参数则是在创建该用户时不创建用户家目录
使用configure命令指定编译参数:
对于配置时使用的参数可以通过./configure--help来进行查询,上面使用的参数解析如下:
--prefix=PATH#指定安装路径
--user=USER#设置用户进程权限
--group=GROUP#设置用户组进程权限
使用make进行编译
[root@leafnginx-1.6.3]#make
检查编译是否成功:
[root@leafnginx-1.6.3]#echo$
0
返回0即说明编译成功。
使用makeinstall安装
[root@leafnginx-1.6.3]#makeinstall
检查安装是否成功:
返回0即说明安装成功。
建立安装目录的软链接
[root@leafnginx-1.6.3]#ln-s/application/nginx-1.6.3//application/nginx
[root@leafnginx-1.6.3]#ls-l/application/
total4
lrwxrwxrwx.1rootroot25Feb2412:32nginx->/application/nginx-1.6.3/
drwxr-xr-x.6rootroot4096Feb2412:28nginx-1.6.3
到此Nginx的编译安装工作已经全部完成了,下面就需要对安装结果进行验证了即验证Nginx是否可以正常提供服务。
3.测试Nginx服务
(1)启动Nginx服务前检查配置文件语法
如下:
[root@leaf~]#/application/nginx/sbin/nginx-t
nginx:theconfigurationfile/application/nginx-1.6.3//conf/nginx.confsyntaxisok
nginx:configurationfile/application/nginx-1.6.3//conf/nginx.conftestissuccessful
(2)启动Nginx服务
[root@leaf~]#/application/nginx/sbin/nginx
如果在启动Nginx服务时出现了问题可以查看Nginx的日志/application/nginx/logs/error.log,再根据日志提供的信息来进行解决。
(3)验证Nginx服务是否正常
查看已开启的端口信息
[root@leaf~]#netstat-lnp|grep80
tcp000.0.0.0:800.0.0.0:*LISTEN6772/nginx
unix2[ACC]STREAMLISTENING91801/init@/com/ubuntu/upstart
可以看到Nginx已经在侦听80端口。
查看Nginx进程
[root@leaf~]#psaux|grepnginx
root67720.00.1450281140Ss12:340:00nginx:masterprocess/application/nginx/sbin/nginx
nginx67730.00.1454601716S12:340:00nginx:workerprocess
root67770.00.0103256832pts/1S+12:360:00grepnginx
在宿主机上使用浏览器进行测试
在我们宿主机的浏览器上输入,查看测试结果
可以正常访问,当然前提是CentOS上的防火墙功能已经关闭。
使用wget命令和curl命令测试
wget命令:
[root@leaftools]#wget127.0.0.1
--2017-02-2412:41:05--
Connectingto127.0.0.1:80...connected.
HTTPrequestsent,awaitingresponse...200OK
Length:612[text/html]
Savingto:aindex.htmla
100%[======================================>]612--.-K/sin0s
2017-02-2412:41:05(44.1MB/s)-aindex.htmlasaved[612/612]
currl命令:
[root@leaftools]#curl127.0.0.1
body{
width:35em;
margin:0auto;
font-family:Tahoma,Verdana,Arial,sans-serif;
}
Welcometonginx!
Ifyouseethispage,thenginxwebserverissuccessfullyinstalledand