CentOS VPS 部署 Python Django Nginx UWSGI 完整指南

Ruby
Ruby
发布时间:2026-10-13 | 5人浏览过
原创

本文详细介绍在 CentOS VPS 上配置 Python、MySQL、Nginx 和 UWSGI 以运行 Django 应用的完整流程。涵盖 GCC 及中文包安装、Python 2.7.2 源码编译与软链接设置、Django 1.3 及相关库安装、MySQL 5.0 配置与密码重置、UWSGI 部署及 Nginx 反向代理配置。

本文详细介绍在 CentOS VPS 上配置 Python、MySQL、Nginx 和 UWSGI 以运行 Django 应用的完整流程,供读者参考。

在试用 VPS 并花费一天时间部署简单应用的过程中,本文省略了使用 Django 创建项目的步骤,读者可自行通过 startproject 命令创建项目。

以下内容记录了实际操作过程,采用文本编辑格式整理至博客,可能排版较为简单。

系统基础环境配置

首先,需要安装 GCC 编译器。


yum -y install gcc automake autoconf libtool make

接下来为 CentOS 系统安装中文支持包。

通过命令 cat /etc/redhat-release 查看 CentOS 版本,示例中为 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 命令进行安装,具体过程略。

安装完成后,重启系统使更改生效。

Python 环境搭建

随后安装 Python 2.7.2 版本。


wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz

第一步,下载 Python 源码包。


./configure -with-zlib=/usr/include (需要看zlib.h文件在那个目录下 whereis zlib.h)
make install

第二步,建立 Python 的软链接。


cd /usr/bin
rm -rf python
ln -s /usr/local/bin/python2.7 python

执行上述操作后,可能会导致 yum 命令无法使用,此时需要修改 yum 配置文件。


vi /usr/bin/yum

修改 yum 脚本第一行的 python 路径为 #!/usr/bin/python2.4,因为 CentOS 系统默认使用 Python 2.4。

Django 及相关库安装

接着安装 Python 图像处理库 PIL。


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 数据库配置

安装 MySQL 数据库,CentOS 5.7 系统通常自带 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 服务器部署

安装 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 用于存放 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 启动 UWSGI 服务。

Nginx 安装与配置

安装 Nginx Web 服务器。


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 -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 程序设计配置方法对大家有所帮助。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表