在128M的VPS上配置mysql+Tornado+Nginx笔记

最近http://goo.gl/2Q0X2 1 又推出一年$10的便宜VPS,128M内存,可以用来学习。在这样的vps 上放一个博客或做反向代理绰绰有余。买下后尝试配一个mysql+Tornado+Nginx环境,因为在小内存的VPS 上安装东西比较麻烦,动不动的内存超出导致安装失败或程序启动不了,在这里记下个人的配置过程,供初学者参考。

我买下的VPS是配置:

Linux-128-Yearly
Guaranteed RAM 128MB
Burstable RAM 128MB
Disk Space 5GB
Bandwidth 250GB/Month
$10.00 USD/Annually

Instant Setup!
1vCPU
1 IP Address
SolusVM Control Panel

选择系统 建议选择debian 或ubuntu 这两个占用的资源少,我安装了debian-6.0-x86,python的版本是2.6.6。

卸载Apache2 卸载Apache2 的命令

apt-get --purge remove apache2
apt-get --purge remove apache2.2-common

安装mysql 首先更新apt-get

apt-get update

一行命令简单安装mysql

apt-get install mysql-server

中间需要提示设置mysql root 用户密码,记住不要跟登录系统root 用户的密码相同,避免不必要的麻烦。

安装比较顺利,但在启动mysql 时失败!

需要修改mysql 配置文件才能启动,以下是我的一份配置。 配置文件: /etc/mysql/my.cnf

[client]
port		= 3306
socket		= /var/run/mysqld/mysqld.sock

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
#
# * Basic Settings
#
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
language	= /usr/share/mysql/english
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address		= 127.0.0.1
#
# * Fine Tuning
#
key_buffer		= 16K
max_allowed_packet	= 1M
thread_stack		= 64K
thread_cache_size       = 4
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
max_connections        = 16
table_cache            = 8
thread_concurrency     = 2

sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K
#
# * Query Cache Configuration
#
query_cache_limit	= 256k
query_cache_size        = 4M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
#
# Here you can see queries with especially long duration
#log_slow_queries	= /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id		= 1
#log_bin		= /var/log/mysql/mysql-bin.log
#expire_logs_days	= 2
#max_binlog_size        = 10M
#binlog_do_db		= include_database_name
#binlog_ignore_db	= include_database_name

#important
#skip-innodb
innodb = OFF
default_storage_engine = MyISAM

#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

[mysqldump]
quick
quote-names
max_allowed_packet	= 16M

[mysql]
no-auto-rehash	# faster start of mysql but no tab completition

[isamchk]
key_buffer = 8M
sort_buffer_size = 8M

[myisamchk]
key_buffer = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout

保存后尝试启动mysql

service mysql restart
mysqladmin -u root -p var | grep have_innodb
| have_innodb   | DISABLED   |

没出意外可以在top 里看到mysqld 进程。

debian 下安装Tornado 一行命令简单安装Tornado

apt-get install python-tornado

debian 下安装Nginx 一行命令简单安装Nginx

apt-get install nginx

好了,就这样全部搞定。建立一个Tornado 测试脚本:tornado_test.py

# -*- coding: utf-8 -*-
from tornado import httpserver
from tornado import ioloop

def handle_request(request):
   message = "You requested %s\n" % request.uri
   request.write("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s" % (
                 len(message), message))
   request.finish()

http_server = httpserver.HTTPServer(handle_request)
http_server.listen(8080)
ioloop.IOLoop.instance().start()

运行tornado_test.py

python tornado_test.py

在浏览器输入你的VPS ip就可以看到:

You requested /

再输入 你的vps ip/test 就可以看到:

You requested /test

最后查看一下内存: free 把free 结果输出到一个文件 free > freefile

total       used       free     shared    buffers     cached
Mem:        131072      34128      99944          0          0          0
-/+ buffers/cache:      34128      99944
Swap:            0          0          0

贴个top 和free 图

图片:128mvps-mysql+Tornado+Nginx.png

Relative Articles