Total Pageviews

Sunday, 28 September 2025

Nginx,Mysql,php-FPM 小内存VPS配置笔记

 

VPS配置如下:

Intel(R) Xeon(R) CPU L5520 @ 2.27GHz
128MB 保证内存 256MB 并发内存,实际上也就是128MB可用,超过128MB的话进程可能会被杀掉。
系统采用Debian 6.0.

Nginx还是必选的,Apache是不错,不过资源占用相对大些,不适合我这个微型vps,最后溜达一圈,决定用Nginx + Php-FPM的方式来运行,当然必须加上Mysql。

首先当然是先精简一下系统一些不需要的软件,然后更新下系统该更新的软件。

先添加上php5.4的源。

```nano /etc/apt/sources.list``````deb http://ftp.debian.org/debian squeeze main contrib non-free
deb http://security.debian.org squeeze/updates main contrib non-free

deb-src http://ftp.debian.org/debian squeeze main contrib non-free
deb-src http://security.debian.org squeeze/updates main contrib non-free

deb http://packages.dotdeb.org squeeze-php54 all
deb-src http://packages.dotdeb.org squeeze-php54 all``````wget http://www.dotdeb.org/dotdeb.gpg``````cat dotdeb.gpg | sudo apt-key add -``````sudo apt-get update```

开始精简系统无用的软件。

```apt-get -y purge apache2-* bind9-* xinetd samba-* nscd-* portmap sendmail-* sasl2-bin```

开始更新系统。

```sudo apt-get upgrade```

安装编译Nginx必须的开发包以及一些常用的开发包。

```apt-get -y build-dep nginx``````apt-get -y install build-essential automake autoconf autoconf2.13 libcurl4-gnutls-dev libxpm-dev libmcrypt-dev libmysqld-dev libmysqlclient-dev libpng12-dev libjpeg8-dev libmhash-dev libxml2-dev libssl-dev libfreetype6-dev libxslt-dev libltdl-dev```

安装Mysql

```apt-get -y install mysql-server```

由于是小内存,mysql的配置必须改一下,否则一个mysql就可以让整个系统内存占用达到150MB以上。

```nano /etc/mysql/my.cnf```

在[mysqld]下添加

```skip-innodb
default-storage-engine = INNODB```

重启mysql

```/etc/init.d/mysql restart```

安装php5-fpm

```apt-get -y install php5-gd php5-mysql php5-fpm php5-curl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-cli php5-common php5-fpm php5-cgi```

开始编译nginx

```wget http://nginx.org/download/nginx-1.2.4.tar.gz``````tar zxvf nginx-1.2.4.tar.gz``````cd nginx-1.2.4```

伪装nginx为IIS8.0,这个操作可选,蛋疼的娱乐动作。

```nano src/core/nginx.h```

修改以下部分

```#define NGINX_VERSION "8.0"
#define NGINX_VER "Microsoft-IIS/" NGINX_VERSION``````nano src/http/ngx_http_header_filter_module.c```

修改以下部分

```static char ngx_http_server_string[] = "Server: Microsoft-IIS/8.0" CRLF;
static char ngx_http_server_full_string[] = "Server: Microsoft-IIS/8.0" CRLF;``````nano src/http/ngx_http_special_response.c```

修改以下部分

```static u_char ngx_http_error_full_tail[] =
"<ce nter>Microsoft-IIS/8.0" CRLF
"" CRLF
"" CRLF
;

static u_char ngx_http_error_tail[] =
"Microsoft-IIS/8.0" CRLF
"" CRLF
"" CRLF```

准备开始编译nginx,这里想添加几个模块,ubuntu自带的nginx默认的编译参数就包含HttpEcho和HttpUpstreamFair两个模块。

相关的下载地址:

https://github.com/agentzh/echo-nginx-module/downloads

https://github.com/gnosek/nginx-upstream-fair

我自己想再添加两个模块,一个是HttpAccessKey,还有一个是GooglePerftools。

相关的下载地址:

http://wiki.nginx.org/HttpAccessKeyModule

http://code.google.com/p/google-perftools/downloads/list

首先编译Google Perftools。

```tar zxvf gperftools-2.0.tar.gz``````cd gperftools-2.0``````./configure``````make && make install``````echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf``````/sbin/ldconfig``````mkdir /tmp/tcmalloc``````chmod 777 /tmp/tcmalloc```

把上面下载的几个模块都解压到nginx目录下,开始编译nginx。

```./configure --user=www-data --group=www-data --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=gnosek-nginx-upstream-fair-a18b409 --add-module=agentzh-echo-nginx-module-8042c62 --add-module=nginx-accesskey-2.0.3 --sbin-path=/usr/sbin/nginx --with-http_flv_module --with-google_perftools_module``````make && make install``````mkdir /etc/nginx/sites-available``````mkdir /etc/nginx/sites-enabled``````mkdir /etc/nginx/conf.d``````mkdir -p /var/lib/nginx/body``````nano /etc/nginx/nginx.conf``````user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;

events {
    worker_connections  100;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    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 /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout   5;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}``````nano /etc/nginx/sites-available/default``````server {
        listen $ipaddress:443 default;
        server_name $ipaddress;
        root /var/www/none;

        ssl on;
        ssl_certificate /etc/nginx/cert/server.crt;
        ssl_certificate_key /etc/nginx/cert/server.key;

error_page 403 /403.htm;
location = /403.htm {
root /var/www/error;
       }
error_page 404 /404.htm;
location = /404.htm {
root /var/www/error;
       }
error_page 500 502 503 504 /500.htm;
location = /500.htm {
root /var/www/error;
       }

location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME /var/www/none/$fastcgi_script_name;
}

        }```

