上手VPS之编译安装Nginx

By | 2013年3月16日

参考:Nginx安装针对VPS的LNMP生产环境:CentOS+Nginx+MySQL+PHP架构配置

在我的VPS,使用了下面代码来安装:

#必要的库
yum install pcre-devel zlib-devel openssl-devel libxml2-devel libxslt-devel gd-devel perl-ExtUtils-Embed

mkdir /var/log/nginx
mkdir /var/cache/nginx
#编译安装
./configure
 --prefix=/opt/ProgramFiles/nginx
 --sbin-path=/opt/ProgramFiles/nginx
 --error-log-path=/var/log/nginx/error.log
 --http-log-path=/var/log/nginx/access.log
 --pid-path=/opt/ProgramFiles/nginx/nginx.pid
 --with-http_ssl_module
 --with-http_realip_module
 --with-http_addition_module
 --with-http_xslt_module
 --with-http_image_filter_module
 --with-http_sub_module
 --with-http_gunzip_module
 --with-http_gzip_static_module
 --with-http_random_index_module
 --with-http_secure_link_module
 --with-http_degradation_module
 --with-http_stub_status_module
 --with-http_perl_module
 --with-pcre
 --http-client-body-temp-path=/var/cache/nginx/http-client-body.temp
 --http-proxy-temp-path=/var/cache/nginx/http-proxy.temp
 --http-fastcgi-temp-path=/var/cache/nginx/http-fastcgi.temp
 --http-uwsgi-temp-path=/var/cache/nginx/http-uwsgi.temp
 --http-scgi-temp-path=/var/cache/nginx/http-scgi.temp

make
make install

配置

/opt/ProgramFiles/nginx/nginx.conf

error_log  /var/log/nginx/error.log info;
user  nobody;
pid /opt/ProgramFiles/nginx/nginx.pid;
worker_processes  1;
worker_rlimit_nofile 51200;
events {
    use epoll;
    worker_connections  51200;
}
http {
    charset utf-8;
    client_max_body_size 100m;
    client_body_buffer_size 256k;
    client_header_buffer_size 32k;
    
    default_type  application/octet-stream;
     
    include   mime.types;
    ignore_invalid_headers on;
    
    large_client_header_buffers 4 32k;
    
    keepalive_timeout 60;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain text/css application/x-javascript application/xml;
    gzip_vary on;
    
    sendfile on;
    server_names_hash_bucket_size 128;
    server_name_in_redirect off;
    
    tcp_nopush on;
    tcp_nodelay on;
    #虚拟主机的配置文件
    include vhosts/*.conf;
}

/opt/ProgramFiles/nginx/conf/fastcgi_params

#PHP PATH_INFO bugs
if ($request_filename ~* (.*)\.php) {
    set $php_url $1;
}
if (!-e $php_url.php) {
    return 403;
}
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

/opt/ProgramFiles/nginx/conf/vhosts/lijialong.name.conf

server {
        listen 80;
        server_name www.lijialong.name lijialong.name;
        access_log off;
        index index.html index.htm index.php;
        root /var/www/web/html/wordpress;
        
        #域名重定向,访问lijialong.name会跳转到www.lijialong.name
        #if ($host !~ "^www\.lijialong\.name$") {
        #        rewrite ^(.*) http://www.lijialong.name$1 permanent;
        #}
        #在域名结尾自动添加斜线/
        #if (-d $request_filename) {
        #        rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
        #}
        
        #将Nginx与FastCGI的通信方式由TCP改为Unix Socket。TCP在高并发访问下比Unix Socket稳定,但Unix Socket速度要比TCP快。
         location ~ .*\.(php|php5)?$ {
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                #fastcgi_pass   127.0.0.1:9000;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME    /var/www/web/html/wordpress$fastcgi_script_name;
                include         fastcgi_params;
        }
        #设置这些文件在浏览器本地缓存30天,可以提高下次打开我博客的页面加载速度
         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                access_log      off;
                expires 30d;
        }
        #设置样式文件在本地浏览器缓存时间为1点,也是为了提高下一次的访问速度
         location ~ .*\.(js|css)$ {
                access_log      off;
                expires 1d;
        }
        #这里是wordpress的伪静态规则
        if (!-e $request_filename){
		rewrite (.*) /index.php;
        }   
        #将网站根目录下docs目录设置为目录样式。
         #location /docs {
        #        autoindex on;
        #}
}