Introduction
The source code can be run directly, supports markdown syntax, supports email registration, and any issues can be referenced in 《Flask Web Development: Web Application Development in Action with Python》.
Blog website: http://www.unrealblue.cc Project repository: https://github.com/linanwx/unrealblue-blog
Preview
Deployment Process
First, to verify the program functionality, you need to deploy it on your local machine. After that, you can use nginx as a reverse proxy tool to expose the port, so that others can access your blog on the public network. Then follow the same steps on the server. The specific process is as follows:
- Install the virtualenv Python virtual environment with
pip install virtualenv
orpip3 install virtualenv
. Then use virtualenv to create a venv environment in a suitable directory, for example under this project withvirtualenv venv
. - Activate the virtualenv environment by running the activate script in the venv directory:
. venv/bin/activate
. Note the position of the dot. After this, you will see the (venv) marker at the beginning of the command line. - Install all modules listed in requirements.txt within the virtual environment:
pip3 install -r requirements.txt
. If installation is too slow, you may need to configure a domestic pip source. See the pip official page for how to change the pip source. - Import environment variables by creating an
env
file in the project directory with the following fields:
MAIL_USERNAME=email@example.com
(Email used by the server to send verification codes, an email account with smtp service enabled. The program uses QQ email by default, modify the config file to use other types of email)MAIL_PASSWORD=password
(Password for the above email, note that QQ email uses a special 16-digit password)FLASK_ADMIN=admin@example.com
(After the server is running, an account created with this email will be the administrator)MAIL_SERVER=smtp.qq.com
(Email server address)FLASKY_MAIL_SENDER=example@foxmail.com
(Sender’s email)
- Set up database migration by entering the following commands:
python manager.py db init
(The init command creates a migration repository, which will add a migrations folder)python manager.py db migrate -m "initial migration"
(The migrate command is used to automatically create migration scripts)python manager.py db upgrade
(Update the database. The first time you use this command, it will create a new database called data-dev.sqlite)
- Deploy the program:
python manager.py deploy
- Run the program locally:
python manager.py runserver
. Open http://127.0.0.1:5000 to view it, press Ctrl+C to exit the program. - If running on a server and you want to preserve data, you can copy the migrations folder and the data-dev.sqlite database to the server, then use
./venv/bin/gunicorn -w 4 -b 127.0.0.1:8080 manager:app
. At this point, you should be able to see the webpage on port 8080, and this port is exposed to the external network. Enter the server address in your local browser, and you will be able to see the blog.