由于我只做https,不需要http,所以配置如上。

加上自己博客站点配置:

```nano /etc/nginx/sites-available/mydomain.com``````server {
        listen $ipaddress:443;
        server_name mydomain.com;
        root /var/www/mydomain.com;
index index.php;

        ssl on;
        ssl_certificate /etc/nginx/cert/blog.crt;
        ssl_certificate_key /etc/nginx/cert/blog.key;

        access_log  /var/log/nginx/blog-2886.access.log  main;
        error_log  /var/log/nginx/blog-2886.error.log;

error_page 403 /403.htm;
location = /403.htm {
root /var/www/error;
       }
error_page 404 /404.htm;
location = /404.htm {
root /var/www/error;
       }
error_page 500 502 503 504 /500.htm;
location = /500.htm {
root /var/www/error;
       }

//wordpress 的伪静态规则

location / {
            index index.html index.php;
            if (-f $request_filename/index.html){
                rewrite (.*) $1/index.html break;
            }
            if (-f $request_filename/index.php){
                rewrite (.*) $1/index.php;
            }
            if (!-f $request_filename){
                rewrite (.*) /index.php;
            }
        }

//伪静态结束

location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME /var/www/mydomain.com/$fastcgi_script_name;
}

        }```

让配置生效:

```ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default``````ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/mydomain.com```

伪装IIS8.0还有这一步:

```nano /etc/nginx/fastcgi_params```

修改以下部分:

```fastcgi_param  SERVER_SOFTWARE    Microsoft-IIS/8.0;```

配置启动脚本:

```nano /etc/init.d/nginx``````#!/bin/sh

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# 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/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/etc/nginx/sbin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
. /etc/default/nginx
fi

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

test_nginx_config() {
if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
return 0
else
$DAEMON -t $DAEMON_OPTS
return $?
fi
}

case "$1" in
start)
echo -n "Starting $DESC: "
test_nginx_config
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;

stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;

restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON || true
sleep 1
test_nginx_config
start-stop-daemon --start --quiet --pidfile \
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;

reload)
echo -n "Reloading $DESC configuration: "
test_nginx_config
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
--exec $DAEMON || true
echo "$NAME."
;;

configtest|testconfig)
echo -n "Testing $DESC configuration: "
if test_nginx_config; then
echo "$NAME."
else
exit $?
fi
;;

status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac

exit 0``````chmod +x /etc/init.d/nginx```

设置为系统默认启动

```insserv -f nginx```

如果是ubuntu则为

```update-rc.d -f nginx defaults```

我要为直接访问ip单独设置证书,操作如下:

```mkdir /etc/nginx/cert``````openssl genrsa -des3 -out /etc/nginx/cert/test.key 1024```

设置一个密码。
为了不让nginx每次启动都要输密码,再用下面的命令删除密码。

```openssl rsa -in /etc/nginx/cert/test.key -out /etc/nginx/cert/server.key```

生成证书

```openssl req -new -key /etc/nginx/cert/server.key -out /etc/nginx/cert/server.csr``````openssl x509 -req -days 3650 -in /etc/nginx/cert/server.csr -signkey /etc/nginx/cert/server.key -out /etc/nginx/cert/server.crt```

删除不需要再用的文件。

```rm /etc/nginx/cert/server.csr /etc/nginx/cert/test.key```

nginx配置完成,重新启动nginx。

```/etc/init.d/nginx restart```

接下来优化一下php。
由于php5.4还不支持eaccelerator,所以我采用了xcache来优化php。

```apt-get -y install php5-dev``````wget http://xcache.lighttpd.net/pub/Releases/2.0.1/xcache-2.0.1.tar.gz``````tar zxvf xcache-2.0.1.tar.gz``````cd xcache-2.0.1``````phpize5``````./configure --with-php-config=/usr/bin/php-config5 --enable-xcache --enable-xcache-optimizer``````make && make install```

修改一下默认的配置文件。

```nano xcache.ini``````[xcache-common]
zend_extension = /usr/lib/php5/20100525+lfs/xcache.so

[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "mOo"
xcache.admin.pass = ""

[xcache]
xcache.shm_scheme =        "mmap"
xcache.size  =               20M
xcache.count =                 1
xcache.slots =                8K
xcache.ttl   =                 0
xcache.gc_interval =           0

xcache.var_size  =            4M
xcache.var_count =             1
xcache.var_slots =            8K
xcache.var_ttl   =             0
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300

xcache.readonly_protection = Off
xcache.mmap_path =    "/dev/zero"

xcache.coredump_directory =   ""

xcache.experimental =        Off

xcache.cacher =               On
xcache.stat   =               On
xcache.optimizer =           Off

[xcache.coverager]
xcache.coverager =          Off
xcache.coveragedump_directory = ""```

添加到php.ini里。

```cat xcache.ini >> /etc/php5/fpm/php.ini```

隐藏php版本

```nano /etc/php5/fpm/php.ini``````expose_php = Off```

再修改下面这段

```cgi.fix_pathinfo=0```

重启php5-fpm.

```/etc/init.d/php5-fpm restart```

大概就是这样了,至于当中一些参数还是可以适当调整,好让服务器运行在最佳的状态。

我经过调整之后,开机只占用60mb不到的内存,最后跑比较占用资源的wordpress,开了缓存,也不过100mb多点。

No comments:

Post a Comment