程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> Debian+Nginx+PHP(FastCGI)+MySQL搭建LNMP服務第二版2

Debian+Nginx+PHP(FastCGI)+MySQL搭建LNMP服務第二版2

編輯:PHP綜合

6、配置eAccelerator加速PHP
mkdir -p /usr/local/webserver/eaccelerator_cache
vi /usr/local/webserver/php/etc/PHP.ini [eaccelerator]
zend_extension="/usr/local/webserver/php/lib/PHP/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

7、創建www用戶和組,以及供 www.freeopens.com虛擬主機使用的目錄
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
mkdir -p /data0/htdocs/www
chmod +w /data0/htdocs/www
chown -R www:www /data0/htdocs/www

8、創建php-fpm配置文件(php-fpm是為PHP打的一個FastCGI管理補丁,可以平滑變更php.ini配置而無需重啟PHP-CGI)

rm -f /usr/local/webserver/php/etc/PHP-fpm.conf
vi /usr/local/webserver/php/etc/PHP-fpm.conf
輸入以 下內容(如果您安裝Nginx+PHP用於程序調試,請將以下的
<value name="display_errors">0</value>
改為
<value name="display_errors">1</value>
以便顯示PHP錯誤信息,否則,Nginx 會報狀態為500的空白錯誤頁):

<?XML version="1.0" ?>
<configuration>

  All relative paths in this config are relative to PHP's install prefix

  <section name="global_options">

    Pid file
    <value name="pid_file">/usr/local/webserver/php/logs/PHP-fpm.pid</value>

    Error log file
    <value name="error_log">/usr/local/webserver/php/logs/PHP-fpm.log</value>

    Log level
    <value name="log_level">notice</value>

    When this amount of PHP processes exited with SIGSEGV or SIGBUS ...
    <value name="emergency_restart_threshold">10</value>

    ... in a less than this interval of time, a graceful restart will be initiated.
    Useful to work around accidental curruptions in accelerator's shared memory.
    <value name="emergency_restart_interval">1m</value>

    Time limit on waiting child's reaction on signals from master
    <value name="process_control_timeout">5s</value>

    Set to 'no' to debug fpm
    <value name="daemonize">yes</value>

  </section>

  <workers>

    <section name="pool">

      Name of pool. Used in logs and stats.
      <value name="name">default</value>

      Address to accept fastCGI requests on.
      Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
      <value name="listen_address">127.0.0.1:9000</value>

      <value name="listen_options">

        Set listen(2) backlog
        <value name="backlog">-1</value>

        Set permissions for unix socket, if one used.
        In Linux read/write permissions must be set in order to allow connections from web server.
        Many BSD-derrived systems allow connections regardless of permissions.
        <value name="owner"></value>
        <value name="group"></value>
        <value name="mode">0666</value>
      </value>

      Additional PHP.ini defines, specific to this pool of workers.
      <value name="PHP_defines">
        <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
        <value name="display_errors">0</value>
      </value>

      Unix user of processes
      <value name="user">www</value>

      Unix group of processes
      <value name="group">www</value>

      Process manager settings
      <value name="pm">

        Sets style of controling worker process count.
        Valid values are 'static' and 'apache-like'
        <value name="style">static</value>

        Sets the limit on the number of simultaneous requests that will be served.
        Equivalent to apache MaxClIEnts directive.
        Equivalent to PHP_FCGI_CHILDREN environment in original PHP.fCGI
        Used with any pm_style.
        <value name="max_children">128</value>

        Settings group for 'apache-like' pm style
        <value name="apache_like">

          Sets the number of server processes created on startup.
          Used only when 'apache-like' pm_style is selected
          <value name="StartServers">20</value>

          Sets the desired minimum number of idle server processes.
          Used only when 'apache-like' pm_style is selected
          <value name="MinSpareServers">5</value>

          Sets the desired maximum number of idle server processes.
          Used only when 'apache-like' pm_style is selected
          <value name="MaxSpareServers">35</value>

        </value>

      </value>

      The timeout (in seconds) for serving a single request after which the worker process will be terminated
      Should be used when 'max_execution_time' ini option does not stop script execution for some reason
      '0s' means 'off'
      <value name="request_terminate_timeout">0s</value>

      The timeout (in seconds) for serving of single request after which a PHP backtrace will be dumped to slow.log file
      '0s' means 'off'
      <value name="request_slowlog_timeout">0s</value>

      The log file for slow requests
      <value name="slowlog">logs/slow.log</value>

      Set open file desc rlimit
      <value name="rlimit_files">65535</value>

      Set max core size rlimit
      <value name="rlimit_core">0</value>

      Chroot to this directory at the start, absolute path
      <value name="chroot"></value>

      Chdir to this directory at the start, absolute path
      <value name="chdir"></value>

      Redirect workers' stdout and stderr into main error log.
      If not set, they will be redirected to /dev/null, according to FastCGI specs
      <value name="catch_workers_output">yes</value>

      How much requests each process should execute before respawn.
      Useful to work around memory leaks in 3rd party librarIEs.
      For endless request processing please specify 0
      Equivalent to PHP_FCGI_MAX_REQUESTS
      <value name="max_requests">1024</value>

      Comma separated list of ipv4 addresses of FastCGI clIEnts that allowed to connect.
      Equivalent to FCGI_WEB_SERVER_ADDRS environment in original PHP.fCGI (5.2.2+)
      Makes sense only with AF_INET listening socket.
      <value name="allowed_clIEnts">127.0.0.1</value>

      Pass environment variables like LD_LIBRARY_PATH
      All $VARIABLEs are taken from current environment
      <value name="environment">
        <value name="HOSTNAME">$HOSTNAME</value>
        <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
        <value name="TMP">/tmp</value>
        <value name="TMPDIR">/tmp</value>
        <value name="TEMP">/tmp</value>
        <value name="OSTYPE">$OSTYPE</value>
        <value name="MACHTYPE">$MacHTYPE</value>
        <value name="MALLOC_CHECK_">2</value>
      </value>
    </section>
  </workers>
