This article mainly introduces server deployment Django Required configuration and uwsgi as well as nginx Configuration of , No introduction Python Installation and creation of virtual environment , It doesn't involve Mysql Database installation and configuration ,Python And virtual environments and Mysql You can search the Internet by yourself , Generally, there is no pit , It can be installed and configured successfully .
1. Upload the local project to the server
Use Xftp Connect to server , adopt Xftp Upload the local project to the specified location on the server , For example, I will upload to home/username Under the folder .
2. To configure Django project
In the project setting.py Inside , Comment out STATICFILES_DIRS, newly added STATIC_ROOT.
# STATICFILES_DIRS = (
# os.path.join(BASE_DIR,'static'),
# )
STATIC_ROOT = os.path.join(BASE_DIR,'static/'
To configure url.py file , stay urlpatterns New in :
url(r'media/(?P.*)$', serve, {"document_root": MEDIA_ROOT}),
url(r'^static/(?P.*)$', serve, {"document_root": STATIC_ROOT}),
Because it's used serve、MEDIA_ROOT and STATIC_ROOT, Import required :
from django.views.static import serve
from newblog.settings import MEDIA_ROOT, STATIC_ROOT
3. Collecting static files
Go to the project root , function
python manage.py collectstatic
pip install uwsgi
5. install nginx
pip install uwsgiyum install nginx
newly build nginx.cong file , Enter the following :
user root;
events{}
http{
include /etc/nginx/mime.types;
server{
listen 80;
server_name Your domain name ;
index index.html ;
root Your project directory ;
location /static {
alias Your project directory /static; # your Django project's static files - amend as required
}
location /media {
alias Your project directory /media;
}
# Finally, send all non-media requests to the Django server.
location / {
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass 127.0.0.1:8000;
}
}
}
Use the new nginx.conf File replacement /etc/nginx/nginx.conf file , It is recommended to back up the original before replacing nginx.conf file
newly build uwsgi.ini file , Enter the following :
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = Your project directory
# Django's wsgi file
module = Project name .wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
virtualenv = Virtual environment path
uwsgi uwsgi.ini -d conf/uwsgi.log(-d Back for you to store log The path of )
sudo /usr/sbin/nginx
Enter your domain name in the browser , You can visit your website normally