DjangoBlog project address:
https://github.com/liangliangyy/DjangoBlog
1.First install the git tool to clone the DjangoBlog project:
RedHat|CentOS:
yum install git -y
Debian|Ubuntu:
apt-get update
apt-get install git -y
Clone the DjangoBlog project:
cd /www/wwwroot/
git clone https://github.com/liangliangyy/DjangoBlog
2.Install Python 3.8.12
3.Add DjangoBlog project
Parameter Description:
- Name:Give your project a name
- Path:Select the root directory of the project,Select the previously cloned directory
- Version:Choose the python version your project needs,Choose Python Version 3.8.12 here
- Framework:The project project framework, my project here is Flask, so choose django
- Startup mode:Choose gunicorn here, You can switch other options according to your needs
- startuo file/dir:diango select the project directory to start
- Port:DjangoBlog defaults to 8000
- Run user:Start with root privileges
- Install module now:When adding a project, install the required modules according to the documentation of the project root directory requirements.txt.
- Start with the sys:Configure startup for the project
When we finish project creation, we can’t open the project normally.We need to initialize the project
After the installation is complete, the display is as follows:
4.Create a database for the DjangoBlog project
When creating, you need to pay attention to selecting the character set: utf8mb4
5.Modify the DjangoBlog configuration:
Go to the DjangoBlog directory, enter the djangoblog directory, and modify the configuration file: settings.py
After commenting the original database, then add the following content, pay attention to modify it to your database name, user name, password, pay attention to the case of the database.
/www/wwwroot/DjangoBlog/djangoblog
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangoblog_test',
'USER': 'DjangoBlog_test',
'PASSWORD': 'P4KajxAhfJ6dMWph',
'HOST': 'localhost',
'PORT': 3306,
'OPTIONS': {'charset': 'utf8mb4'},
}
}
Comment the configuration below, otherwise it will cause database errors later, only keep: TIME_ZONE = 'Asia/Shanghai'
#LANGUAGE_CODE = 'zh-hans' (English will be displayed after adding annotations)
#USE_I18N = True
#USE_L10N = True
#USE_TZ = True
configuration on the project
The Python project env used by the project
Please replace "8658305af42d6efded53c296d677d3ba_venv" with your project env and execute the command.
6.Start performing data migration
Generate data:
/www/wwwroot/DjangoBlog/8658305af42d6efded53c296d677d3ba_venv/bin/python3 manage.py makemigrations
/www/wwwroot/DjangoBlog/8658305af42d6efded53c296d677d3ba_venv/bin/python3 manage.py migrate
Generate super user: (create an administrator user according to the prompt)
/www/wwwroot/DjangoBlog/8658305af42d6efded53c296d677d3ba_venv/bin/python3 manage.py createsuperuser
Generate test data:
/www/wwwroot/DjangoBlog/8658305af42d6efded53c296d677d3ba_venv/bin/python3 manage.py create_testdata
Collect static files:
/www/wwwroot/DjangoBlog/8658305af42d6efded53c296d677d3ba_venv/bin/python3 manage.py collectstatic --noinput
/www/wwwroot/DjangoBlog/8658305af42d6efded53c296d677d3ba_venv/bin/python3 manage.py compress --force
Restart the DjangoBlog project
7.The port 8000 used by DjangoBlog is released in Security
try to access
Open the browser: http://127.0.0.1:8000/
and you can see the effect
Replace the ip address with your server address.
8.Map the DjangoBlog project to a web service (nginx/apache), and nginx/apache is responsible for forwarding the request to our Python project on the backend
9.Static file configuration
Add the following to site config URL rewrite:
location /static/ {
alias /www/wwwroot/DjangoBlog/collectedstatic/;
expires max;
access_log off;
log_not_found off;
}
location /media {
alias /www/wwwroot/DjangoBlog/uploads/;
expires max;
}
location ~ \.py$ {
return 403;
}
10.DjangoBlog project deploys SSL
Access effect:
Login background error after deploying SSL:
https://129djangoblog.com/admin
Solution:
Modify the configuration file: /www/wwwroot/DjangoBlog/djangoblog/settings.py
ALLOWED_HOSTS = ['*', '127.0.0.1', '129djangoblog.com']
# django 4.0新增配置
CSRF_TRUSTED_ORIGINS = ['https://129djangoblog.com']
Replace 129djangoblog.com with your domain name, then go to Python Manager to restart the DjangoBlog project.
References:
https://github.com/liangliangyy/DjangoBlog/blob/master/docs/README-en.md
https://www.bt.cn/bbs/thread-90005-1-1.html