django项目部署至linux云服务器

   当Django web项目在本地开发完毕后,这仅能自己浏览,需要将代码放到服务器上,这样用户才能访问,下文将详细介绍Django项目如何部署到linux服务器。

<font color=red>服务器环境介绍</font>

  • 操作系统:CentOS7
  • Python版本: 2.7.5
  • Nginx主配置文件位置:/etc/nginx/nginx.conf

<font color=red>安装基础开发包</font>

Centos 下安装步骤如下:

  • yum groupinstall "Development tools" -y
  • yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel -y

<font color=red>安装Python包管理工具</font>

easy_install包链接: https://pypi.python.org/pypi/distribute

安装步骤:

  • cd ~

  • yum install wget -y

  • wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz

  • ls

  • tar xf distribute-0.6.49.tar.gz

  • cd distribute-0.6.49

  • python setup.py install

CentOS7:
python2.7 setup.py install

CentOS6:
python2.7 setup.py install

测试是否安装成功:
easy_install --version

<font color=red>安装 uwsgi</font>

sudo yum install python-devel -y

1、安装wget,gcc-c++ gcc*:

yum install -y wget gcc-c++ epel-release -y

2、安装pip

yum install python-pip -y

3、升级pip

pip install --upgrade pip

4、Uwsgi安装

pip install uwsgi

Successfully installed uwsgi-2.0.17.1

5、查看 uwsgi 版本

uwsgi --version

CentOS 7.0使用的是firewall作为防火墙,默认防火墙是打开的
查看防火墙状态:
firewall-cmd --state

停止firewall:
systemctl stop firewalld.service

禁止firewall开机启动:
systemctl disable firewalld.service

uwsgi:https://pypi.python.org/pypi/uWSGI

uwsgi 参数详解:

http://uwsgi-docs.readthedocs.org/en/latest/Options.html

测试 uwsgi 是否正常:

新建 test.py 文件,内容如下:

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

然后在终端运行:
uwsgi --http :8001 --wsgi-file test.py

在浏览器内输入:
http://服务器IP地址:8001

查看是否有"Hello World"输出,若没有输出,请检查你的安装过程。

<img alt="django项目部署至linux云服务器-Picture2.png" src="assets/django项目部署至linux云服务器-Picture2.png" width="" height="" >

<font color=red>Django 安装</font>

Django 版本对应的Python 版本:

Django 版本 Python 版本
1.8 2.7, 3.2 , 3.3, 3.4, 3.5
1.9, 1.10 2.7, 3.4, 3.5
1.11 2.7, 3.4, 3.5, 3.6
2.0 3.5+

1、Django安装
pip install django==1.8

2、Python版本查看:

方式一:pip list
方式二:python
      import django
      print(django.get_version())

3、测试django:

cd ~
django-admin.py startproject mysite
cd mysite

进入项目文件夹
/data/wwwroot/mysite
,添加static和templates,分别用于存放静态文件和模板文件。

django-admin.py startapp blog
允许所有主机访问
增加应用

python manage.py runserver 0.0.0.0:8002
(注意0.0.0.0这个IP地址)

在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。

<font color=red>安装Nginx</font>

yum install nginx -y

Nginx主配置文件:
/etc/nginx/nginx.conf

看服务器上安装的nginx版本号,主要是通过ngix的-v或-V选项,查看方法如下图所示:

<img alt="django项目部署至linux云服务器-Picture1.png" src="assets/django项目部署至linux云服务器-Picture1.png" width="" height="" >

-v 显示 nginx 的版本。

-V 显示 nginx 的版本,编译器版本和配置参数。

有时想知道nigix是否在正常运行,需要用linux命令查看nginx运行情况。执行命令:

ps -A | grep nginx

如果返回结果的话,说明有nginx在运行,服务已经启动。

nginx启动、停止、无间断服务重启:

service nginx start

service nginx stop

service nginx reload

在浏览器中输入IP地址:
<img alt="django项目部署至linux云服务器-Picture3.png" src="assets/django项目部署至linux云服务器-Picture3.png" width="" height="" >

出现如下界面:
<img alt="django项目部署至linux云服务器-Picture4.png" src="assets/django项目部署至linux云服务器-Picture4.png" width="" height="" >

则nginx安装成功!

<font color=red>uwsgi 配置</font>

Django正常运行之后我们就开始配置一下uwsgi。

我们网站项目路径是 /data/wwwroot/mysite/,在项目根目录下创建
mysite.xml文件,输入如下内容:

<uwsgi>
   <socket>127.0.0.1:8997</socket><!-- 内部端口,自定义 -->
   <chdir>/data/wwwroot/mysite/</chdir><!-- 项目路径 -->
   <module>mysite.wsgi</module>
   <processes>4</processes> <!-- 进程数 -->
   <daemonize>uwsgi.log</daemonize><!-- 日志文件 -->
</uwsgi>

并保存
配置文件里的mysite这是我们的项目名。

<font color=red>配置nginx.conf文件</font>

首先切换到nginx的配置文件所在路径:
cd /etc/nginx

备份一下nginx.conf文件,以防意外,

cp nginx.conf nginx.conf.bak

然后打开nginx.conf,把原来的内容删除,

rm -rf nginx.conf

直接加入以下内容:(nginx.conf)

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen       80;
        server_name  www.django.cn;
        charset utf-8;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8997;
           uwsgi_param UWSGI_SCRIPT mysite.wsgi;
           uwsgi_param UWSGI_CHDIR /data/wwwroot/mysite; #项目路径

        }
        location /static/ {
        alias /data/wwwroot/mysite/static/; #静态资源路径
        }
    }
}

注意该配置文件中的路径要和mysite.xml项目路径对应上。

执行以下命令先检查配置文件是否有错
nginx -t,没有错就执行以下命令:nginx

终端没有任何提示就证明nginx启动成功。可以使用你的服务器地址查看,成功之后就会看到一个nginx欢迎页面。

<font color=red>访问项目的页面</font>

进入网站项目目录:
cd /data/wwwroot/mysite/

执行下面命令:
uwsgi -x mysite.xml

以上步骤都没有出错的话。
执行一下命令重启nginx 。

nginx -s reload

然后在浏览器里访问你的项目地址!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。