Personal study notes, reference articles are as follows:
https://blog.csdn.net/tr_136163798/article/details/120901363
https://www.dusaiphoto.com/article/71/
https://docs.djangoproject.com/zh -hans/3.2/howto/static-files/
https://docs.djangoproject.com/zh-hans/3.2/howto/static-files/deployment/
This article was published on my personal blog for the first time, welcome to visit:
https://sunguoqi.com/2021/12/15/Django_04/
Following the guidelines of Django's official documentation, we have made a voting application. We develop locally, and the project will be deployed to the server when it goes online. This note records the entire process of my personal deployment of the Django project, for reference only.
Article directory
1. New site
1. Create a site using the pagoda panel
站点创建成功!
2. Configure SSL certificate (optional)
配置成功
Second, the packaging program
1. Export dependencies
Open the project locally and execute the following command to export the project dependencies.
pip freeze > requirements.txt
2. Package upload
Close pycharm, make the project mysite into a compressed package, and upload it to the website directory using the pagoda panel/www/wwwroot/django.sunguoqi.com
3. Decompression
The pagoda panel can directly decompress the file without having to operate on the command line.
3. Environment installation
1. Install the virtual environment
Use the Xshell remote host, enter the website directory, and execute the following command to install the virtual environment.
pip3 install virtualenv
2. Create a virtual environment
Enter the project directory
and execute the following command to create a virtual environment. venv is the name of the virtual environment, which can be customized
virtualenv venv
3. Activate the virtual environment
cd venv
source bin/activate
4. Install dependency packages
cd ..
pip3 install -r requirements.txt
报错
ERROR: Failed building wheel for mysqlclient
解决方案
Execute the following commands respectively
sudo apt-get install python3 python-dev python3-dev
sudo apt-get install build-essential libssl-dev libffi-dev
sudo apt-get install libxml2-dev libxslt1-dev zlib1g-dev
Reinstall the database engine
pip3 install mysqlclient
成功
Execute the package install command
pip3 install -r requirements.txt
Because the program terminated when installing the database engine, the following packages have not been installed.
成功
Fourth, configure the project database
1. Modify the project database configuration information
2. Migrate the database
python3 manage.py migrate
成功
5. Start the project
1. Modify the project settings
mysite/setting.py
ALLOWED_HOSTS = ['*']
2. Create a background management super user
Because the database data during development has not been imported, we now have no background account data. Execute the following command to create a super user.
python manage.py createsuperuser
3. Start the project
Open port 8000 of your host and execute the following command.
python3 manage.py runserver 0.0.0.0:8000
The project can be started normally!
Six, Screen process hosting
When the SSH session is closed, the web service is also closed, causing the website to fail to connect. We need to make the process work 24 hours a day, here I use screen to achieve.
1. Install screen
apt-get install screen
2. Create a screen
screen -S django_study
3. Start the project
python3 manage.py runserver 0.0.0.0:8000
Seven, Nginx reverse proxy
At present, we can only use IP:Port
the method to access the web. When the final project is launched, it is usually accessed by the domain name. The domain name correspondence of our project is to django.sunguoqi.com
use the reverse proxy of Nginx to establish a relationship between the domain name and the port.
在宝塔面板上配置
Eight, the deployment is complete
Access django.sunguoqi.com/polls/
, there are no available votes, because we did not add data in the background.
1. Log in to the background
visit django.sunguoqi.com/admin/
,
2. Add vote
3. to vote
Visit django.sunguoqi.com/polls/
to vote.
4. Submit data
九、DEBUG=False?
In essence, our current project is still using the web server that comes with Django, and we just use Nginx as a reverse proxy.
When we formally deploy the project, we need to turn off debugging. When we start the project without making any changes, the following error will appear.
In the project development stage, when DEBUG=True, Django will use its own web service to process static files, but its performance is not very good. So in the project deployment phase, when DEBUG=False, Django will no longer manage static files. Django wants us to hand over static files to Nginx (or other web servers) for processing.
At this time, we need to deal with static files.
1. Configure the static file path
First, we change the relevant configuration in setting.py to this.
import os
# 关闭调试模式
DEBUG = False
# 允许的服务器
ALLOWED_HOSTS = ['*']
# 静态文件收集目录
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
2. Collect static files
Execute the following command to collect the static files in the project. The location after collection is the static file collection directory we set above.
python3 manage.py collectstatic
3. Configure Nginx
Add the following to your site configuration file.
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|woff|woff2)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
4. Start the project
ok!