首页 > linux, mysql, nginx > Nginx + php-5.3.1 + mysql-5.1.45 for vps 256内存

Nginx + php-5.3.1 + mysql-5.1.45 for vps 256内存

2010年3月23日 eric 发表评论 阅读评论

刚买的 VPS 因为内存太小,所以各个方面都要优化!mysql 用的 mysqlnd 方式连接。

参考文章:www.s135.com

本人VPS系统 :ubuntu-server-9.04

安装步骤:
  (系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为ubuntu-server)

  一、获取相关开源程序:
  1、【适用ubuntu操作系统】利用ubuntu Linux系统自带的apt-get命令安装、升级所需的程序库:

apt-get install build-essential autoconf automake libtool libjpeg62-dev libpng12-dev libfreetype6-dev libxml2-dev libbz2-dev libncurses5-dev libcurl4-openssl-dev curl openssl bison libsasl2-dev

  2、下载程序源码包:
  ①、从软件的官方网站下载:

cd /home
wget http://sysoev.ru/nginx/nginx-0.8.34.tar.gz
wget http://cn.php.net/distributions/php-5.3.2.tar.bz2
wget http://php-fpm.org/downloads/0.6/php-fpm-0.6~5.3.1.tar.gz
wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.45.tar.gz
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"
wget http://pecl.php.net/get/memcache-2.2.5.tgz
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.01.tar.gz
wget http://bart.eaccelerator.net/source/0.9.6/eaccelerator-0.9.6.tar.bz2
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz

  二、安装PHP 5.2.13(FastCGI模式)
  1、编译安装PHP 5.2.13所需的支持库:

tar zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr/local
make
make install
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/usr
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure --prefix=/usr
make
make install
cd ../

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure --prefix=/usr
make
make install
cd ../

 tar zxvf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
./configure --prefix=/usr
make
make install

  2、编译安装mysql-5.1.45.tar.gz

/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
tar zxvf mysql-5.1.45.tar.gz
cd mysql-5.1.45/
autoreconf --force --install
Aclocal
libtoolize --automake --force
autoconf
./configure --prefix=/usr/local/mysql/ --with-charset=utf8 --with-collation=utf8_general_ci --with-client-ldflags=-all-static -with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-unix-socket-path=/tmp/mysql.sock --sysconfdir=/usr/local/mysql/etc --without-debug --with-mysqld-user=mysql
make && make install
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
cd /usr/local/mysql

 ①、创建MySQL数据库存放目录

mkdir data
mkdir logs

mkdir etc
chown -R mysql:mysql data logs 

②、以mysql用户帐号的身份建立数据表:

/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

 ③、创建my.cnf配置文件:

vi /usr/local/mysql/etc/my.cnf

  输入以下内容:

[client]
port    = 3306
socket  = /tmp/mysql.sock
[mysql]
prompt="(\u:nginxs:)[\d]> "
no-auto-rehash
[mysqld]
user    = mysql
port    = 3306
socket  = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/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
log-error = /usr/local/mysql/logs/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

 ④、创建管理MySQL数据库的shell脚本:

vi /etc/init.d/mysql

  输入以下内容(这里的用户名admin和密码12345678接下来的步骤会创建):

#!/bin/sh
mysql_port=3306
mysql_username="admin"
mysql_password="12345678"

function_start_mysql()
{
    printf "Starting MySQL...\n"
    /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf 2>&1 > /dev/null &
}

function_stop_mysql()
{
    printf "Stoping MySQL...\n"
    /usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown
}

function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 5
    function_start_mysql
}

if [ "$1" = "start" ]; then
    function_start_mysql
elif [ "$1" = "stop" ]; then
    function_stop_mysql
elif [ "$1" = "restart" ]; then
function_restart_mysql
else
    printf "Usage: /usr/local/mysql {start|stop|restart}\n"
fi

  ⑤、赋予shell脚本可执行权限:

chmod +x /etc/init.d/mysql

⑥、启动MySQL:

/etc/init.d/mysql start

 ⑦、通过命令行登录管理MySQL服务器(提示输入密码时直接回车):

/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock

⑧、修改root密码为(12345678):

/usr/local/mysql/bin/mysqladmin -uroot -p password '123456'

  3、编译安装PHP(FastCGI模式)

tar zxvf php-5.3.1.tar.gz
tar zxvf php-fpm-0.6~5.3.1.tar.gz
php-fpm-0.6-5.3.1/generate-fpm-patch
cd php-5.2.13/
Patch -p1 < ../fpm.patch 
./buildconf --force
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-fpm --with-fpm-conf=/usr/local/php/etc/php-fpm.conf --with-libevent=/usr --disable-safe-mode --disable-magic-quotes --enable-zend-multibyte --enable-mbregex --enable-tokenizer=shared --with-layout=PHP --enable-sigchild --enable-xml --with-libxml-dir=/usr --enable-simplexml --enable-filter --disable-debug --with-openssl=shared --with-pcre-regex=/usr --with-zlib=shared,/usr --enable-bcmath=shared --enable-calendar=shared --enable-ctype=shared --with-curl --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-freetype-dir=/usr --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-iconv --with-ldap --enable-mbstring --enable-hash --with-mysql=mysqlnd --with-mysqli=mysqlnd --enable-pdo --with-pdo-mysql=mysqlnd --enable-shmop --enable-sockets --with-mcrypt
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/php/etc/php.ini
cd ../

  4、编译安装PHP5扩展模块

tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

tar jxvf eaccelerator-0.9.6.tar.bz2
cd eaccelerator-0.9.6/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

5、修改php.ini文件
  手工修改:查找/usr/local/php/etc/php.ini中的extension_dir = ”./”
  修改为extension_dir = ”/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/”
  并在此行后增加以下几行,然后保存:
  extension = ”memcache.so”

  6、配置eAccelerator加速PHP:

mkdir -p /usr/local/php/eaccelerator_cache
vi /usr/local/php/etc/php.ini

  按shift+g键跳到配置文件的最末尾,加上以下配置信息:

[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="4"
eaccelerator.cache_dir="/usr/local/php/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"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"

   7、创建www用户和组

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

  8、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
  在/usr/local/php/etc/目录中创建php-fpm.conf文件:

rm -f /usr/local/php/etc/php-fpm.conf
vi /usr/local/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">logs/php-fpm.pid</value>

    Error log file
    <value name="error_log">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">12</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端口,进程数为12,因为VPS内存小,用户为www:

ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start

   三、安装Nginx 0.8.34
  1、安装Nginx所需的pcre库:

tar zxvf pcre-8.01.tar.gz
cd pcre-8.01/
./configure --enable-utf8 --enable-pcregrep-libbz2
make && make install
cd ../

  2、安装Nginx

tar zxvf nginx-0.8.34.tar.gz
cd nginx-0.8.34/
./configure --prefix=/usr/local/nginx --user=www --group=www --with-file-aio --with-http_flv_module --with-http_gzip_static_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-cc-opt=' -O3'
make && make install
cd ../

3、创建 域名虚拟主机目录

mkdir -p /var/www
chmod +w /var/www
chown -R www:www /var/www

4、创建Nginx配置文件
  ①、在/usr/local/nginx/conf/目录中创建nginx.conf文件:

rm -f /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf

  输入以下内容:

user  www www;
worker_processes 2;
error_log  logs/nginx_error.log  crit;
pid        logs/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  domain name;
    index index.html index.htm index.php;
    root  /var/www/wordpress;

    #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  status.domain name;

    location / {
    stub_status on;
    access_log   off;
    }
  }
}

  ②、在/usr/local/nginx/conf/目录中创建fcgi.conf文件:

vi /usr/local/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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

5、启动Nginx

ulimit -SHn 65535
/usr/local/nginx/sbin/nginx

 四、配置开机自动启动Nginx + PHP +mysql 和脚本

1.创建 nginx 启动脚本

Vim /etc/init.d/nginx

#! /bin/sh

 ### BEGIN INIT INFO

# Provides:          nginx

# Required-Start:    $all

# Required-Stop:     $all

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: starts the nginx web server

# Description:       starts nginx using start-stop-daemon

### END INIT INFO

PATH=/usr/local/nginx/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/loca/sbin

DAEMON=/usr/local/nginx/sbin/nginx

NAME=nginx

DESC=nginx

 test -x $DAEMON || exit 0

 # Include nginx defaults if available

 case "$1" in

  start)

        echo -n "Starting $DESC: "

        nginx

        echo "$NAME."

        ;;

  stop)

        echo -n "Stopping $DESC: "

        nginx -s stop

        echo "$NAME."

        ;;

  restart|force-reload)

        echo -n "Restarting $DESC: "

        nginx -s stop

        sleep 1

        nginx

        echo "$NAME."

        ;;

  reload)

      echo -n "Reloading $DESC configuration: "

        nginx -s reload

      echo "$NAME."

      ;;

  *)

        N=/etc/init.d/$NAME

        echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2

        exit 1

        ;;

esac

exit 0

2.创建 php-fmp 启动脚本

ln -s /usr/local/php/sbin/php-fpm /etc/init.d/php-fpm

3.设置开启 nginx php-cgi  mysql 自动启动

update-rc.d nginx defaults

update-rc.d mysql defaults

update-rc.d php-fpm defaults

四、把wordpress 放到 虚拟目录下

mv wordpress /var/www/

转载请注明:文章转载自:Eric's linux and nginx! (http://www.nginxs.com)
本文地址:http://www.nginxs.com/linux/81.html

分类: linux, mysql, nginx 标签: , , , , ,
  1. 2018年2月16日09:23 | #1

    Reducing down trees shouldn’t be as easy job.

  2. 2018年2月16日14:39 | #2

    Let’s face it, lawn care providers abound.

  3. 2018年2月17日23:19 | #3

    Copyright 2017 Skilled Landscaping Companies, LLC.

评论分页
1 ... 3 4 5 81
订阅评论
4+9= (必填)