我的Django-blog学习(二):使用虚机模拟搭建内网博客

最近在学习追梦人生写Django博客教程,其中做到搭建服务器和域名时,由于都没有这些,只能使用贫困法玩玩啦。


第一步:配置虚机。

如教程所说,我用了Ubuntu的镜像,创建了新用户,更新了系统,安装了python、nginx等

[email protected]:~$ history
    2  sudo apt-get update
    3  sudo apt-get upgrade
    4  sudo apt-get install nginx
    5  sudo apt-get install git
    6  python3
    7  pip list
    8  sudo apt-get install python3-pip
    9  sudo pip3 install virtualenv
   10  sudo service nginx start

开启nginx后
在浏览器输入地址:
我的Django-blog学习(二):使用虚机模拟搭建内网博客

第二步:

在连接的虚机中创建好项目,创建好虚拟环境:

[email protected]:~/sites/demo.blog.com$ virtualenv --python=python3 env
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/liuxuan/sites/demo.blog.com/env/bin/python3
Also creating executable in /home/liuxuan/sites/demo.blog.com/env/bin/python
Installing setuptools, pip, wheel...done.

git clone自己的项目,保存到准备好的项目文件夹

git clone https://github.com/Arrowarcher/tanblog.git  
# 这是我自己的github,需要事先把代码上传的github等代码管理平台的仓库

现在的目录结构是这样的:
我的Django-blog学习(二):使用虚机模拟搭建内网博客

第三步

  • **虚拟环境,再进入到项目根目录,即 requirements.txt 所在的目录,安装项目的全部依赖:
    我的Django-blog学习(二):使用虚机模拟搭建内网博客
  • 收集静态文件
python manage.py collectstatic
  • 生成数据库表
    makemigrations、migrate
  • 超级用户
    python manage.py createsuperuser

重点来了

配置Nginx

由于我是用虚机模拟的服务器,也没有域名,配置Nginx自然也和博客中的不一样
原本的配置文件:

server {
    charset utf-8;
    listen 80;
    server_name demo.zmrenwu.com(域名); 

    location /static { 
        alias /home/yangxg/sites/demo.zmrenwu.com/django-blog-tutorial/static; 
    }

    location / { 
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/demo.zmrenwu.com.socket(域名.socket);
    }
}

由于我使用的是ip访问,而不是域名,这里的demo.zmrenwu.com(域名)换成ip地址就好,demo.zmrenwu.com.socket(域名.socket)换成ip,去掉.socket


运行服务

(env)[email protected]:~/sites/demo.blog.com/tanblog# gunicorn --bind unix:/tmp/10.8.0.76 tanblog.wsgi:application
[2018-10-24 10:17:50 +0800] [2036] [INFO] Starting gunicorn 19.8.1
[2018-10-24 10:17:50 +0800] [2036] [INFO] Listening at: unix:/tmp/10.8.0.76 (2036)
[2018-10-24 10:17:50 +0800] [2036] [INFO] Using worker: sync
[2018-10-24 10:17:50 +0800] [2039] [INFO] Booting worker with pid: 2039

我的Django-blog学习(二):使用虚机模拟搭建内网博客