一、项目环境部署 1 开发环境部署 1.1 软件版本 ? Python3.7.19 ? Django2.2 ? Pycharm2021.3 ? Mysql8.0 ? Windows 10 1.2 硬件版本 ? 台式主机 2 生产环境部署 2.1 软件版本 ? Python3.7.12 ? Django2.2 ? Mysql8.0 ? Uwsgi ? Nginx ? Centos7 2.2 硬件版本 ? Window Server 2012 Hyper-V虚拟机 二、项目环境搭建 1 开发环境搭建 1.1 安装Pycharm2021.3 1.2 安装Python3.7.9 1.3 安装Django2.2 1.4 安装Mysql8.0 略(与生产环境共用一套数据库) 2 生产环境搭建
2.1 安装Centos7服务器 在Windows Server 2012系统下安装Hyper-V服务,并创建虚拟机。
2.2 安装Python3.7.12
systemctl stop firewalld.service
setenforce 0
getenforce
yum –y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel python-devel libffi-devel
yum –y install gcc
yum –y install wget
wget https://www.python.org/ftp/python/3.7.12/Python-3.7.12.tgz
mkdir –p /usr/local/python3
tar zxvf Python-3.7.12.tgz
cd Python-3.7.12
./configure –prefix=/usr/local/python3
make
make install
ln –s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln –s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
pip3 install –upgrade pip
2.3 安装Django2.2
pip3 install django==2.2
ln –s /usr/local/python3/bin/django-admin /usr/local/bin/django-admin
django-admin startproject toms
django-admin startapp app01
python3 manage.py runserver 172.16.253.5:8088
2.4 安装Uwsgi
pip3 install uwsgi
ln –s /usr/local/python3/bin/uwsgi /usr/local/bin/uwsgi
vi uwsgi.ini
[uwsgi]
;建立socket链接,与nginx对接
socket = 172.16.253.5:8088
;配置http对外ip和端口,用户端访问
http = 172.16.253.5:8088
;配置django项目的根目录
chdir = /home/mysite/toms
;配置django项目的wsgi指向
module = toms.wsgi
;配置主进程
master = true
;配置进程数量
processes = 5
;配置线程数量
threads = 2
max-requests = 2000
vacuum = true
;配置uwsgi日志输出,必须是绝对路径,文件自动创建
daemonize = /home/mysite/toms/uwsgi.log
;配置django启动ip和端口
stats = 172.16.253.5:9000
post-buffering = 65535
buffer-size = 65535
ara-kiri-verbose = true
ara-kiri = 300
pidfile = /home/mysite/toms/uwsgi.pid
uwsgi --ini uwsgi.ini
uwsgi --reload uwsgi.pid
uwsgi --stop uwsgi.pid
ps -ef|grep uwsgi
2.5 安装Nginx
yum –y install gcc
yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
yum -y install nginx --skip-broken
systemctl start nginx
systemctl stop nginx
systemctl status nginx
2.6 安装Mysql8.0
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
sudo yum --enablerepo=mysql80-community install mysql-community-server --nogpgcheck
systemctl start mysqld
systemctl status mysqld
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
ALTER USER 'root'@'localhost' identified by 'Sn@wc00000';
create user 'root'@'%' identified with mysql_native_password by 'Sn@wc00000';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
show database;
create database <database-name>;
*注意*
django连接mysql报错2026协议版本不匹配,在mysql /etc/my.cnf 添加 skip_ssl
3 django项目开发环境与生产环境分离
django项目开发与测试环境settings文件分离 step1. 在django项目settings.py文件下创建settings文件夹 step2. 在settings文件夹下创建四个文件 /settings init.py base.py #公共配置文件 develop.py #开发环境配置文件 product.py #生产环境配置文件 step3. 将settings.py文件中公共内容复制到/settings/base.py中,将其他分环境区分放入develop.py或product.py,如数据库配置、debug配置等 step4. 修改manage.py文件
import os
import sys
def main():
profile = os.environ.get('PROJECT_PROFILE', 'develop')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toms.settings.%s' % profile)
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
step5. 修改wgsi.py文件
"""
WSGI config for toms project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toms.settings.product')
application = get_wsgi_application()
step6. pycharm中配置django项目框架内容
文件—设置—语言和框架—django—设置:…\settings\develop.py
step7. pycharm配置django服务器运行环境
运行—编辑配置—django服务器—环境变量: DJANGO_SETTINGS_MODULE=toms.settings.develop
Step8. 生产环境centos7服务器配置环境变量
cd /etc/profile.d vi python3.sh export PROJECT_PROFILE=product
|