python做网站部署起来真的太麻烦了!
apt-get update && apt-get upgrade hostnamectl set-hostname django-server hostname #查看结果 nano /etc/hosts 编辑host文件,在127.0.0.1下在加入以下这行,ctrl+X 198.59.119.183 django-server adduser coreyms adduser coreyms sudo exit ssh coreyms@198.59.119.183 #重新登陆 pwd #在这/home/coreyms mkdir -p -/.ssh ls -la
切换到本地电脑
ssh-keygen -b 4096 #使用默认路径 scp -/.ssh/id-rsa.pub coreyms@198.58.119.183:-/.ssh/authorized_keys #上传key
服务器上查看key
ls.ssh sudo chmod 700 -/.ssh/ sudo chmod 600 -/.ssh/* exit #测试登陆 sudo nano /etc/ssh/sshd_config #修改以下两项 permitRootlogin no passswordAuthentication no #重启 sudo systemctl restart sshd
sudo apt-get install ufw sudo ufw default allow outgoing sudo ufw default deny incoming sudo ufw allo ssh sudo ufw allow 8000 sudo ufw enalbe sudo ufw status #显示状态
布置django
本地
source Desktop/django_env/bin/activate pip freeze pip freeze > requirements.txt cd desktop scp -r django_project coreyms@198.58.119.183:-/
server
ls #显示django_project sudo apt-get install python3-pip sudo apt-get install python3-venv python3 -m venv django_project/venv #make viretual env ls django_project/ cd django_project/ source venv/bin/activate pip install -r requirements.txt sudo nano django_project/settings.py #modify Allowed_host = [´198.58.119.183´] static_Root = os.path.join(base_dir,´static´) #add this line python manage.py collectstatic ls #显示static文件夹 python manage.py runserver 0.0.0.0:8000
访问198.58.119.183:8000,应该能访问了。
cd #into root directory sudo apt-get install apache2 sudo apt-get install libapache2-mod-wsgi-py3 cd /ect/apache2/sites-availabe/ ls sudo cop 000-default.conf django_project.conf sudo nano django_project.conf #在 </virtualhost>前面加添加以下内容 Alias /static /home/coreyms/django_project/static <Directory /home/coreyms/django_project/static> Require all granted <Directory> Alias /media /home/coreyms/django_project/media <Directory /home/coreyms/django_project/media> Require all granted <Directory> <Directory /home/coreyms/django_project/django_project> <Files wsgi.py> Require all granted <File> <Directory> WSGISciptAlias / /home/coreyms/django_project/django_project/wsgi.py WSGIdaemonProcess django_app python-path=/home/coreyms/django_project python-home=/home/coreyms/django_project/venv/ WSGIProcessGroup django_app </virtualhost> sudo a2ensite django_project sudo a2dissite 000-default.conf sudo chown :www-data django_project/db.sqlite3 sudo chmod 664 django_project/db.sqlite3 sudo chown :www-data django_project/ sudo chmod 775 django_project/ sudo chown -r :www-data django_project/media/ sudo chmod -R 775 django_project/media/ sudo touch /etc/config.json sudo nano django_project/django_project/settings.py #copy secret_key,并删除。 sudo nano /etc/config.json
#建立json文件
{ ´secret_key´:´xxx´, ´Email_user´: ´´, ´Email_pass´: ´´, }
sudo nano /etc/config.json import json with open(´/etc/config.json´) as config_file: config = json.load(config_file) secret_key = config[´secret_key´] Debug = False Email_host_usr = config.get(´Email_user´) Email_host_password = config.get(´Email_pass´)
sudo ufw delet allow 8000 sudo ufw allow http/tcp sudo service apache2 restart
现在访问,应该可以访问了。
不过还没有包括绑定域名。
goolge: django deploy checklist