Home >  > Linode Ubuntu部署Django记录

Linode Ubuntu部署Django记录

0

python做网站部署起来真的太麻烦了!

1apt-get update && apt-get upgrade
2 
3hostnamectl set-hostname django-server
4hostname  #查看结果
5nano /etc/hosts
6 
7 
8编辑host文件,在127.0.0.1下在加入以下这行,ctrl+X
9198.59.119.183  django-server
10 
11adduser coreyms
12adduser coreyms sudo
13exit
14 
15ssh coreyms@198.59.119.183  #重新登陆
16pwd  #在这/home/coreyms
17mkdir -p -/.ssh
18ls -la

切换到本地电脑

1ssh-keygen -b 4096  #使用默认路径
2scp -/.ssh/id-rsa.pub coreyms@198.58.119.183:-/.ssh/authorized_keys
3   #上传key

服务器上查看key

1ls.ssh
2sudo chmod 700 -/.ssh/
3sudo chmod 600 -/.ssh/*
4exit  #测试登陆
5sudo nano /etc/ssh/sshd_config
6#修改以下两项
7permitRootlogin no
8passswordAuthentication no
9#重启
10sudo systemctl restart sshd
1sudo apt-get install ufw
2sudo ufw default allow outgoing
3sudo ufw default deny incoming
4sudo ufw allo ssh
5sudo ufw allow 8000
6 
7sudo ufw enalbe
8sudo ufw status  #显示状态

布置django
本地

1source Desktop/django_env/bin/activate
2pip freeze
3pip freeze > requirements.txt
4cd desktop
5scp -r django_project coreyms@198.58.119.183:-/

server

1ls #显示django_project
2sudo apt-get install python3-pip
3sudo apt-get install python3-venv
4python3 -m venv django_project/venv  #make viretual env
5ls django_project/
6cd django_project/
7source venv/bin/activate
8pip install -r requirements.txt
9sudo nano django_project/settings.py
10#modify
11Allowed_host = [´198.58.119.183´]
12static_Root = os.path.join(base_dir,´static´)  #add this line
13python manage.py collectstatic
14ls  #显示static文件夹
15python manage.py runserver 0.0.0.0:8000

访问198.58.119.183:8000,应该能访问了。

1cd #into root directory
2sudo apt-get install apache2
3sudo apt-get install libapache2-mod-wsgi-py3
4cd /ect/apache2/sites-availabe/
5ls
6sudo cop 000-default.conf django_project.conf
7sudo nano django_project.conf
8#在 </virtualhost>前面加添加以下内容
9Alias /static /home/coreyms/django_project/static
10<Directory /home/coreyms/django_project/static>
11    Require all granted
12<Directory>
13        
14Alias /media /home/coreyms/django_project/media
15<Directory /home/coreyms/django_project/media>
16    Require all granted
17<Directory>
18 
19<Directory /home/coreyms/django_project/django_project>
20     <Files wsgi.py>
21       Require all granted
22     <File>
23<Directory>
24 
25WSGISciptAlias / /home/coreyms/django_project/django_project/wsgi.py
26WSGIdaemonProcess django_app python-path=/home/coreyms/django_project python-home=/home/coreyms/django_project/venv/ 
27WSGIProcessGroup django_app
28</virtualhost>
29 
30sudo a2ensite django_project
31sudo a2dissite 000-default.conf
32sudo chown :www-data django_project/db.sqlite3
33sudo chmod 664 django_project/db.sqlite3
34sudo chown :www-data django_project/
35sudo chmod 775 django_project/
36sudo chown -r :www-data django_project/media/
37sudo chmod -R 775 django_project/media/
38 
39sudo touch /etc/config.json
40sudo nano django_project/django_project/settings.py
41#copy secret_key,并删除。
42sudo nano /etc/config.json

#建立json文件

1{
2   ´secret_key´:´xxx´,
3   ´Email_user´: ´´,
4   ´Email_pass´: ´´,
5}
1sudo nano /etc/config.json
2import json
3with open(´/etc/config.json´) as config_file:
4  config = json.load(config_file)
5secret_key = config[´secret_key´]
6Debug = False
7 
8Email_host_usr = config.get(´Email_user´)
9Email_host_password = config.get(´Email_pass´)
1sudo ufw delet allow 8000
2sudo ufw allow http/tcp
3sudo service apache2 restart

现在访问,应该可以访问了。
不过还没有包括绑定域名。

goolge: django deploy checklist

暧昧帖

本文暂无标签

发表评论

*

*