centos系统LNMP环境配置(五)配置nginx支持php

配置nginx
1、修改nginx.conf
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf$(date "+%F")
vim /usr/local/nginx/conf/nginx.conf

配置文件内容如下:

# 首行user去掉注释,修改Nginx运行组为www www;
# 必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
user  www www;
worker_processes  1;

# 开启nginx错误日志
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    client_max_body_size 2m;

    #gzip  on;

    # 包含域名配置文件( 支持通配符)
    include vhost/*.conf;
}

2、配置 fastcgi.conf文件

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;    # 脚本文件请求的路径  
fastcgi_param  QUERY_STRING       $query_string;            # 请求的参数;如?app=123  
fastcgi_param  REQUEST_METHOD     $request_method;            # 请求的动作(GET,POST)  
fastcgi_param  CONTENT_TYPE       $content_type;             # 请求头中的Content-Type字段  
fastcgi_param  CONTENT_LENGTH     $content_length;             # 请求头中的Content-length字段。    
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;         # 脚本名称   
fastcgi_param  REQUEST_URI        $request_uri;              # 请求的地址不带参数  
fastcgi_param  DOCUMENT_URI       $document_uri;             # 与$uri相同。   
fastcgi_param  DOCUMENT_ROOT      $document_root;             # 网站的根目录。在server配置中root指令中指定的值   
fastcgi_param  SERVER_PROTOCOL    $server_protocol;             # 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。    
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;                # cgi 版本  
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;            # nginx 版本号,可修改、隐藏  
fastcgi_param  REMOTE_ADDR        $remote_addr;             # 客户端IP  
fastcgi_param  REMOTE_PORT        $remote_port;             # 客户端端口  
fastcgi_param  SERVER_ADDR        $server_addr;             # 服务器IP地址  
fastcgi_param  SERVER_PORT        $server_port;             # 服务器端口  
fastcgi_param  SERVER_NAME        $server_name;             # 服务器名,域名在server配置中指定的server_name
#fastcgi_param  PATH_INFO         $path_info;                # 可自定义变量  
# PHP only, required if PHP was built with --enable-force-cgi-redirect  
fastcgi_param  REDIRECT_STATUS    200;

3、配置虚拟主机公用配置文件server.conf

# php文件访问配置
location ~ .*\.(php|php5)?$
{
    #fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
}

# 静态文件缓存30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
    expires 30d;
    # access_log off;
}

# js,css文件缓存15个小时
location ~ .*\.(js|css)?$
{
    expires 15d;
    # access_log off;
}

4、创建虚拟主机文件 vhost/iong.cn.conf

server {
    listen 80;

    # 配置域名
    server_name  www.iong.cn iong.cn;

    # 配置网站目录
    root   /usr/local/nginx/html/iong.cn;

    # 配置域名重定向
    if ($host != 'www.iong.cn' ) {
        rewrite ^/(.*)$ http://www.yphp.cn/$1 permanent;
    }

    location / {

        # 配置rewrite
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }

        # include  /usr/local/nginx/html/yphp/.htaccess;
        # rewrite ^/(.+)/(.+)[/]?$ /index.php?m=$1&a=$2 last;

        # 配置默认访问文件
        index  index.php index.html index.htm;
    }

    # 包含虚拟主机公用配置文件
    include server.conf;
}

5、重启服务

/etc/init.d/nginx stop # 停止nginx 服务 或 service nginx start
/etc/init.d/nginx start # 启动nginx 服务

6、绑定host文件,然后在浏览器中打开iong.cn 即可

Composer安装
1、在 Linux和 Mac OS X 中可以运行如下命令:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

你可能感兴趣的:(#,linux环境配置)