VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法详解
本文实例讲述了VPSCENTOS上配置python,mysql,nginx,uwsgi,django的方法。分享给大家供大家参考,具体如下:
昨天试用了VPS,花了一天部署了一个简单应用。在下面的过程中省去了用django创建project的一步,忘记了你自己一用startporject创建。
下面是原来边操作,边记录的东西,我习惯文本编辑。可能格式不好看。现在搬到博客中来。
首先安装GCC.
yum-yinstallgccautomakeautoconflibtoolmake
给CENTOS安装中文包
查看CENTOS版本cat/etc/redhat-release我的是5.7在官方网站上找5.7的,没找到,用5.5的吧。
yumgroupinstallchinese-support vi/etc/sysconfig/i18n
内容如下:
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的字库。
wgethttp://ftp.dc.volia.com/pub/CentOS/5.5/os/x86_64/CentOS/fonts-chinese-3.02-12.el5.noarch.rpm wgethttp://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
wgethttp://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
1.
./configure-with-zlib=/usr/include(需要看zlib.h文件在那个目录下whereiszlib.h) makeinstall
2.建立软连接
cd/usr/bin rm-rfpython ln-s/usr/local/bin/python2.7python
这样做了之后,可能导致一个问题yum命令不能用,这时需要修改yum
vi/usr/bin/yum
修改第一行的python路径#!/usr/bin/python2.4因为centos是用的python2.4
安装PILpython库
wgethttp://effbot.org/downloads/Imaging-1.1.7.tar.gz pythonsetup.pyinstall
安装Django1.3
wgethttp://www.djangoproject.com/download/1.3/tarball/ pythonsetup.pyinstall
安装setuptools
wgethttp://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea shsetuptools-0.6c11-py2.7.egg–prefix=/usr/local
安装python-mysqldb
wgethttp://ncu.dl.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz yuminstallmysql-devel pythonsetup.pyinstall
安装MYSQL(CENTOS自带5.0)
yuminstallmysql-server
MYSQL登陆问题:
#/etc/init.d/mysqlstop #mysqld_safe--user=mysql--skip-grant-tables--skip-networking& #mysql-urootmysql mysql>UpdateuserSETPassword=PASSWORD('newpassword')whereUSER='root'; mysql>FLUSHPRIVILEGES; mysql>quit #/etc/init.d/mysqlrestart #mysql-uroot-p Enterpassword:<输入新设的密码newpassword> mysql>grantallprivilegeson*.*to'root'@'%'identifiedby'newpassword'withgrantoption;
安装UWSGI
wgethttp://projects.unbit.it/downloads/uwsgi-1.1.tar.gz
解压后
make cpuwsgi/usr/bin
注:在网上查看资料时,还有需要用pythonsetup.pybuild方式操作的,具体的,可以查下uwsgi的官网说明。
在你的django项目里面建立一个django_wsgi.py的文件,比如我的在/opt/www/uploadfile下
cd/opt/www/uploadfile vidjango_wsgi.py importos importsys sys.path.append("/opt/www")#与我project路径有关,修改成自己的 os.environ['DJANGO_SETTINGS_MODULE']='uploadfile.settings'#配置有关,修改成自己的 importdjango.core.handlers.wsgi application=django.core.handlers.wsgi.WSGIHandler()
建立目录/home/uwsgi
viuwsig.ini
内容如下:
[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
wgethttp://nginx.org/download/nginx-1.0.15.tar.gz yuminstallglib2-developenssl-develpcre-develbzip2-develgzip-devel
然后
./configure
可以看到安装后的路径:
nginxpathprefix:"/usr/local/nginx" nginxbinaryfile:"/usr/local/nginx/sbin/nginx" nginxconfigurationprefix:"/usr/local/nginx/conf" nginxconfigurationfile:"/usr/local/nginx/conf/nginx.conf" nginxpidfile:"/usr/local/nginx/logs/nginx.pid" nginxerrorlogfile:"/usr/local/nginx/logs/error.log" nginxhttpaccesslogfile:"/usr/local/nginx/logs/access.log" nginxhttpclientrequestbodytemporaryfiles:"client_body_temp" nginxhttpproxytemporaryfiles:"proxy_temp" nginxhttpfastcgitemporaryfiles:"fastcgi_temp" nginxhttpuwsgitemporaryfiles:"uwsgi_temp" nginxhttpscgitemporaryfiles:"scgi_temp" makeinstall
然后
cp/usr/local/nginx/sbin/nginx/usr/bin
运行nginx启动nginx.
如果要停止
nginx-sstop
nginx如何重启
如下命令:nginx-s reload当然也有一个reopen,具体区别自己去看吧。哥就不说了。
接下来是配置nginx与django的配合了。
cd/usr/local/nginx/conf vidjango_uwsgi.conf
内容如下:
server{ listen80; server_name216.24.200.212; location/{ uwsgi_pass127.0.0.1:9000; includeuwsgi_params; access_logoff; } location^~/static{ root/opt/www/uploadfile; } location^~/admin/{ uwsgi_pass127.0.0.1:9000; includeuwsgi_params; access_logoff; } 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_logoff; } }
然后打开nginx.conf编辑,在http{}中增加如下:
includedjango_uwsgi.conf; client_max_body_size20m;#这是为了控制上传文件大小用的
====到此配置基本完成,下面启动===========================
查看进程
ps-ef|grepuwsgi|grep-vgrep
如果uwsgi没启动,就如下操作
uwsgi--ini/home/uwsgi/uwsgi.ini
监听端口(俺的配置文件中用的9000)
netstat-an|grep9000 nginx-sreload
打开网页查看吧,比如
http://myipaddress
希望本文所述对大家基于Django框架的Python程序设计有所帮助。