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

<VirtualHost *:80>
    ServerName www.shen.com
    ServerAdmin [email protected]
    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
    <Directory /var/www/>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>


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






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章