django开发个人简易Blog—nginx+uwsgin+django1.6+mysql 部署到CentOS6.5
前面说完了此项目的创建及数据模型设计的过程。如果未看过,可以到这里查看,并且项目源码已经放大到github上,可以去这里下载。
代码也已经部署到sina sea上,地址为http://fengzheng.sinaapp.com/
先跳过视图展示及表单处理的部分,先介绍一下如何部署。
标题中已经把部署环境介绍的很清楚了:
- 服务器:CentOS6.5 其实就是我的开发机
- mysql:Server version: 5.1.73 Source distribution
- nginx版本: nginx/1.6.0
- python版本:2.7.3
- django版本:(1, 6, 5, 'final', 0)
- uwsgi
下面介绍一下我的部署过程,仅仅是我的部署过程,针对不同的配置可能会有所不同,仅供参考。
有些软件需要在线安装,而linux的默认源是国外的,下载速度特别慢,可以先设置一个国内源,我这里设置的是163源,下载速度还是很快的.
1、进入存放源配置的文件夹
cd /etc/yum.repos.d
2、备份默认源
mv ./CentOS-Base.repo ./CentOS-Base.repo.backup
3、使用wget下载163的源
wget http://mirrors.163.com/.help/CentOS-Base-163.repo
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
4、把下载下来的文件CentOS-Base-163.repo设置为默认源
mv CentOS-Base-163.repo CentOS-Base.repo
mv CentOS6-Base-163.repo CentOS-Base.repo
1.安装mysql:
CentOS6.5默认的mysql版本就是5.1.73,所以如果不是有特殊要求的话,可以不进行更改。如果有要求的话,可以卸载自带的mysql,重新安装需要的版本。
这里有一篇介绍用yum命令安装mysql的文章,可以参考安装。当然,还可以下载源码,解压缩,编译,安装。过程就不做过多介绍了。
mysql的常用命令:
- 检查mysql服务状态
- # service mysqld status
- 启动mysql服务,要启动mysql必须有权限 一般之前会用su命令,输入管理员密码
- # service mysqld start
- 停止mysql服务
- # service mysqld stop
- 重启
- # service mysqld restart
- 登录 用root身份
- # mysql -u root –p
- 显示所有数据库
- # show databases;
- 使用myblog数据库
- # use myblog;
- 显示所有表
- # show tables;
2.升级python到2.7.3:
由于CentOS6.5默认的python版本是2.6的版本,所以需要升级。下面给出源码安装的方法:
- #下载python2.7.3源码压缩包
- wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
- #解压缩
- tar xf Python-2.7.3.tar.bz2
- #进入解压缩后的目录
- cd Python-2.7.3
- #配置及环境检查
- ./configure
- #安装
- make install
安装之后,在终端窗口中输入python,可以查看python版本是否已经是2.7.3的版本。
注:这样升级之后可能会导致yum命令失效,
因为yum依赖于ContOS系统默认的python版本,而升级python之后,yum脚本中的python版本被修改为最新版本,此时需要改回为原来的python版本,ContOS6.5默认的python版本为python2.6.6,解决方法如下:
进入yum所在目录
cd /usr/bin
su
vim yum
将第一行
#!/usr/bin/python2.7
改为:
#!/usr/bin/python2.6输入:wq! 强制保存
3.安装MySQLdb模块:
需要到这里下载源码压缩包,目前最新版本是1.2.3。安装过程:
cd
/home/fengzheng/Soft/ #进入压缩包所在目录
tar
-zxf MySQL-python-1.2.3.
tar
.gz #解压
cd
MySQL-python-1.2.3 #进入解压后的目录
python setup.py build #编译
python setup.py
install #安装
安装完成后,可以在终端窗口中输入以下命令测试是否安装成功,如果没有出现错误信息,则说明安装成功。
- python
- import MySQLdb
4.安装django:
这个不多说,可以到django官网下载源码,然后用命令进行源码安装:
cd
/home/fengzheng/Soft/
tar
-zxf Django-1.6.5.
tar
.gz
cd
Django-1.6.5/
python setup.py
install
也可以用官网上提供的在线安装方法,需要pip的支持:pip install Django==1.6.5
5.安装uwsgi:
export LDFLAGS="-Xlinker --no-as-needed" $ pip install uwsgi
测试uwsgi是否安装成功:
新建一个uwsgiTest.py文件,代码如下:
- #-*- coding:utf-8 -*-
- def application(env, start_response):
- start_response('200 OK', [('Content-Type','text/html')])
- return "Hello uwsgi"
进入文件所在目录,执行命令:
- uwsgi --http :1989 --wsgi-file uwsgiTest.py
之后,在浏览器访问http://127.0.0.1:1989 ,如果出现Hello uwsgi字样,说明uwsgi安装成功。
6.安装nginx:
在这里下载CentOS6.x所需的nginx所需的rpm文件。运行命令:
su
rpm –ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install nginx
nginx常用命令:
- #查看nginx安装位置:
- whereis nginx
- #查看ngin状态
- service nginx status
- 启动Nginx:
- /usr/sbin/nginx
- 或者直接输入 nginx
- 或者 service nginx start
- #停止nginx
- service nginx stop
- #重启
- service nginx restart
- 或者
- nginx -s reload
- 注:启动服务必须具有管理员权限 即之前要有su命令
有时候会出现异常:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
出现此问题是因为80端口被Nginx自己占用,解决方法为:
fuser -k 80/tcp
然后再启动Nginx
最后访问http://127.0.0.1 ,看到如下界面,说明nginx安装正确并成功启动:
7.配置uwsgi与nginx支持django:
uwsgi和nginx都可以单独工作,我们要把这两者联系起来,用来支持django项目。
首先我们打开项目所在目录,在根目录,也就是manage.py所在的目录新建一个django_uwsgi.py的文件,这个文件是要django以uwsgi的方式来运行,文件内容如下:代码中注释的那两行是manage.py运行django的方式,可以看出有什么不同。
- """
- WSGI config for fengzhengBlog project.
- It exposes the WSGI callable as a module-level variable named ``application``.
- For more information on this file, see
- https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/
- """
- import os
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fengzhengBlog.settings")
- #from django.core.wsgi import get_wsgi_application
- #application = get_wsgi_application()
- from django.core.handlers.wsgi import WSGIHandler
- application = WSGIHandler()
为了实现Nginx与uWSGI的连接,两者之间将采用soket来通讯方式,还需要在项目根目录,即和上面的django_uwsgi.py同一目录新建一个文件来实现,文件格式可以是xml,命名为django_socket.xml,内容如下:
- <uwsgi>
- <socket>:8077</socket>
- <chdir></chdir>
- <module>django_uwsgi</module><!-- 指定模块 即上面创建的django_uwsgi.py的名称 -->
- <processes>4</processes> <!-- 进程数 -->
- <daemonize>uwsgi.log</daemonize>
- </uwsgi>
或者是ini格式,命名为django_socket.ini,内容如下:
- [uwsgi]
- vhost = false
- socket = 127.0.0.1:8077 ;通信端口
- master = true
- enable-threads = true
- workers = 4
- wsgi-file = django_uwsgi.py ;指定模块 即上面创建的django_uwsgi.py
配置nginx,用weheris nginx命令查看nginx的安装目录在/etc/nginx,进入此目录,用vim打开nginx.conf配置文件,修改内容:
- user nginx;
- worker_processes 1;
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- #tcp_nopush on;
- keepalive_timeout 65;
- #gzip on;
- include /etc/nginx/conf.d/*.conf;
- server {
- listen 80; #80端口
- server_name 127.0.0.1; #最后访问的地址
- access_log /home/fengzheng/mypython/access.log; #日志
- error_log /home/fengzheng/mypython/error.log;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- include uwsgi_params;
- uwsgi_pass 127.0.0.1:8077; #前面django_socket.xml或django_socket.ini文件中配置的端口
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- #以下配置的静态文件
- location /css/ {
- alias /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/css/; }
- location /js/ {
- alias /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/js/;
- }
- location /images/ {
- alias /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/images/; }
- location /ueEditor/ {
- alias /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/ueEditor/; }
- }
- #以下是另一个项目配置
- server {
- listen 81;
- server_name 127.0.0.1;
- access_log /home/fengzheng/mypython/accessue.log;
- error_log /home/fengzheng/mypython/errorue.log;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- include uwsgi_params;
- uwsgi_pass 127.0.0.1:8088;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- location /upload/ {
- alias /home/fengzheng/ueEditor_django/ueEditor_django/upload/; }
- location /UE/ {
- alias /home/fengzheng/ueEditor_django/ueEditor_django/UE/; }
- }
- }
上面配置了两个server,即可以支持两个django站点。如果只有一个,可以将下面的server节点去掉。注意location节点的配置,如:
location /css/ {
alias /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/css/; }
location后面跟的是项目中的静态文件的目录前后都要有“/”,alias后面是静态文件所在的目录。对应urls.py中的路由配置:
- ( r'^css/(?P<path>.*)$', 'django.views.static.serve',
- { 'document_root': ROOT+'/css' }
- ),
- ( r'^js/(?P<path>.*)$', 'django.views.static.serve',
- { 'document_root': ROOT+'/js' }
- ),
- ( r'^images/(?P<path>.*)$', 'django.views.static.serve',
- { 'document_root': ROOT+'/images' }
- ),
- ( r'^ueEditor/(?P<path>.*)$', 'django.views.static.serve',
- { 'document_root': ROOT+'/ueEditor' }
- ),
在上面的设置后,可以让Nginx来处理静态文件 。非静态文件请求Nginx会发给 socket 8077,然后让uWSGI来进行处理。
8.启动网站:
配置完成后,重启nginx : nginx -s reload
启动uwsgi服务:
进入项目根目录,即前面创建的django_uwsgi.py所在的目录。
运行如下命令,使用django_socket.xml配置:
- uwsgi -x django_socket.xml
如果系统不支持-x命令,可以运行下面的命令启动django_socket.ini配置:
- uwsgi --ini django_socket.ini
启动成功后,会得到如下信息:
- [uWSGI] getting INI configuration from django_socket.ini
- *** Starting uWSGI 2.0.5.1 (64bit) on [Thu Jul 17 01:02:06 2014] ***
- compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-4) on 22 June 2014 20:36:27
- os: Linux-2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013
- nodename: localhost
- machine: x86_64
- clock source: unix
- detected number of CPU cores: 1
- current working directory: /home/fengzheng/blog/fengzhengBlog
- detected binary path: /usr/local/bin/uwsgi
- !!! no internal routing support, rebuild with pcre support !!!
- your processes number limit is 1024
- your memory page size is 4096 bytes
- detected max file descriptor number: 1024
- lock engine: pthread robust mutexes
- thunder lock: disabled (you can enable it with --thunder-lock)
- uwsgi socket 0 bound to TCP address 127.0.0.1:8077 fd 3
- Python version: 2.7.3 (default, Jun 20 2014, 02:55:10) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
- Python main interpreter initialized at 0x17db280
- python threads support enabled
- your server socket listen backlog is limited to 100 connections
- your mercy for graceful operations on workers is 60 seconds
- mapped 363840 bytes (355 KB) for 4 cores
- *** Operational MODE: preforking ***
- WSGI app 0 (mountpoint='') ready in 10 seconds on interpreter 0x17db280 pid: 7871 (default app)
- *** uWSGI is running in multiple interpreter mode ***
- spawned uWSGI master process (pid: 7871)
- spawned uWSGI worker 1 (pid: 7873, cores: 1)
- spawned uWSGI worker 2 (pid: 7874, cores: 1)
- spawned uWSGI worker 3 (pid: 7875, cores: 1)
- spawned uWSGI worker 4 (pid: 7876, cores: 1)
查看上述信息,发现启动成功,开启了4个线程。
更详细的安装配置可查看http://django-china.cn/topic/101/#top和http://django-china.cn/topic/124/讲的很详细。
之后访问站点:http://127.0.0.1 即可查看效果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)