本文实例讲述了VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法。分享给大家供大家参考,具体如下:
昨天试用了VPS,花了一天部署了一个简单应用。在下面的过程中省去了用django 创建project的一步,忘记了你自己一用startporject 创建。
下面是原来边操作,边记录的东西,我习惯文本编辑。可能格式不好看。现在搬到博客中来。
首先安装GCC.
1
|
yum -y install gcc automake autoconf libtool make
|
给CENTOS 安装中文包
查看 CENTOS 版本 cat /etc/redhat-release 我的是 5.7 在官方网站上找 5.7 的,没找到,用5.5的吧。
1
2
|
yum groupinstall chinese-support
vi /etc/sysconfig/i18n
|
内容如下:
1
2
3
|
LANG= "zh_CN.UTF-8"
SUPPORTED= "zh_CN.UTF-8:zh_CN:zh:en_US.UTF-8:en_US:en"
SYSFONT= "latarcyrheb-sun16"
|
使用locale命令查看系统语言设置:
locale
下面用了 5.5 的字库。
1
2
|
wget http: //ftp . dc .volia.com /pub/CentOS/5 .5 /os/x86_64/CentOS/fonts-chinese-3 .02-12.el5.noarch.rpm
wget http: //ftp . dc .volia.com /pub/CentOS/5 .5 /os/x86_64/CentOS/fonts-ISO8859-2-75dpi-1 .0-17.1.noarch.rpm
|
rpm -ivh 安装就不说了。
安装完毕,然后reboot
安装python2.7.2
1
|
wget http: //www .python.org /ftp/python/2 .7.2 /Python-2 .7.2.tgz
|
1.
1
2
|
. /configure -with-zlib= /usr/include (需要看zlib.h文件在那个目录下 whereis zlib.h)
make install
|
2. 建立软连接
1
2
3
|
cd /usr/bin
rm -rf python
ln -s /usr/local/bin/python2 .7 python
|
这样做了之后,可能导致一个问题yum 命令不能用,这时需要修改yum
1
|
vi /usr/bin/yum
|
修改第一行的python路径 #!/usr/bin/python2.4 因为centos 是用的python2.4
安装PIL python 库
1
2
|
wget http: //effbot .org /downloads/Imaging-1 .1.7. tar .gz
python setup.py install
|
安装Django 1.3
1
2
|
wget http: //www .djangoproject.com /download/1 .3 /tarball/
python setup.py install
|
安装setuptools
1
2
|
wget http: //pypi .python.org /packages/2 .7 /s/setuptools/setuptools-0 .6c11-py2.7.egg #md5=fe1f997bc722265116870bc7919059ea
sh setuptools-0.6c11-py2.7.egg –prefix= /usr/local
|
安装python-mysqldb
1
2
3
|
wget http: //ncu .dl.sourceforge.net /project/mysql-python/mysql-python/1 .2.3 /MySQL-python-1 .2.3. tar .gz
yum install mysql-devel
python setup.py install
|
安装MYSQL (CENTOS自带 5.0)
1
|
yum install mysql-server
|
MYSQL 登陆问题:
1
2
3
4
5
6
7
8
9
10
|
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> Update user SET Password=PASSWORD( 'newpassword' ) where USER= 'root' ;
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysql restart
# mysql -uroot -p
Enter password: <输入新设的密码newpassword>
mysql> grant all privileges on *.* to 'root' @ '%' identified by 'newpassword' with grant option;
|
安装UWSGI
1
|
wget http: //projects .unbit.it /downloads/uwsgi-1 .1. tar .gz
|
解压后
1
2
|
make
cp uwsgi /usr/bin
|
注:在网上查看资料时,还有需要用python setup.py build 方式操作的,具体的,可以查下uwsgi的官网说明。
在你的django 项目里面建立一个django_wsgi.py 的文件,比如我的在/opt/www/uploadfile下
1
2
3
4
5
6
7
8
|
cd /opt/www/uploadfile
vi django_wsgi.py
import os
import sys
sys.path.append( "/opt/www" ) #与我project路径有关,修改成自己的
os.environ[ 'DJANGO_SETTINGS_MODULE' ] = 'uploadfile.settings' #配置有关,修改成自己的
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
|
建立目录 /home/uwsgi
1
|
vi uwsig.ini
|
内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[uwsgi]
socket=127.0.0.1:9000
listen=200
master= true
pidfile= /usr/local/nginx/uwsgi .pid
processes=8
pythonpath= /opt/www/uploadfile
pythonpath= /opt/www/
module=django_wsgi
profiler= true
memory-report= true
enable -threads= true
logdate= true
limit-as=6048
daemonize= /opt/www/logs/django .log
|
运行 uwsgi --ini /home/uwsgi/uwsgi.ini
安装nginx
1
2
|
wget http: //nginx .org /download/nginx-1 .0.15. tar .gz
yum install glib2-devel openssl-devel pcre-devel bzip2 -devel gzip -devel
|
然后
1
|
. /configure
|
可以看到安装后的路径:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
nginx path prefix: "/usr/local/nginx"
nginx binary file : "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file : "/usr/local/nginx/conf/nginx.conf"
nginx pid file : "/usr/local/nginx/logs/nginx.pid"
nginx error log file : "/usr/local/nginx/logs/error.log"
nginx http access log file : "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
make install
|
然后
1
|
cp /usr/local/nginx/sbin/nginx /usr/bin
|
运行nginx 启动nginx.
如果要停止
1
|
nginx -s stop
|
nginx 如何重启
如下命令:nginx -s reload
当然也有一个 reopen ,具体区别自己去看吧。哥就不说了。
接下来是配置 nginx 与 django 的配合了。
1
2
|
cd /usr/local/nginx/conf
vi django_uwsgi.conf
|
内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
server {
listen 80;
server_name 216.24.200.212;
location / {
uwsgi_pass 127.0.0.1:9000;
include uwsgi_params;
access_log off;
}
location ^~ /static {
root /opt/www/uploadfile ;
}
location ^~ /admin/ {
uwsgi_pass 127.0.0.1:9000;
include uwsgi_params;
access_log off;
}
location ~* ^.+\.
(mpg|avi|mp3|swf|zip|pdf|jpg|gif|png|bmp|jpeg|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt| tar |mid|midi|wav|rtf|mpeg|js|css)$ {
root /opt/www/uploadfile/static ;
access_log off;
}
}
|
然后打开nginx.conf 编辑,在http{}中增加如下:
1
2
|
include django_uwsgi.conf;
client_max_body_size 20m; #这是为了控制上传文件大小用的
|
====到此配置基本完成,下面启动===========================
查看进程
1
|
ps -ef| grep uwsgi| grep - v grep
|
如果uwsgi 没启动,就如下操作
1
|
uwsgi --ini /home/uwsgi/uwsgi .ini
|
监听端口(俺的配置文件中用的9000)
1
2
|
netstat -an| grep 9000
nginx -s reload
|
打开网页查看吧,比如
http://myipaddress
希望本文所述对大家基于Django框架的Python程序设计有所帮助。