1.apache安装,启动,停止
sudo apt-get install apache2
sudo service apache2 start
sudo service apache2 stop
2.安装mod_wsgi
sudo apt-get install libapache2-mod-wsgi
查看mod_wsgi.so在哪个目录
sudo find / -name mod_wsgi.so
3.安装webpy
sudo easy_install web.py
若出现sudo: easy_install: command not found
那么安装sudo apt-get install python-setuptools
4.安装MySQL
sudo apt-get install mysql-server mysql-client
5.安装MySQLdb
sudo apt-get install python-mysqldb
6.配置虚拟主机
cd /etc/apache2/sites-available
vi www-shen
ServerName www.shen.com
ServerAdmin liushen_shen@163.com
DocumentRoot /var/www/
ErrorLog ${APACHE_LOG_DIR}/www_shen_error.log
CustomLog ${APACHE_LOG_DIR}/www_shen_access.log combined
WSGIScriptAlias / /var/www/code.py/
Alias /static /var/www/static/
AddType text/html .py
Order deny,allow
Allow from all
7.测试
登陆mysql
mysql -u root -p
创建数据库
CREATE DATABASE IF NOT EXISTS db_test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
创建表
use db_test;
CREATE TABLE user(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(20) NOT NULL,
age int
);
向表里插入数据
insert into user(name, age) values('shen', 20);
insert into user(name, age) values('jack', 25);
最后是虚拟主机中配置的code.py
# -*- coding: utf-8 -*-
import web
urls = (
'/*', 'Index',
)
user = ''
passwd = ''
db = web.database(dbn='mysql', user=user, pw=passwd, db='db_test')
class Index:
def GET(self):
users = db.select('user')
result = ''
for user in users:
result += str(user.id)
result += ' '
result += user.name
result += ' '
result += str(user.age)
result += '\n'
return result
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
else:
application = web.application(urls, globals()).wsgifunc()