EC2上部署Django个人网站并连上Godaddy域名

项目命名为PersonalWebsite,后面各处取名时会用到。此项目数据库用的是Django自带的sqlite。

第一步:在AWS上创建新的EC2,选择Ubuntu Free Tier

这就是网站服务器了,可以通过ssh连接之。为了方便,写一个命令:$ ssh PersonalWebsite

# edit ~/.ssh/config

Host PersonalWebsite
HostName <ec2_hostname>
User ubuntu
IdentityFile "~/.ssh/<your_key_pair>.pem"

安装必要程序。可以通过执行写好的脚本文件

 

# filename: install.sh

sudo apt-get update && upgrade;
sudo apt-get install apache2 libapache2-mod-wsgi;
sudo apt-get install python-pip;
sudo pip install django;
sudo apt-get install mysql-server python-mysqldb;
sudo apt-get install git;

 

第二步 植入网站

先从github上拉下来: $ git clone <url>

我的Django网站文件结构如下

 

~/
    PersonalWebsite/
        App/
            __init__.py
            admin.py
            models.py
            tests.py
            urls.py
            views.py
        PersonalWebsite/
            __init__.py
            settings.py
            urls.py
            views.py
            wsgi.py
        static/
            css/
            images/
        template/
            app/
            other_html.html
        db.sqlite3
        manage.py

配置Apache服务器,在/etc/apache2/sites-enabled/PersonalWebsite.conf里声明wsgi.py和静态文件的路径。

 

WSGIScriptAlias / /home/ubuntu/PersonalWebsite/PersonalWebsite/wsgi.py
WSGIPythonPath /home/ubuntu/PersonalWebsite
<Directory /home/ubuntu/PersonalWebsite/PersonalWebsite>
    <Files wsgi.py>
        Order deny,allow
        Require all granted
    </Files>
</Directory>

Alias /media/ /home/ubuntu/PersonalWebsite/media/ 
Alias /static/ /home/ubuntu/PersonalWebsite/static/

<Directory /home/ubuntu/PersonalWebsite/static>
    Require all granted
</Directory>

<Directory /home/ubuntu/PersonalWebsite/media>
    Require all granted
</Directory> 

 

第三步 绑定域名

  1. 首先,在AWS里将EC2绑定一个Elastic IP(The Elastic IP address assigned to the instance, if applicable. Elastic IP addresses are static IP addresses assigned to your account that you can quickly remap to other instances.
    操作路径:AWS Dashboard => (左侧导航栏)Security & Network下的Elastic IP
     
  2. 在Godday.com购买了example.com域名
    在Domain => Domain Details => DNS ZONE FILE 中修改A(Host)的Record:
    使 @ (即此域名)对应上一步中的Elastic IP

 

小贴士

网站的更新流程可以是:在本地开发 --> 上传至Github --> 登入EC2 --> $ git pull --> 重启Apache