</configuration>

9、啟動PHP-CGI進程,監聽127.0.0.1的 9000端口,進程數為128(如果服務器內存小於3GB,可以只開啟64個進程),用戶為www:
ulimit -SHn 65535
/usr/local/webserver/php/sbin/PHP-fpm start
注:/usr/local/webserver/php/sbin/php-fpm還有其他參數,包括:start|stop|quit|restart|reload|logrotate,修改php.ini後不重啟PHP-CGI,重新加載配置文件使用reload。

這裡會根據你給的虛擬機的內存大小出現錯誤提示:
Starting PHP_fpm eAccelerator: Could not allocate 67108864 bytes, the maximum size the kernel allows is 33554432 bytes. Lower the amount of memory request or increase the limit in /proc/sys/kernel/shmmax.
PHP Warning:  [eAccelerator] Can not create shared memory area in Unknown on line 0
PHP Fatal error:  Unable to start eAccelerator module in Unknown on line 0
Failed
這個大概意思就是說內存不足,導致啟動不了,沒有關系,因為我們還沒有對其進行優化,優化後,就會正常了。我們繼續,先不理會這個提示!
四、安裝Nginx 0.8.36
1、安裝Nginx所需的pcre庫

tar zxvf pcre-8.01.tar.gz
cd pcre-8.01/
./configure
make && make install
cd ../

2、安裝Nginx
tar zxvf nginx-0.8.36.tar.gz
cd nginx-0.8.36/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../

3、創建Nginx日志目錄 mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs

