DjangoBlog deployment Tutorial

This article will taught you how to deploy DjangoBlog project, the basic environment here at my first introduction, please read this section, the following tutorial will use these conventions to introduce:

  • System isubuntu 18.04 LTS
  • Suppose you domain name is www.djangoblog.com.
  • python virtual environment directory ~/python/env
  • djangoblog source location ~/python/DjangoBlog
  • Login Userserver
  • Editor vim, you can use your favorite editor.

 

Installation ftp, upload the code up, the path is ~ / python / DjangoBlog, when attention is connected Ali cloud, using ssh connection port 22,

Ready to work

Upgrading the system

First, we need to upgrade the system to the latest version at the terminal performs:

sudo apt update
sudo apt upgrade -y

After the update to be completed, it is recommended to restart the system, and then subsequent operations. Terminal under execution:

sudo reboot

After the system starts, you can begin the installation dependencies and python mysql the environment.

Installation depends

Terminal under execution:

APT-Server MySQL the install the sudo -Y # installed MySQL 
the sudo APT to python3 the install to python3-dev-pip-pip the memcached Python -Y # mounted pip and the memcached 
the sudo Supervisor -Y APT the install 
the sudo Nginx -Y APT the install 
the sudo the install APT-GET Python - libmysqlclient-default-dev dev # installation mysqlclient depend 


first step -y install mysqlclient APT sudo
2. Python installation mysqlclient need in order to install the following libraries based on:
sudo install libmysqlclient APT-GET-dev
sudo install APT for libssl-dev
sudo install APT ++ libcrypto - dev
sudo PIP3 install the mysqlclient        
Successfully Installed the mysqlclient-1.4.6

Next you need to set up a virtual environment under the python terminal performs:

cd ~
mkdir -p  python/env && cd python/env
virtualenv -p /usr/bin/python3 djangoblog
source djangoblog/bin/activate

After completion of the implementation of the above command, your terminal should look similar to the following way:   More about python virtual environment description please refer to Python virtual environment settings  in this article.

 

 

 

Here begin the installation operation relies djangoblog required, the next terminal:

source ~/python/env/djangoblog/bin/activate
cd ~/python/DjangoBlog
pip install -Ur requirements.txt

The basic configuration and deployment

Configure mysql database

Terminal under execution:

sudo mysql_secure_installation

Similar to the following, where I need to enter with a red arrow marked out, need to pay attention, root password must bear in mind that will be used later.

 Logged in as root:

mysql -uroot -p

If you can not log in, you can use the following steps, please note that the password I have here is just an example, please revise your own password :

sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Q1W2E3R4T5#$%qwe';
FLUSH PRIVILEGES;
exit 

如果提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements,则说明你的密码强度不够,需要提供更复杂的密码,如下所示:  修改mysql默认字符集,使用你喜欢的编辑器,如vim或者nano,打开/etc/mysql/conf.d/mysql.cnf,配置文件,注意,需要使用root权限,如vim:

sudo vim /etc/mysql/conf.d/mysql.cnf

删除该文件的[mysql]行并贴入如下内容:

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

退出并保存,重启mysql服务,终端下执行:

sudo /etc/init.d/mysql restart

接下来就可以登录mysql数据库并创建用户和数据了,终端下执行:

mysql -uroot -p #进入mysql终端
CREATE USER 'djangoblog'@'localhost' IDENTIFIED BY 'DjAnGoBlOg123!@#';
CREATE DATABASE `djangoblog` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */; GRANT all ON djangoblog.* TO 'djangoblog'@'localhost'; FLUSH PRIVILEGES; exit #退出 

同样,这里的DjAnGoBlOg123!@#密码也要改成你自己的密码。

修改djangoblog配置

修改DjangoBlog/settings.py中的DATABASES配置,如下所示,当然,你也可以将其中的配置写入你的.bashrc中,这样就不需要改这个文件了,我这里只是介绍下如何修改:

