# pbp **Repository Path**: zjcnew/pbp ## Basic Information - **Project Name**: pbp - **Description**: Django项目开发,个人博客网站类。 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-06-11 - **Last Updated**: 2021-01-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # pbp #### 项目介绍 首个Django项目开发,个人博客网站类。 #### 框架模型 Django #### 运行平台 Linux #### 安装部署 ##### 1.安装Python3环境 ###### CentOS 7 ```angular2 curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install python36 python36-pip python36-devel git gcc -y ``` ###### Ubuntu 16.04 ``` sudo add-apt-repository ppa:deadsnakes/ppa sudo apt-get update sudo apt-get -y install python3.6 python3.6-dev git gcc ``` ##### 2.安装MySQL、Redis、Nginx ###### CentOS 7 ```angular2 yum install mariadb-server redis nginx -y systemctl restart redis mariadb systemctl enable redis mariadb ``` ###### Ubuntu 16.04 ```angular2 sudo apt-get -y install mysql-server redis-server nginx sudo systemctl restart mysql redis-server sudo systemctl enable mysql redis-server ``` ##### 3.安装docker ###### CentOS 7 ``` yum install docker -y mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://cygnf2eh.mirror.aliyuncs.com"] } EOF systemctl start docker systemctl enable docker ``` ###### Ubuntu 16.04 ```angular2 sudo apt-get install docker-ce mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://cygnf2eh.mirror.aliyuncs.com"] } EOF sudo systemctl start docker sudo systemctl enable docker sudo usermod -a -G docker $USER ``` ##### 4.创建数据库实例 登录MySQL ```angular2 mysql -u root -p ``` 使用SQL语句创建数据库实例并授权访问 ```angular2 create database pbp character set utf8; grant all privileges on pbp.* to 'pbp'@'localhost' identified by 'N4d)D@912'; flush privileges; exit ``` ##### 5.创建Python虚拟环境(推荐) 如果需要在同一台服务器上部署多个Python项目,且没有基于Docker容器隔离的环境,建议将项目部署到Python虚拟环境。 ```angular2 cat > /etc/pip.conf << EOF [global] index-url = http://mirrors.aliyun.com/pypi/simple/ trusted-host = mirrors.aliyun.com disable-pip-version-check = true timeout = 120 EOF pip3.6 install virtualenvwrapper mkdir /virtualenvs sed -i "s/which python/which python3.6/" /usr/local/bin/virtualenvwrapper.sh echo "export WORKON_HOME=/virtualenvs" >> /etc/profile echo "source /usr/local/bin/virtualenvwrapper.sh" >> /etc/bashrc source /etc/profile source /etc/bashrc mkvirtualenv -p /usr/bin/python3.6 pbp ``` ##### 6.克隆项目 ``` git clone https://gitee.com/zjcnew/pbp.git /virtualenvs/pbp/project cd /virtualenvs/pbp/project pip3 install -r requirements.txt pip3 install fdfs_client-py-master.zip mkdir {logs,run,media} ``` ##### 7.修改配置 ###### (1).配置数据库服务器地址,授权访问的帐号密码。 ```angular2 vi utils/dbs/my.cnf ``` ```angular2 host = localhost socket = /var/lib/mysql/mysql.sock port = 3306 database = pbp user = pbp password = N4d)D@912 default-character-set = utf8 ``` ###### (2).配置云通讯短信验证码发送平台授权帐号id与token ```angular2 vi utils/send_sms.py ``` ```angular2 self.sid = 'kzuh4675h8i7o3arn99a9zlmitrz118u' self.token = 'tnapwwlpyqsibu29hvb50irz8aq0nh3l' ``` ###### (4).配置阿里云oss对象存储参数,修改utils/oss2_aliyun.py 登录阿里云oss控制台获取参数BucketName与Endpoint,accesskeys获得ak与sk。 ```angular2 AccessKeyId = 'abcdsdfsadfsadfsf' AccessKeySecret = 'dsadsaere2rwqrewerwqrewq' Endpoint = 'https://oss-cn-shenzhen.aliyuncs.com' BucketName = 'pbp-project-files' ``` ###### (5).百度云音视频点播VOD参数 登录百度云-安全认证获取ak与sk 修改static/js/course/course_detail.js ```angular2 ak: "412a628a1025456d83fdae14a459f076" ``` 修改static/js/admin/course/courses_pub.js ```angular2 ak: '412a628a1025456d83fdae14a459f076', sk: 'd263dad9a5d740f097a6e20c456111ed' ``` ###### (6).配置用于密码加密的RSA密钥对 推荐使用此平台来生成密钥 http://web.chacuo.net/netrsakeypair #### 私钥 ```angular2 vi utils/decrypt.py ``` ```angular2 PRIVATE_KEY = '''-----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQCr5TeyP7x1XAVj8mkbnKR4Pjt4gyeOvEapnjs7djhBNo4a2EWu LVxTW0NQiDvzCXRFJWDMtxvogVBhfQB+FDgdvJPtqF/xsA4V7wN5PaJATWNkoT3d SEhaV5CN5gd9T4ZZhLi1l+3KPeN/uZfL1CzmTOrVMmQp2QbGpTVw4ZORswIDAQAB AoGAB4fAG9/s6ptAZTN+ZUZEcjwPsOTV+8vFsLKM17sDFeBE82ocC1hTXhd2aHW/ YpM8c7cCeZVf9JyNn+ew+NT77EcrMxCtG6ZvJ03OOZZrdaq4RJBIiWJFdmp33eIf 0EalQm3gcc+0qwLx/EGfJR3+aqZSQv0MS4SjHpGDbnViaUECQQDIHpoPnQN8t6D4 qNCKh7jwPcyu+D+ghTk14h9JRTps/B8eQ9mMLHA1KMaNUZzj7E0dw3J5kWpScLSd UM1pN61hAkEA2+UIWbRd9+FnO/JjF2mzqgPc6bPyHTWl/1Og0IDGz9svgndiUZ6w 1B7t/sNPBsOeBcm7aoJEv4Hmde0F1p7jkwJBAJW6H7aS2asUkQG6/NzJE1vcQSqg XqqxQs7H8nSVsVMdiALS7Q/tp75+VaAiceouQLyGIT5HyG8KGHijniydFcECQBP8 xH6K44O1jFGpnZIyPG92vGzNu3MmPjlwG4z4N5oLHU1plpk+qGoPiah/jj5aJ5H1 HUD4KFGT+CO98KrqdFsCQQClvXow8Dg7ENintkzx+I5u27VmE7DnsIuhHFw8/1+o O8izIH8KvLwwvKmMrOdWn/f1RtjqyapXNJOo9vTAuNXP -----END RSA PRIVATE KEY----- ''' ``` #### 公钥 ```angular2 vi static/js/users/auth.js vi static/js/users/login.js ``` ```angular2 RSA.setPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCr5TeyP7x1XAVj8mkbnKR4Pjt4gyeOvEapnjs7djhBNo4a2E WuLVxTW0NQiDvzCXRFJWDMtxvogVBhfQB+FDgdvJPtqF/xsA4V7wN5PaJATWNkoT3dSEhaV5CN5gd9T4ZZhLi1l+3KPeN/uZfL1CzmTOrVMmQp2QbGpTVw4Z ORswIDAQAB"); ``` ###### (7).配置Django,修改配置文件pbp/settings.py ```angular2 CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/10", # 配置redis服务器参数 "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", # "PASSWORD": "mysecret", "SOCKET_CONNECT_TIMEOUT": 5, # in seconds "SOCKET_TIMEOUT": 5, # in seconds "CONNECTION_POOL_KWARGS": {"max_connections": 50}, } } } # 配置fdfs服务器地址与端口号 FDFS_URL = 'http://192.168.0.20:8888/' FDFS_SERVER_URL = 'http://192.168.0.20:8888/' ``` ###### (8).配置Nginx修改配置文件/etc/nginx/nginx.conf ```angular2 server { listen 80; server_name localhost; charset utf-8; client_max_body_size 75M; location / { uwsgi_pass unix:/dev/shm/uwsgi.sock; #uwsgi_pass 127.0.0.1:8000; include uwsgi_params; } location /static { alias /virtualenvs/pbp/project/static; expires 6h; } location /media { alias /virtualenvs/pbp/project/media; expires 6h; } } ``` ##### 8.迁移数据 ```angular2 python3.6 manage.py makemigrations python3.6 manage.py migrate ``` ##### 9.运行elasticsearch容器 ```angular2 docker image pull elasticsearch:2.4.6 docker run -d -e ES_JAVA_OPTS="-Xms256m -Xmx256m" --net=host --restart=always --name=es docker.io/elasticsearch:2.4.6 docker exec -it es /bin/bash mkdir plugins/ik cd plugins/ik wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v1.10.6/elasticsearch-analysis-ik-1.10.6.zip unzip elasticsearch-analysis-ik-1.10.6.zip chown -R elasticsearch.elasticsearch ../ik/ exit docker restart es ``` Elasticsearch重建索引 ```angular2 python3.6 manage.py rebuild_index ``` ##### 10.运行fastdfs容器 ```angular2 docker image pull delron/fastdfs docker run -d --net=host --restart=always --name=tracker docker.io/delron/fastdfs tracker docker run -d --net=host -e TRACKER_SERVER=192.168.0.20:22122 --restart=always --name=storage docker.io/delron/fastdfs storage ``` ##### 11.配置uwsgi权限 ``` useradd -s /sbin/nologin -M uwsgi chown -R uwsgi.uwsgi /virtualenvs/pbp/project/ mysql -uroot -p pbp < pbp.sql ``` ##### 12.创建uwsgi服务启动脚本 ```angular2 vi /usr/lib/systemd/system/uwsgi.service ``` ```angular2 [Unit] Description=uWSGI - A Web Server for Django After=network.target remote-fs.target nss-lookup.target [Service] Type=forking User=uwsgi Group=uwsgi LimitNOFILE=65536 LimitNPROC=65536 PIDFile=/virtualenvs/pbp/project/run/uwsgi.pid ExecStart=/virtualenvs/pbp/bin/uwsgi --ini /virtualenvs/pbp/project/pbp/uwsgi.ini ExecStop=/virtualenvs/pbp/bin/uwsgi --stop /virtualenvs/pbp/project/run/uwsgi.pid [Install] WantedBy=multi-user.target ``` ##### 13.启动uwsgi与Nginx ```angular2 systemctl start uwsgi nginx systemctl enable uwsgi nginx ``` ##### 14.浏览器访问