4、創建Nginx配置文件
①、在/usr/local/webserver/nginx/conf/目錄中創建 nginx.conf文件:
rm -f /usr/local/webserver/nginx/conf/nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf
輸入以下內容:
user  www www;
worker_processes 8;
error_log  /data1/logs/nginx_error.log  crit;
pid        /usr/local/webserver/nginx/nginx.pid;
#SpecifIEs the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events   use epoll;
  worker_connections 65535; http   include       mime.types;
  default_type  application/octet-stream;
  #charset  gb2312;
  server_names_hash_bucket_size 128;
  clIEnt_header_buffer_size 32k;
  large_clIEnt_header_buffers 4 32k;
  clIEnt_max_body_size 8m;
  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 60;
  tcp_nodelay on;
  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 application/x-Javascript text/CSS application/XML;
  gzip_vary on;
  #limit_zone  crawler  $binary_remote_addr  10m;
  server     listen       80;
    server_name  blog.s135.com;
    index index.Html index.htm index.PHP;
    root  /data0/htdocs/blog;
    #limit_conn   crawler  20;    
    location ~ .*\.(php|php5)?$       #fastCGI_pass  unix:/tmp/PHP-CGI.sock;
      fastCGI_pass  127.0.0.1:9000;
      fastCGI_index index.PHP;
      include fCGI.conf;     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$       expires      30d;     location ~ .*\.(JS|CSS)?$       expires      1h;     log_format  Access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
    access_log  /data1/logs/access.log  Access;   server     listen       80;
    server_name  www.freeopens.com;
    index index.Html index.htm index.PHP;
    root  /data0/htdocs/www;
    location ~ .*\.(php|php5)?$       #fastCGI_pass  unix:/tmp/PHP-CGI.sock;
      fastCGI_pass  127.0.0.1:9000;
      fastCGI_index index.PHP;
      include fCGI.conf;     log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
    Access_log  /data1/logs/wwwlogs.log  wwwlogs; }
②、在/usr/local/webserver/nginx/conf/目錄中創建fCGI.conf文件
vi /usr/local/webserver/nginx/conf/fCGI.conf
輸入以下內容:

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastCGI_param  SERVER_SOFTWARE    nginx;

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_FILENAME    $document_root$fastCGI_script_name;
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  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;

5、啟動Nginx
ulimit -SHn 65535
/usr/local/webserver/nginx/sbin/nginx

五、配置開機自動啟動Nginx + PHP + MySQL
vi /etc/rc.local
把下面的內容增加到 exit 0的上面,這個地方一定要注意,是在exit 0的上面,放到下面的話,就不會啟動了!!
/data0/mysql/3306/MySQL start
ulimit -SHn 65535
/usr/local/webserver/php/sbin/PHP-fpm start
/usr/local/webserver/nginx/sbin/nginx
六、相關軟件的重新啟動
如果說你改了nginx的配置文件,就要用下面的命令,使配置生效
/usr/local/webserver/nginx/sbin/nginx -t   檢查參數
kill -HUP `cat /usr/local/webserver/nginx/nginx.pid`   平滑重啟
如果說你改了 PHP.ini就要用下面的命令,使配置生效
/usr/local/webserver/php/sbin/PHP-fpm reload 重新加載 MySQL數據庫重新啟動
/data0/mysql/3306/MySQL restart
七、優化Linux 內核參數
vi /etc/sysctl.conf
在末尾增加以下內容
# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.Netdev_max_backlog =  32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retrIEs = 2
net.ipv4.tcp_syn_retrIEs = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535
使 配置立即生效
/sbin/sysctl -p

