1. 程式人生 > 資料庫 >VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法詳解

VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法詳解

本文例項講述了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 –prefix=/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程式設計有所幫助。