django运维项目开发(一、环境部署)

django运维项目开发(一、环境部署),第1张

django运维项目开发(一、环境部署)

一、项目环境部署
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

#关闭防火墙和selinux
systemctl stop firewalld.service
setenforce 0
getenforce 
#Permissive
#安装python依赖包
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
#更新gcc
yum –y install gcc
#安装wget
yum –y install wget
#下载python文件
wget https://www.python.org/ftp/python/3.7.12/Python-3.7.12.tgz
#安装/编译python3环境
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

#pip安装django2.2版本
pip3 install django==2.2
#设置django软连接
ln –s /usr/local/python3/bin/django-admin /usr/local/bin/django-admin
#创建django项目
django-admin startproject toms
#创建django应用
django-admin startapp app01
#运行django服务器
python3 manage.py runserver 172.16.253.5:8088

2.4 安装Uwsgi

#pip安装uwsgi
pip3 install uwsgi
#设置uwsgi软连接
ln –s /usr/local/python3/bin/uwsgi /usr/local/bin/uwsgi
#在django项目根目录下(与manage.py同级)创建uwsgi.ini配置文件
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
#配置uwsgi主进程文件,必须是决定路径,文件自动创建
pidfile = /home/mysite/toms/uwsgi.pid


#运行uwsgi配置文件,启动uswgi服务
uwsgi --ini uwsgi.ini
#重启uwsgi服务
uwsgi --reload uwsgi.pid
#停止uwsgi服务
uwsgi --stop uwsgi.pid
#查看uwsgi进程
ps -ef|grep uwsgi

2.5 安装Nginx

#更新gcc
yum –y install gcc
#安装依赖组件
yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
#编辑nginx镜像下载源
vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
#下载安装nginx服务器
yum -y install nginx --skip-broken
#启动nginx服务
systemctl start nginx
#停止nginx服务
systemctl stop nginx
#查看nginx服务状态
systemctl status nginx

2.6 安装Mysql8.0

#下载mysql8.0安装包文件
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
#安装mysql8.0
sudo yum --enablerepo=mysql80-community install mysql-community-server --nogpgcheck
#启动mysql服务
systemctl start mysqld
#查看mysql服务状态
systemctl status mysqld

#查看 root权限密码
grep 'temporary password' /var/log/mysqld.log
#本地登陆mysql
mysql -uroot -p
#修改root密码,密码需满足复杂度
ALTER USER 'root'@'localhost' identified by 'Sn@wc00000';
#创建支持远程登陆的root权限
create user 'root'@'%' identified with mysql_native_password by 'Sn@wc00000';
#修改任意地址使用root权限登陆
grant all privileges on *.* to 'root'@'%' with grant option;
#刷新配置
flush privileges;

#查看数据库
show database;
#创建数据库
create database ;

*注意*
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文件

#manage.py
 #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" 
 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文件

#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—设置:…settingsdevelop.py

step7. pycharm配置django服务器运行环境

运行—编辑配置—django服务器—环境变量: DJANGO_SETTINGS_MODULE=toms.settings.develop

Step8. 生产环境centos7服务器配置环境变量

cd /etc/profile.d
vi python3.sh
export PROJECT_PROFILE=product

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5715103.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存