八、在小內存服務器上的配置優化
1、Nginx  的主配置文件(nginx.conf)優化
vi /usr/local/webserver/nginx/conf/nginx.conf #Nginx每個進程耗費10M~12M內存,這裡只開啟一個Nginx進程,節省內 存。
worker_processes 1;
#對網頁文件、CSS、 JS、XML等啟動gzip壓縮,減少數據傳輸量,提高訪問速度。
gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-Javascript text/CSS application/XML;
gzip_vary on;
location ~ .*\.(php|PHP5)?$      #將Nginx與FastCGI的通信方式由TCP改為Unix Socket。TCP在高並發訪問下比Unix Socket穩定,但Unix Socket速度要比TCP快。
     fastCGI_pass  unix:/tmp/PHP-CGI.sock;
     #fastCGI_pass  127.0.0.1:9000;
     fastCGI_index index.PHP;
     include fCGI.conf; location ~ /read.PHP      #將Nginx與FastCGI的通信方式由TCP改為Unix Socket。TCP在高並發訪問下比Unix Socket穩定,但Unix Socket速度要比TCP快。
     fastCGI_pass  unix:/tmp/PHP-CGI.sock;
     #fastCGI_pass  127.0.0.1:9000;
     fastCGI_index index.PHP;
     include fCGI.conf; #博 客的圖片較多,更改較少,將它們在浏覽器本地緩存15天,可以提高下次打開我博客的頁面加載速度。
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$      expires      15d;    #博客會加載很多Javascript、CSS,將它們在浏覽器本地緩存1天,訪問者在看完一篇文章或一頁後,再看另一篇文件或另一頁的內容,無需從服務器再次下載相同的JavaScript、CSS,提高了頁面顯示速度。
   location ~ .*\.(JS|CSS)?$      expires      1d;
   }

2、PHP(FastCGI)的配置優化
PHP.ini 配置文件中關於eAcelerator的優化。只使用1M共享內存,刪除所有在最後3600秒內無法存取的腳本緩存,用磁盤輔助進行緩存。
vi /usr/local/webserver/php/etc/PHP.ini
根 據內容增加或者修改
eaccelerator.shm_size="1"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"

PHP-fpm.conf 的配置優化
vim /usr/local/webserver/php/etc/PHP-fpm.conf
一是修改以下一行,將啟動的 PHP-CGI進程數由原來的128個改為5個:
<value name="max_children">5</value>
二是修改以下一行,將TCP模式改為 Unix Socket模式:
<value name="listen_address">/tmp/PHP-CGI.sock</value> 3、MySQL配置優化 vi /data0/MySQL/3306/my.cnf
根據自己的情況改吧
[clIEnt]
port    = 3306
socket  = /tmp/MySQL.sock
[MySQL]
prompt="(\u:s135:)[\d]> "
no-auto-rehash
[MySQLd]
user    = MySQL
port    = 3306
socket  = /tmp/MySQL.sock
basedir = /usr/local/webserver/MySQL
datadir = /usr/local/webserver/MySQL/data
open_files_limit    = 600
back_log = 20
max_connections = 100
max_connect_errors = 200
table_cache = 60
external-locking = FALSE
max_allowed_packet = 16M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size = 10
thread_concurrency = 8
query_cache_size = 0M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default_table_type = MyISAM
thread_stack = 192K
transaction_isolation = READ-UNCOMMITTED
tmp_table_size = 512K
max_heap_table_size = 32M
/usr/local/webserver/MySQL/data/slow.log
/usr/local/webserver/MySQL/data/error.log
long_query_time = 1
log_long_format
server-id = 1
#log-bin = /usr/local/MySQL/data/binlog
binlog_cache_size = 2M
max_binlog_cache_size = 4M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 4M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
bulk_insert_buffer_size = 2M
myisam_sort_buffer_size = 4M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
[MySQLdump]
quick
max_allowed_packet = 16M

九、每天定時切割Nginx日志
1、創建腳本/usr/local /webserver/nginx/sbin/cut_nginx_log.sh vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
輸入以下內容
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/Access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
2、設置crontab,每天凌晨00:00切割nginx訪問日志
crontab -e
增加以下內容
00 00 * * * /bin/bash  /usr/local/webserver/nginx/sbin/cut_nginx_log.sh

如果有任何編譯方面的錯誤提示,大家可能參考一下我的第一版文章http://www.freeopens.com /2010_03_855.Html
在這裡面涉及到一些,希望對大家有用!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved