闲来无聊,学一下nginx的配置,简单的列出目录的文件,提供文件的下载
查看系统版本
[root@localhost nginx-1.7.9]# cat /etc/centos-release
CentOS release 6.5 (Final)
源码解压,-C指定解压的目录
[root@localhost nginx-1.7.9]tar -xvf nginx-1.7.9.tar.gx -C /opt/
[root@localhost nginx-1.7.9]cd /opt/nginx-1.7.9
新建一个目录,注备用来安装nginx
[root@localhost nginx-1.7.9]# mkdir /usr/local/nginx -p
[root@localhost nginx-1.7.9]#
配置,注意这里的进入了源码解压的目录的,用--prefix指定安装目录,需要指定别的选择可以用--help来查看
[root@localhost nginx-1.7.9]# ./configure --prefix=/usr/local/nginx/
checking for getaddrinfo() ... found
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
出错,提示没有PCRE 库
[root@localhost nginx]# yum list | grep pcre
pcre.x86_64 7.8-6.el6 @anaconda-CentOS-201311272149.x86_64/6.5
pcre-devel.x86_64 7.8-6.el6 @base
ghc-pcre-light.x86_64 0.4-7.el6 epel
ghc-pcre-light-devel.x86_64 0.4-7.el6 epel
pcre.i686 7.8-6.el6 base
pcre-devel.i686 7.8-6.el6 base
pcre-static.x86_64 7.8-6.el6 base
[root@localhost nginx]#
一般这样列出来的,我们就安装提示需要的那个包或者是那个包的devel版本的
[root@localhost nginx-1.7.9]# yum -y install pcre-devel
安装好PCRE库后,再次执行配置命令
[root@localhost nginx-1.7.9]# ./configure --prefix=/usr/local/nginx/
checking for sha1 in system md library ... not found
checking for sha1 in system OpenSSL crypto library ... not found
checking for zlib library ... not found
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
又有提示需要zlib库
[root@localhost nginx-1.7.9]# yum list | grep gzip
gzip.x86_64 1.3.12-19.el6_4 @anaconda-CentOS-201311272149.x86_64/6.5
gzip.x86_64 1.3.12-22.el6 base
perl-PerlIO-gzip.x86_64 0.18-6.el6 epel
python-gzipstream.noarch 1.4.3-1.el6 epel
[root@localhost nginx-1.7.9]#
[root@localhost nginx-1.7.9]#
[root@localhost nginx-1.7.9]# yum -y install gzip
[root@localhost nginx-1.7.9]# ./configure --prefix=/usr/local/nginx/
checking for sha1 in system OpenSSL crypto library ... not found
checking for zlib library ... not found
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
[root@localhost nginx-1.7.9]# yum -y install zlib
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* epel: mirrors.123host.vn
* extras: mirrors.163.com
* updates: mirrors.163.com
Setting up Install Process
Package zlib-1.2.3-29.el6.x86_64 already installed and latest version
Nothing to do
表示这个库已经安装了,我们装另一个
上面说过,一般我们安装提示需要的那个包或者是那个包的devel版本的就行了
[root@localhost nginx-1.7.9]# yum -y install zlib-devel
没一次执行完一条命令,可以查看命令有没有执行成功,不管执行了什么命令,echo $?时,如果返回0表示命令执行成功,否则执行失败。
[root@localhost nginx-1.7.9]# echo $?
0
上面几个库可以用一条命令来安装的,把多个库的名字加在命令后面就可以了
配置完了,安装
[root@localhost nginx-1.7.9]# make &&make install
[root@localhost nginx-1.7.9]# echo $?
0
安装成功
另外一种检验方法
[root@localhost nginx-1.7.9]# ngi
安装完成之后,输入命令的前几个字母,按tab键,如果没补全,有可能没安装成功,但是这不是一点的,有可能是路径没包含在$PATH里面
用上面的命令更新检索库
[root@localhost nginx-1.7.9]# updatedb
如果还是无法补全,用which命令查看我们需要的命令的路径
[root@localhost nginx]# which nginx
/usr/local/nginx/sbin/nginx
或者这样找:
[root@localhost nginx-1.7.9]# locate nginx | grep bin
/opt/nginx-1.7.9/objs/src/http/ngx_http_upstream_round_robin.o
/opt/nginx-1.7.9/src/http/ngx_http_upstream_round_robin.c
/opt/nginx-1.7.9/src/http/ngx_http_upstream_round_robin.h
/usr/local/nginx/sbin
/usr/local/nginx/sbin/nginx
用下面的命令查看系统默认的路径
[root@localhost nginx]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/nginx:/root/bin
很明显,我们的nginx命令不在系统默认的路径下,系统找不到,所以无法补全
添加系统环境变量,是系统能够找到我们安装的程序(命令)
[root@localhost nginx-1.7.9]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin
我们的bash_profile本来就有PATH变量的了,我们只是在后面添加了可执行的nginx的路径
或者我们把可执行的nginx程序拷贝到PATH变量默认的任何一个目录也可以(上面echo $PATH出来的)
改完之后,使之立即生效,
[root@localhost nginx-1.7.9]# source ~/.bash_profile
启动nginx程序,
[root@localhost nginx-1.7.9]# nginx
如果要指定配置文件启动,就用-c参数
[root@localhost nginx-1.7.9]# nginx -c /usr/local/nginx/conf/nginx.conf
如果提示80端口被占用了,查找一下
[root@localhost nginx]# netstat -anlp | grep 80
看到是apache占用这个端口
[root@localhost nginx-1.7.9]# ps -ef | grep apach
root 678 27379 0 15:09 pts/0 00:00:00 grep --color apach
apache 15563 1956 0 Dec21 ? 00:00:17 (wsgi:cobbler_w
apache 15564 1956 0 Dec21 ? 00:00:00 /usr/sbin/httpd
apache 15565 1956 0 Dec21 ? 00:00:01 /usr/sbin/httpd
apache 15566 1956 0 Dec21 ? 00:00:00 /usr/sbin/httpd
apache 15567 1956 0 Dec21 ? 00:00:05 /usr/sbin/httpd
apache 15568 1956 0 Dec21 ? 00:00:00 /usr/sbin/httpd
apache 15569 1956 0 Dec21 ? 00:00:01 /usr/sbin/httpd
apache 15570 1956 0 Dec21 ? 00:00:00 /usr/sbin/httpd
apache 15582 1956 0 Dec21 ? 00:00:00 /usr/sbin/httpd
apache 15583 1956 0 Dec21 ? 00:00:00 /usr/sbin/httpd
[root@localhost nginx-1.7.9]#
把apache服务干掉
[root@localhost nginx-1.7.9]# pkill httpd
服务没有了[root@localhost nginx-1.7.9]# ps -ef | grep apach
root 684 27379 0 15:09 pts/0 00:00:00 grep --color apach
下面是把配置文件及注释去掉的最简单的列出目录的文件
[root@localhost nginx]# vim conf/nginx.conf
1 worker_processes 2;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 autoindex on;
11 autoindex_exact_size on;
12 autoindex_localtime on;
13 server {
14 listen 80;
15 server_name localhost;
16 location /{
17 root html;
18 index index.html index.htm;
19 }
20 error_page 500 502 503 504 /50x.html;
21 location = /50x.html {
22 root html;
23 }
24 }
25 }
只是添加了粗体那3行,其实11和12行都额可以不要的。
我们安装好后,默认的web跟目录是html目录的,比如我指定的安装目录是/usr/local/nginx/
默认的根目录就是/usr/local/nginx/html目录。
做完上面的配置,我们需要把根目录默认的index.html文件删掉或者改为别的名字
否则会默认打开 index.html文件,而不是把根目录的文件全部列出来
重新加载nginx配置
[root@localhost nginx]# nginx -s reload
在浏览器地址栏输入虚拟机的IP,即可访问我们的下载列表。
我们提供的下载列表里的文件是以超链接的形式显示出来的,如果是浏览器可以打开的文件如txt文件,
点击之后不会下载,而是直接打开。
用firefox 浏览器,提示没flash插件,所以没配图。
--------------------------
此文为作者原处学习笔记,转载请署明出处。