linux系統LNMP的nginx、php配置(nginx部分),lnmpnginx

來源:互聯網
上載者:User

linux系統LNMP的nginx、php配置(nginx部分),lnmpnginx

LNMP的nginx、php配置(nginx部分)

一、nginx設定檔/usr/local/nginx/conf/nginx.conf

清空nginx.conf,將下面的配置拷貝進去:

user nobody nobody;  worker_processes 2;  error_log /usr/local/nginx/logs/nginx_error.log crit;  pid /usr/local/nginx/logs/nginx.pid;  worker_rlimit_nofile 51200;  events  {  use epoll;  worker_connections 6000;  }    http  {  include mime.types;  default_type application/octet-stream;  server_names_hash_bucket_size 3526;  server_names_hash_max_size 4096;  log_format combined_realip 'remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"';  sendfile on;  tcp_nopush on;  keepalive_timeout 30;  client_header_timeout 3m;  client_body_timeout 3m;  send_timeout 3m;  connection_pool_size 256;  client_header_buffer_size 1k;  large_client_header_buffers 8 4k;  request_pool_size 4k;  output_buffers 4 32k;  postpone_output 1460;  client_max_body_size 10m;  client_body_buffer_size 256k;  client_body_temp_path /usr/local/nginx/client_body_temp;  proxy_temp_path /usr/local/nginx/proxy_temp;  fastcgi_temp_path /usr/local/nginx/fastcgi_temp;  fastcgi_intercept_errors on;  tcp_nodelay on;  gzip on;  gzip_min_length 1k;  gzip_buffers 4 8k;  gzip_comp_level 5;  gzip_http_version 1.1;  gzip_types text/plain application/x-javascript text/css text/htm applicationxml;  ##配置虛擬機器server{listen 80;server_name localhost;index index.html index.htm index.php;root /usr/local/nginx/html;location ~ \.php$ {     include fastcgi_params;     fastcgi_pass 127.0.0.1:9000;     fastcgi_index index.php;     fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;      }}}

 

虛擬機器設定server可以單獨在conf/vhosts/下單獨建vhosts.conf進行配置,然後在nginx.conf裡加入:

include /vhosts/*.conf

驗證、重新載入nginx:

 

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

 

此時,在瀏覽器裡進行訪問:

二、將之前lamp搭建的discuz遷移到nginx上運行:

 

cd /usr/local/nginx/conf/vhosts  cp vhosts.conf discuz.conf  vi discuz.conf  /usr/local/nginx/sbin/nginx -t  /usr/local/nginx/sbin/nginx -s reload  

此時就可以訪問原來的discuz了:

三、php-fpm的配置

設定檔路徑:/usr/local/php-fpm/etc/php-fpm.conf,清空後,粘貼下面的代碼:

 

[global]  pid = /usr/local/php-fpm/var/run/php-fpm.pid  error_log = /usr/local/php-fpm/var/log/php-fpm.log    [www]  //一個網站對應一個池子,監聽連接埠也不同listen = 127.0.0.1:9000   ;listen = /tmp/php-fcgi.sock  user = php-fpm  group = php-fpm  listen.owner = nobody  listen.group = nobody  pm = dynamic            //動態進程數,如指定static,則有pm.max_children指定固定子進程數pm.max_children = 50     //子進程最大數pm.start_servers = 20    //啟動時,子進程數pm.min_spare_servers = 5  //保證空閑進程最小值,空閑進程低於此值則增加新子進程pm.max_spare_servers = 35  //保證空閑進程最大值,空閑進程高於此值則進行 清理pm.max_requests = 500  rlimit_files = 1024  slowlog = /usr/local/php-fpm/var/log/slow.log  //定義慢查詢日誌路徑request_slowlog_timeout = 1  //定義逾時時間php_admin_value[open_basedir]=/data/www:/tmp/   //定義opne_basedir[www1]  listen = 127.0.0.1:9001 ;listen = /tmp/php-fcgi.sock  user = php-fpm  group = php-fpm  listen.owner = nobody  listen.group = nobody  pm = dynamic  pm.max_children = 50  pm.start_servers = 20  pm.min_spare_servers = 5  pm.max_spare_servers = 35  pm.max_requests = 500  rlimit_files = 1024

 

四、nginx進階配置

1.配置第二個虛擬機器主機

可以直接在nginx.conf加入

include vhosts/*.conf;

這樣,就可以直接在/conf/vhosts目錄下建立其他的虛擬機器主機設定檔。

2.設定預設虛擬機器主機

虛擬機器主機設定檔中修改這一行就可以了,在瀏覽器中輸入ip訪問,會直接存取這個預設虛擬機器主機;

listen 80 default_server;

3.使用者認證

因為要使用htpasswd,所以首先要安裝apache,直接使用yum install httpd安裝,就可以使用htpasswd:

 

/usr/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd weix

 

在nginx的設定檔增加:

 

location / {              root /data/www;            auth_basic  "Auth";              auth_basic_user_file  /usr/local/nginx/conf/.htpasswd;             } 

 

4、網域名稱重新導向

在nginx的設定檔增加:

 

if ($host != 'www.1.com'){        rewrite ^/(.*)$ http://www.1.com/$1 permanent;                } 

 

5、日誌切割

配置日誌:

在對應虛擬伺服器設定檔discuz.conf中加入:

access_log /home/logs/discuz.log combined_realip;

其中combined_realip是已經在nginx.conf中定義的日誌格式;

編寫shell:

vim /usr/local/sbin/logrotate.sh

 

#!/bin/bash  d=`date -d "-1 day" +%Y%m%d`  /bin/mv /home/logs/discuz.log /home/logs/discuz_$d.log  /etc/init.d/nginx - reload >/dev/null 2>/dev/null  cd /home/logs  gzip discuz_$d.log

 

將指令碼加入計劃任務,就可以每天分割日誌並進行壓縮了;

6、靜態檔案不記錄日誌,配置緩衝

編輯對應的虛擬機器組態檔,加入如下代碼:

 

loation ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$       {                  expires 30d;                  access_log off;       }  location ~ .*\.(js|css)?$       {                 expires 12h;                 access_log off;       } 

 

7、防盜鏈

編輯虛擬機器主機設定檔,加入如下代碼,注意,location只會匹配一次,gif、jpg等如果另外定義了不記錄日誌,防盜鏈如果在後面會導致防盜鏈失效,所以,配置的時候要和其他針對這些檔案操作的寫在一起,例如中,就多加了兩行對日誌和緩衝時間的定義;

 

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls|)$ {        valid_referers none blocked server_names *.1.com *.a.com *.b.com *.baidu.com *.soso.com *.google.com *.google.cn ;        if ($invalid_referer){        return 403;        #rewrite ^/ http:/www.example.com/nophoto.gif;  }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.