本文实例讲述了VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法。分享给大家供大家参考,具体如下:
昨天试用了VPS,花了一天部署了一个简单应用。在下面的过程中省去了用django 创建project的一步,忘记了你自己一用startporject 创建。
下面是原来边操作,边记录的东西,我习惯文本编辑。可能格式不好看。现在搬到博客中来。
首先安装GCC.
yum -y install gcc automake autoconf libtool make
给CENTOS 安装中文包
查看 CENTOS 版本 cat /etc/redhat-release 我的是 5.7 在官方网站上找 5.7 的,没找到,用5.5的吧。
yum groupinstall chinese-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 的字库。
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
wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
1.
./configure -with-zlib=/usr/include (需要看zlib.h文件在那个目录下 whereis zlib.h) make install
2. 建立软连接
cd /usr/bin rm -rf python ln -s /usr/local/bin/python2.7 python
这样做了之后,可能导致一个问题yum 命令不能用,这时需要修改yum
vi /usr/bin/yum
修改第一行的python路径 #!/usr/bin/python2.4 因为centos 是用的python2.4
安装PIL python 库
wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz python setup.py install
安装Django 1.3
wget http://www.djangoproject.com/download/1.3/tarball/ python setup.py install
安装setuptools
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 �Cprefix=/usr/local
安装python-mysqldb
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)
yum install mysql-server
MYSQL 登陆问题:
# /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
wget http://projects.unbit.it/downloads/uwsgi-1.1.tar.gz
解压后
make cp uwsgi /usr/bin
注:在网上查看资料时,还有需要用python setup.py build 方式操作的,具体的,可以查下uwsgi的官网说明。
在你的django 项目里面建立一个django_wsgi.py 的文件,比如我的在/opt/www/uploadfile下
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
vi uwsig.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
wget http://nginx.org/download/nginx-1.0.15.tar.gz yum install glib2-devel openssl-devel pcre-devel bzip2-devel gzip-devel
然后
./configure
可以看到安装后的路径:
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
然后
cp /usr/local/nginx/sbin/nginx /usr/bin
运行nginx 启动nginx.
如果要停止
nginx -s stop
nginx 如何重启
如下命令:
nginx -s reload
当然也有一个 reopen ,具体区别自己去看吧。哥就不说了。
接下来是配置 nginx 与 django 的配合了。
cd /usr/local/nginx/conf vi django_uwsgi.conf
内容如下:
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{}中增加如下:
include django_uwsgi.conf; client_max_body_size 20m; #这是为了控制上传文件大小用的
====到此配置基本完成,下面启动===========================
查看进程
ps -ef|grep uwsgi|grep -v grep
如果uwsgi 没启动,就如下操作
uwsgi --ini /home/uwsgi/uwsgi.ini
监听端口(俺的配置文件中用的9000)
netstat -an|grep 9000 nginx -s reload
打开网页查看吧,比如
http://myipaddress
希望本文所述对大家基于Django框架的Python程序设计有所帮助。