Preface : I believe many people have encountered such a problem, the Django project will be completed, ready to deploy to Linux, encountered a lot of trouble, online tutorials most of the use of Python2 Django Project, once used for Python3, There will be a lot of bugs. This tutorial details the detailed process of the Python3 Django 1.11 Project Online
Create a virtual environment
# cd/home/#
mkdir djangoproject #
CD./djangoproject/
# python-m venv djangoprojectenv
where python-m venv xxx is python3 a way to create a virtual environment for XXX, where I have created a djangoprojectenv virtual environment
into the virtual environment
# source Djangoprojectenv/bin/activate
You can see that you have entered the virtual environment and have a (djangoprojectenv) identity before the command line, for example:
# (DJANGOPROJECTENV) [Root@iz2zeb45dolegxb5nfuexez djangoproject]#
Installation Tools
When using PIP, we recommend that you use the watercress source, the installation speed will be fast, for example:
# pip Install-i https://pypi.douban.com/simple Django
Because I am using the Aliyun server, use PIP to install the application, will automatically accelerate, so the default way, if we feel that their download speed is slow, you can use watercress source
# pip install Django
# yum install mysql-devel
# pip install mysqlclient
# pip Install Gunicorn
To create a Django project
# django-admin.py Startproject djangoproject.
# ls
You can see the directory structure
# djangoproject djangoprojectenv manage.py
Note: Create Djangoproject. I'm sure you don't want to get rid of it .
Modify settings for an item
# Vim djangoproject/settings.py
to modify database parameters in settings.py
DATABASES = {'
default ': {
' ENGINE ': ' Django.db.backends.mysql ', '
NAME ': ' Djangoproject ',
' USER ': ' Root ',
' PASSWORD ': ' PASSWORD ', '
HOST ': ' 127.0.0.1 ',
' PORT ': ' 3306 ',
' OPTIONS ': {
' Init_ Command ': ' SET sql_mode= ' Strict_trans_tables '}
}
Note Modify database name, account number and password
add static file path parameters to settings.py
Static_root = Os.path.join (Base_dir, "static/")
Modify IP Permissions in settings.py to allow all IP access
Allowed_hosts = ["*"]
Then save the file and press Esc:wq
to generate a datasheet in a database
#./manage.py makemigrations
#/manage.py Migrate
Create a Super User
#./manage.py Createsuperuser
Collect static files
#./manage.py collectstatic
This step is important, otherwise the page will be garbled
Open the Django project
#./manage.py Runserver 0.0.0.0:8000
You can access the server in your browser, enter the IP and port, the format is as follows:
http://server_domain_or_IP:8000
You can also access your database.
Http://server_domain_or_IP:8000/admin
Log into your own set of Superuser to view the database
test whether Gunicorn can start your project Services
# Gunicorn--bind 0.0.0.0:8000 djangoproject.wsgi:application
Visit the IP address to see if the browser can view the content properly (no exit virtual environment at this time)
After completing the test, press Ctrl-c to stop Gunicorn running
Exit Virtual Environment
# Deactivate
Create a Gunicorn systemd Service file
# Vim/etc/systemd/system/gunicorn.service
The amendment reads as follows:
[Unit]
Description=gunicorn daemon
after=network.target
[Service]
user=root
Group=nginx
Workingdirectory=/home/djangoproject
Execstart=/home/djangoproject/djangoprojectenv/bin/gunicorn--workers 3 --bind unix:/home/djangoproject/djangoproject.sock djangoproject.wsgi:application
[Install]
wantedby= Multi-user.target
Be sure to master your own engineering path and virtual environment
WorkingDirectory and Execstart modified for their own path
turn on the Gunicorn service and start the startup
# Systemctl start Gunicorn
# Systemctl Enable Gunicorn
Configure Nginx agent through Gunicorn
# vim/etc/nginx/nginx.conf
Add content as follows:
server {
listen ;
server_name server_domain_or_ip;
Location =/favicon.ico {access_log off; log_not_found off;}
location/static/{
root/home/djangoproject;
}
Location/{
proxy_set_header Host $http _host;
Proxy_set_header x-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_set_header X-forwarded-proto $scheme;
Proxy_pass http://unix:/home/djangoproject/djangoproject.sock
}
}
Note Modify your own IP address or domain name, and file path
SERVER_DOMAIN_OR_IP represents your IP address or domain name.
Modify permissions for Nginx
# usermod-a-G root nginx
# chmod 710/home/
# nginx-t
If there is no error, proceed to the next step.
turn on the Nginx service and start the startup
# Systemctl start Nginx
# Systemctl Enable Nginx
Now, you can access your IP.
Note: This article is logged by the root user