在免费亚马逊EC2上编译安装nginx+uwsgi+bottle

之前做的Python小站点均是用nginx反代方式,实际使用python app.py方式启动,显着是非常不专业啊。而且效率也没测试过。

真正对Python感兴趣时候是从[observer专栏杂记]的 使用python爬虫抓站的一些技巧总结等文章开始的。So,之前因为部署uwsgi太麻烦虽然收藏了observer的配置Nginx+uwsgi更方便地部署python应用文章,但一直没有实际去配置过。

为了表示不停折腾精神,这几天把网址缩短项目用bottle重写一下(bottle只有一个文件,比tornado轻量多了,反正俺也无需考虑效率问题),部署在一个亚马逊免费的EC2(Elastic Compute Cloud)上。此次修改Todo list还包含引入免费的redis(使用redis4you提供的免费5M服务)等。

所以趁此从头开始的机会,配置了一下uwsgi。记录下完整安装过程(其中好多安装配置copy自lnmp.org的centos.sh中配置)。

申请亚马逊免费EC2的教程不提供了,自己Google一下,网上有详细的图文教程的。

  • 一些简单配置

    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

    export PATH

  • 安装redis支持

    easy_install redis

  • 安装必备组件

    yum -y install openssl openssl-devel ncurses ncurses-devel python-devel

  • 添加用户用户组/设置相应的权限

    groupadd mysql

    useradd -s /sbin/nologin -M -g mysql mysql

    groupadd www

    useradd -s /sbin/nologin -g www www

    mkdir -p /home/wwwroot

    chmod +w /home/wwwroot

    mkdir -p /home/wwwlogs

    chmod 777 /home/wwwlogs

    chown -R www:www /home/wwwroot

  • pcre 8.20安装

    wget -c http://sourceforge.net/projects/pcre/files/latest/download?source=files

    tar xvf pcre-8.20.tar.bz2

    ./configure

    make && make install

  • nginx 1.0.9安装(其中nginx.conf和init.d.nginx等可以在这里下载)

    wget http://nginx.org/download/nginx-1.0.9.tar.gz

    tar zxvf nginx-1.0.9.tar.gz

    cd nginx-1.0.9

    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module

    make && make install

    mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

    cp /home/ec2-user/conf/nginx.conf /usr/local/nginx/conf/nginx.conf

    mkdir -p /home/wwwroot/au81.com

    chmod -R 755 /home/wwwroot/au81.com/

    chown -R www:www /home/wwwroot/au81.com/

    cp /home/ec2-user/conf/init.d.nginx /etc/init.d/nginx

    chmod +x /etc/init.d/nginx

  • mysql安装

    wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.59.tar.gz/from/http://mysql.he.net/

    tar -zxvf mysql-5.1.59.tar.gz

    cd mysql-5.1.59

    ./configure --prefix=/usr/local/mysql --with-extra-charsets=all --enable-thread-safe-client --enable-assembler --with-charset=utf8 --enable-thread-safe-client --with-extra-charsets=all --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile

    make && make install

    cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf

    sed -i 's/skip-locking/skip-external-locking/g' /etc/my.cnf

    /usr/local/mysql/bin/mysql_install_db --user=mysql

    chown -R mysql /usr/local/mysql/var

    chgrp -R mysql /usr/local/mysql/.

    cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql

    chmod 755 /etc/init.d/mysql

    cat > /etc/ld.so.conf.d/mysql.conf<

    /usr/local/mysql/lib/mysql

    /usr/local/lib

    EOF

    ldconfig

    ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql

    ln -s /usr/local/mysql/include/mysql /usr/include/mysql

    /etc/init.d/mysql start

    ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

    ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump

    ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk

    /usr/local/mysql/bin/mysqladmin -u root password ********

    /etc/init.d/mysql restart

    /etc/init.d/mysql stop

  • python-mysql安装

    wget http://sourceforge.net/projects/mysql-python/files/latest/download?source=files

    tar xzf MySQL-python-1.2.3.tar.gz

    cd MySQL-python-1.2.3

    python setup.py build

    python setup.py install

  • 设置mysql nginx服务自动启动

    chkconfig --level 345 nginx on

    chkconfig --level 345 mysql on

  • uwsgi 0.9.9.2安装

    yum install libxml2 libxml2-devel

    wget http://projects.unbit.it/downloads/uwsgi-0.9.9.2.tar.gz

    tar -zxvf uwsgi-0.9.9.2.tar.gz

    cd uwsgi-0.9.9.2/

    python setup.py build

    make

    cp ./uwsgi /usr/local/sbin/

    uwsgi -x /home/uwsgi.xml

经典的helloword(文件名为app.py)

import os

from bottle import route, run, default_app

@route('/')

def main():

return "Hello world!"

if __name__ == "__main__":

# Interactive mode

run()

else:

# Mod WSGI launch

os.chdir(os.path.dirname(__file__))

application = default_app()

添加新评论 »