Server Preparation for django deployment
Guide is made for Ubuntu 18.04
Workflow
Check Server Enviroment
Check Python version
python --version; pip --versionCheck cpuinfo
cat /proc/cpuinfoCheck Ubuntu version
lsb_release -aInstall pip
apt-get install python3-pip, make symbol link to pip3 under /usr/bin withln -s pip3 pipor to update pip withpip install -U pipinstall virtualenv, sudo pip install virtualenv
update package: pip install Package —upgrade
Create a dir to store your virtualenvs (I use ~/.virtualenvs)
'mkdir ~/.virtualenvs'
At this point you are all set to use virtualenv with the standard commands. However, I prefer to use the extra commands included in virtualenvwrapper. Lets set that up.
Install virtualenvwrapper
'sudo pip install virtualenvwrapper'
Set WORKON_HOME to your virtualenv dir
'export WORKON_HOME=~/.virtualenvs'
Add virtualenvwrapper.sh to .bashrc
Add this line to the end of ~/.bashrc so that the virtualenvwrapper commands are loaded.
'echo ". /usr/local/bin/virtualenvwrapper.sh" >> .bashrc'
Exit and re-open your shell, or reload .bashrc with the command . .bashrc and you’re ready to go.
Create a new virtualenv
'mkvirtualenv myawesomeproject'
to exit your new virtualenv, use deactivate.
Switch between enviornments with workon
To load or switch between virtualenvs, use the workon command:
'workon myawesomeproject'
6 install nginx
`apt-get install nginx`
7 install postgresql
`apt-get install postgresql`
8 add postgre to service
`$ sudo systemctl disable postgresql` or
`$ sudo systemctl enable postgresql`
9 create database and assign user right
sudo -u postgres psql
create database mysql; # ; is important
create user myuser with encrypted password 'password'
grant all privileges on database mysql to myuser
\password myuser # change password for myuser
\l list all databases
\connect dbname # connect to dbname database
deploy django application
copy the folder structure to server. We can use WinSCP to transfer the files via sFTP.
clean migration files and python cache
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
make migrations
python manage.py makemigrations
python manage.py migrate
migrate data if necessary
python manage.py dumpdata > data.json # dumpdata app_name to migrate just one app data
python manage.py shell # delete contentType
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
delete sample.com domain if django site is installed
python manage.py loaddata data.json
python manage.py createsuperuser # in case not in old test system
configure uwsgi
Use uwsgi emperor module
_In case of running one uwsgi instance on the server, use the command uwsgi --ini inifile to start the server and uwsgi --reload pidfile to reload the server. kill -INT pid or kill -INTcat pidfile`` to stop the server
make ini configure file
[uwsgi]
#使用nginx连接时使用
#socketfile
my_app = my_knowledgebase_uwsgi
socket = /home/linkaiyi/my_knowledgebase/uwsgi/%(my_app).sock
chmod-socket = 666
#socket=127.0.0.1:5050
#直接做web服务器使用
#http=127.0.0.1:5000
#http=81.169.229.121:5000
#项目目录
chdir=/home/linkaiyi/my_knowledgebase/
#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=ddk_knowledgebase/wsgi.py
processes=6
threads=2
master=True
pidfile=/home/linkaiyi/my_knowledgebase/uwsgi/%(my_app).pid
daemonize=/home/linkaiyi/my_knowledgebase/uwsgi/%(my_app).log
virtualenv=/home/linkaiyi/.virtualenvs/my_knowledgebase
disable-logging = True
harakiri = 20
max-requests = 5000# set https on
env=HTTPS=on# clear environment on exit
vacuum = true
use %n refer to current ini filename
2 Setup Emperor enviroment
# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid username --gid groupname --daemonize /var/log/uwsgi_emperor.log # without daemonize option, uwsgi will run in foreground
# restart the vassal
touch --no-dereference $INI_FILE
install Certbot and certificate the domains
install the Certbot
sudoapt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python-certbot-nginx
# first domain name will be used as foldername
# expand the certificate with --expand option
# set certificate name with --cert-name name
sudo certbot --nginx -d www.mydomain.com,mydomain.com,someother.domain.com
or
sudo certbot --nginx certonly --expand --cert-name mycert -d www.mydomain.com,otherdomain.com