webpy mysql_linux下安装Apache+mod_wsgi+webpy+MySQL+MySQLdb

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值