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

最近 123systems http://goo.gl/2Q0X2 又推出一年$10的便宜 VPS,128M内存,可以用来学习。在这样的vps 上放一个博客或做反向代理绰绰有余,买下后尝试配一个mysql+Tornado+Nginx 环境。

128M的VPS

因为在小内存的VPS 上安装东西比较麻烦,动不动的内存超出导致安装失败或程序启动不了,在这里记下个人的配置过程,供初学者参考。

VPS配置

1
2
3
4
5
6
7
8
9
10
11
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 的命令

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

安装mysql

首先更新apt-get

1
apt-get update

一行命令简单安装mysql

1
apt-get install mysql-server

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
[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

1
service mysql restart

1
2
mysqladmin -u root -p var | grep have_innodb
| have_innodb   | DISABLED   |

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

debian 下安装Tornado

一行命令简单安装Tornado

1
apt-get install python-tornado

debian 下安装Nginx

一行命令简单安装Nginx

1
apt-get install nginx

测试

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

1
2
3
4
5
6
7
8
9
10
11
12
13
# -*- 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

1
python tornado_test.py

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

1
You requested /

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

1
You requested /test

最后查看一下内存:

1
2
3
free
把free 结果输出到一个文件
free > freefile

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

本文网址: https://pylist.com/topic/20.html 转摘请注明来源

Suggested Topics

SAE+python+Tornado+pyTenjin 的完整示例

python 简单易懂,Tornado 高效易学,pyTenjin 轻巧快速,SAE 安全稳定使用门槛低。现在把他们结合在一起做了一个可运行在SAE 上的完整示例。...

ssdb 在小内存vps 上的配置

ssdb 是一个数据结构和接口与redis 很相近的NoSQL 数据库,但它对内存依赖不高,数据可直接落到硬盘,所以 ssdb 在小内存上跑得也很欢。...

服务器推送技术 Tornado comet

long poll的原理是,客户端与服务器将建立一条长连接,也就是说,客户端会发出一个请求,而服务器,将阻塞请求,直到有数据需要传递,才会返回。 ...

Leave a Comment