DATABASES = {
    'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'djangoblog', 'USER': 'djangoblog', 'PASSWORD': 'DjAnGoBlOg123!@#', 'HOST': 'localhost', 'PORT': 3306, 'OPTIONS': {'charset': 'utf8mb4'}, } } 

接下来开始执行数据库迁移,终端下执行:

./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser #创建超级用户
./manage.py collectstatic --no-input
./manage.py compress --force

下面可以尝试启动网站,终端下执行:

./manage.py runserver

看到如下所示,则说明启动成功了:  接下来你就可以使用浏览器打开http://127.0.0.1:8000/ 来查看了。你需要访问http://127.0.0.1:8000/admin/ 在站点中,将默认的example.com修改成你自己的,如www.djangoblog.com

gunicorn 配置

安装,终端下执行:

source ~/python/env/djangoblog/bin/activate
pip install gunicorn

配置,终端下执行:

vim ~/python/gunicorn_start.sh

贴入如下内容:

#!/bin/bash

NAME="DjangoBlog"
DJANGODIR=/home/server/python/DjangoBlog #Django project directory USER=server # the user to run as GROUP=server # the group to run as NUM_WORKERS=1 # how many worker processes should Gunicorn spawn DJANGO_SETTINGS_MODULE=DjangoBlog.settings # which settings file should Django use DJANGO_WSGI_MODULE=DjangoBlog.wsgi # WSGI module name echo "Starting $NAME as `whoami`" # Activate the virtual environment cd $DJANGODIR source /home/server/python/env/djangoblog/bin/activate export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE export PYTHONPATH=$DJANGODIR:$PYTHONPATH # Create the run directory if it doesn't exist RUNDIR=$(dirname $SOCKFILE) test -d $RUNDIR || mkdir -p $RUNDIR # Start your Django Unicorn # Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon) exec /home/server/python/env/djangoblog/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \ --name $NAME \ --workers $NUM_WORKERS \ --user=$USER --group=$GROUP \ --log-level=debug \ --log-file=- 

注意,将该文件中的server修改成你自己的用户,如果是root用户的话,也要将/home/server修改为/root。同时,将DjangoBlog/settings.py中的DEBUG = True修改为DEBUG = False。 增加可执行权限:

chmod +x gunicorn_start.sh

执行:

./gunicorn_start.sh

如果看到类似下面的输出,则说明成功了。  然后使用Ctrl+c来终止。

nginx配置

终端下执行:

# 删除默认配置
sudo rm /etc/nginx/sites-enabled/default
sudo vim /etc/nginx/sites-enabled/djangoblog.com.conf

贴入如下内容:

server {

    listen 80;
    server_name www.djangoblog.com; root /home/server/python/DjangoBlog/; access_log /var/log/nginx/django_access.log; error_log /var/log/nginx/django_error.log; location /static/ { alias /home/server/python//DjangoBlog/collectedstatic/; expires max; access_log off; log_not_found off; } location /media { # 静态文件配置 alias /home/server/python/DjangoBlog/uploads/; expires max; } location ~ \.py$ { return 403; } location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://127.0.0.1:8000; break; } } } 

保存并退出。重启nginx:

sudo /etc/init.d/nginx restart

配置Supervisor

终端下执行:

sudo vim /etc/supervisor/conf.d/djangoblog.conf

贴入如下内容:

[program:djangoblog]
command = /home/server/python//gunicorn_start.sh
user = server autostart=true autorestart=true redirect_stderr = true stdout_logfile = /var/log/djangoblog.log stderr_logfile=/var/log/djangoblog.err 

同样,需要将server修改成你自己等用户名. 保存成功后继续执行:

sudo supervisorctl update
sudo supervisorctl reload 
sudo /etc/init.d/memcached restart && sudo /etc/init.d/nginx restart

至此,全部完成。你可以通过你的浏览器访问http://www.djangoblog.com了。 更多配置相关可以参考: https://github.com/liangliangyy/DjangoBlog/blob/master/docs/config.md。这里就不再赘述了。

更新代码

因为我会一直维护这个项目,为其添加新功能或者修复bug,所以后续如果我更新项目之后建议你也同步的更下下你的源码。更新方式建议你直接fork我的仓库,然后在我修改过之后,从我的仓库拉取修改合并入你的仓库,然后使用ftp或者直接使用git,来在你的线上环境更新源码。
下面列出两篇文章,介绍如何更新源码来供大家参考:

  1. https://jinlong.github.io/2015/10/12/syncing-a-fork/
  2. https://github.com/staticblog/wiki/wiki/保持fork之后的项目和上游同步

致大家

如果大家成功的使用本系统部署了自己的网站,请将自己的域名添加在 https://github.com/liangliangyy/DjangoBlog/issues/214 中,让更多的人可以看到,谢谢大家。

本条目发布于 2019-08-05 。属于Python 分类, 被贴了  ,  标签。 作者是 

Guess you like

Origin www.cnblogs.com/andy0816/p/12334227.html