安装Nginx网络HTTP服务器(Linux) (2015-01-23)

第一步:安装依赖库

CentOS用这个

yum install make gcc pcre-devel zlib-devel openssl-devel git bison wget

 

Ubuntu用这个

sudo apt-get install gcc libpcre++-dev libssl-dev make git

 

第二步:下载所需nginx的扩展源码

mkdir -p /home/git/code
cd /home/git/code
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
git clone https://github.com/agentzh/sregex
git clone https://github.com/agentzh/replace-filter-nginx-module

 

 

第三步:安装sregex

cd /home/git/code/sregex
make
make install

 

 

第四步:到www.nginx.org官方下载最新稳定版

mkdir ~/down
cd ~/down
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.6.2.tar.gz

 

 

第五步:编译安装

cd ~/down/nginx-1.6.2
./configure \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_spdy_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-ipv6 \
--add-module=/home/git/code/ngx_http_substitutions_filter_module \
--add-module=/home/git/code/replace-filter-nginx-module

执行编译安装

make
make install

 

 

 第六步:查看编译相关信息

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

如果看到下面的提示就对了

nginx version: nginx/1.6.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
TLS SNI support enabled
configure arguments: --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-ipv6 --add-module=/home/git/ngx_http_substitutions_filter_module --add-module=/home/git/replace-filter-nginx-module

 

 

第七步:启动与检查

当你执行下面语句时,没有任何返回,说明运行成功。

/usr/local/nginx/sbin/nginx

确定是否运行成功

netstat -anp |grep 80

如果结果中存在

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      712/nginx

说明成功了

再来看看712/nginx的具体路径

ps x |grep 712

如果结果是

  712 ?        Ss     0:00 nginx: master process /usr/local/nginx/sbin/nginx

可以看到路径/usr/local/nginx/sbin/nginx说明成功了

 

最后将

/usr/local/nginx/sbin/nginx

写入到

/etc/rc.local

让系统开机就自动启动nginx

 

 

 

 

最后说一些可能会出现的问题

/usr/local/nginx/sbin/nginx: error while loading shared libraries: libsregex.so.0: cannot open shared object file: No such file or directory

对于上面这种问题,可以用下面方法解决

ldd /usr/local/nginx/sbin/nginx

确定问题后,如果是32位系统执行

ln -s /usr/local/lib/libsregex.so.0 /lib

 如果是64位系统,执行

ln -s /usr/local/lib/libsregex.so.0 /lib64

 

 

 

关于配置PHP方面的

请编辑下面文件(推荐vi或vim)

/usr/local/nginx/conf/nginx.conf

1:找到下面一段文字,大概在65行(vi/vim编辑器的可以使用:65到达)

        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

将每一行前面的#符号都去掉

 2:将其中的

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

改成

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

3:保存后,重新载入nginx配置文件

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

4:创建phpinfo.php测试文件

/usr/local/nginx/html/phpinfo.php

 内容为

<?php
phpinfo();

5:运行php文件测试

http://127.0.0.1/phpinfo.php

请将127.0.0.1换成你服务器的IP地址,并在浏览器里打开查看。

 

 

配置参考

worker_processes 1;
pid logs/nginx.pid;
events{
    worker_connections 1024;
}
http{
    access_log off;
    include mime.types;
    sendfile on;
    client_body_temp_path temp/client_body_temp;
    fastcgi_temp_path temp/fastcgi_temp;
    scgi_temp_path temp/scgi_temp;
    uwsgi_temp_path temp/uwsgi_temp;
    proxy_temp_path temp/proxy_temp;
    server{
        listen 0.0.0.0:80;
        server_name www.baidu.com;
        proxy_set_header Host www.baidu.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        location / {
            proxy_pass http://www.baidu.com;
        }
    }
    server{
        listen 0.0.0.0:80 default;
        root html;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值