编译安装Nginx

来源:互联网 发布:淘宝的详情页怎么做 编辑:程序博客网 时间:2024/05/21 04:39

安装make:

yum -y install gcc automake autoconf libtool make

安装g++:

yum -y install gcc gcc-c++

PCRE库:

Nginx需要PCRE(Perl Compatible Regular Expression),因为Nginx的Rewrite模块和Http核心模块都会使用到PCRE正则表达式语法。其下载地址为http://www.pcre.org/,我们也可以通过yum来安装。

yum -y install pcre pcre-devel

zlib库:

zlib库提供了压缩算法,Nginx很多地方都会用到gzip算法。其下载地址为http://www.zlib.net/,也可以通过yum安装。

yum -y install zlib zlib-devel

OpenSSL:

Nginx中如果服务器提供安全页面,就需要用到OpenSSL库。其下载地址为http://www.openssl.org/,也可以通过yum安装。

yum -y install openssl openssl-devel

下载安装

wget https://nginx.org/download/nginx-1.10.3.tar.gz

tar zxf nginx-1.10.3.tar.gz

cd nginx-1.10.3

./configure

make && make install

make install没有提示error就没问题

Nginx会默认安装在/usr/local/nginx目录,我们cd到/usr/local/nginx/sbin/目录,存在一个Nginx二进制可执行文件。直接运行就可以启动Nginx。

启动:/usr/local/nginx/sbin/nginx 

关闭:/usr/local/nginx/sbin/nginx  -s stop

重启:/usr/local/nginx/sbin/nginx -s reload


设置成服务并自启动


修改nginx的配置文件来支持php文件的解析

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

去掉location ~ \.php$ {节点的注释



修改nginx的默认web路径(原来为/usr/local/nginx/html)

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

修改成/website/www



在原有的location项下面添加一个location项来处理静态文件(css,js,图片)

#静态资源处理
location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf|mp3|mp4|ttf|woff|svg|eot|txt|html|htm)$
      {
          root /website/www/nzg;
          if (-f $request_filename) {
           expires 1d;
           break;
           }
      }

url重写,隐藏index.php

同上需要另外配置一个location项

    location / {
        root /website/www/nzg;
        index index.php;
        if (!-e $request_filename){   #方式1
            rewrite ^/(.*) /index.php last;
        }
        #try_files $uri $uri/ /index.php$is_args$args;  #方式2
    }

这里可使用两种方式来隐藏index.php


常用server节点配置

    server {        listen       80;        #server_name  47.52.110.110;        server_name mytest.cn;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   /website/61group;            index  index.php index.html index.htm;            if (!-e $request_filename){   #方式1                rewrite ^/(.*) /index.php last;            }        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #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;        #}        location ~ \.php$ {            root /website/61group;            fastcgi_pass 127.0.0.1:9000;            fastcgi_index index.php;            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;            include fastcgi_params;        }        location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf|mp3|mp4|ttf|woff|svg|eot|txt|html|htm)$        {            root /website/61group;            if (-f $request_filename) {                expires 1d;                break;            }        }        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }




进入到/website/www目录

vi index.php

添加

<?php phpinfo();?>

保存

再重启nginx即可在浏览器浏览即可

--------

配置本机的hosts文件

添加

192.168.1.222     mytest.cn

#我是在虚拟机里面安装nginx的,其中192.168.1.222 是虚拟机系统的内网ip,如果nginx安装在本机的话就使用127.0.0.1

---------

如果报错403解决办法

将web目录设置为777权限,-R表示向下递归

chmod -R 777 /website/www



0 0