您的位置:首页 > 运维架构 > Nginx

CentOS+nginx+uwsgi+Python 多站点环境搭建

2016-08-19 10:26 549 查看
环境:

CentOS X64 6.4

nginx 1.5.6

Python 2.7.5

正文:

一:安装需要的类库及Python2.7.5

安装必要的开发包

yum groupinstall "Development tools"

yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

CentOS 自带Python2.6.6,但我们可以再安装Python2.7.5:

cd ~
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 tar xvf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local
make && make altinstall

安装完毕后,可是使用”python2.7”命令进入python2.7的环境。

二:安装Python包管理

easy_install包 https://pypi.python.org/pypi/distribute

方便安装Python的开发包

cd ~
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz tar xf distribute-0.6.49.tar.gz
cd distribute-0.6.49
python2.7 setup.py install
easy_install --version

红色部分必须是“python2.7”,否则将安装到默认的2.6环境内。

pip包 https://pypi.python.org/pypi/pip

安装pip的好处是可以pip list、pip uninstall 管理Python包, easy_install没有这个功能,只有uninstall

easy_install pip
pip --version

三:安装uwsgi

uwsgi:https://pypi.python.org/pypi/uWSGI

uwsgi参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html

pip install uwsgi
uwsgi --version

测试uwsgi是否正常:

新建test.py文件,内容如下:

def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"

然后在终端运行:

uwsgi --http :8001 --wsgi-file test.py

在浏览器内输入:http://127.0.0.1:8001,看是否有“Hello World”输出,若没有输出,请检查你的安装过程。

四:安装django

pip install django

测试django是否正常,运行:

django-admin.py startproject demosite
cd demosite
python2.7 manage.py runserver 0.0.0.0:8002

在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。

五:安装nginx

cd ~
wget http://nginx.org/download/nginx-1.5.6.tar.gz tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 \
--with-http_stub_status_module \
--with-http_gzip_static_module
make && make install

六:配置uwsgi

uwsgi支持ini、xml等多种配置方式,但个人感觉ini更方便:

在/ect/目录下新建uwsgi9090.ini,添加如下配置:

[uwsgi]
socket = 127.0.0.1:9090
master = true         //主进程
vhost = true          //多站模式
no-stie = true        //多站模式时不设置入口模块和文件
workers = 2           //子进程数
reload-mercy = 10
vacuum = true         //退出、重启时清理文件
max-requests = 1000
limit-as = 512
buffer-sizi = 30000
pidfile = /var/run/uwsgi9090.pid    //pid文件,用于下面的脚本启动、停止该进程
daemonize = /website/uwsgi9090.log

设置uwsgi开机启动,在/ect/init.d/目录下新建uwsgi9090文件,内容如下:

server {
listen       80;
server_name  localhost;

location / {
include  uwsgi_params;
uwsgi_pass  127.0.0.1:9090;
uwsgi_param UWSGI_SCRIPT demosite.wsgi;
uwsgi_param UWSGI_CHDIR /website/demosite;
index  index.html index.htm;
client_max_body_size 35m;
}
}

server {
listen       1300;

location / {
include  uwsgi_params;
uwsgi_pass  127.0.0.1:9091;
uwsgi_param UWSGI_SCRIPT DjangoStudy.wsgi;
uwsgi_param UWSGI_CHDIR /website/DjangoStudy;
index  index.html index.htm;
}
}


然后我们就可以通过http://127.0.0.1:1300来访问新的网站了。

十:其他配置

防火墙设置

CentOS默认关闭外部对80、3306等端口的访问,所以要在其他计算机访问这台服务器,就必须修改防火墙配置,打开/etc/sysconfig/iptables

在“-A INPUT –m state --state NEW –m tcp –p –dport 22 –j ACCEPT”,下添加:

-A INPUT -m state --state NEW -m tcp -p -dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p -dport 3306 -j ACCEPT

然后保存,并关闭该文件,在终端内运行下面的命令,刷新防火墙配置:

service iptables restart

安装Mysqldb

yum -y install mysql-devel

easy_install-2.7 MySQL-python

注意红色部分,easy_install-2.7,否则它将默认安装到Python2.6环境内。

------------------------------------------------------------------------------------------------------------------

2014年12月02日添加:

CentOS 7中默认使用Firewalld做防火墙,所以修改iptables后,在重启系统后,根本不管用。

Firewalld中添加端口方法如下:

firewall-cmd --zone=public --add-port=3306/tcp [b]--permanent[/b]

firewall-cmd --reload
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: