CentOS7 python django框架 天天生鲜项目 搭建流程

github源代码

演示链接

CentOS python3 安装

  1. 安装python3.9.7 : wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz

  2. 解压 : tar -zxvf Python-3.9.7.tgz

  3. 进入解压后的目录 :cd Python-3.9.7

  4. 编译安装

    ./configure --prefix=/usr/local/python39
    make && make install

    • 其中--prefix是Python的安装目录,指定把python安装到那里,同时也安装了setuptools和pip工具
    1. 如果编译安装失败:一般情况都是因为缺少编译环境,通常python的编译环境需要zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

    yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

  5. 进入安装目录 cd /usr/local/python39

  6. 创建软连接 Linux已经安装了python2.7.5,这里我们不能将它删除,如果删除,系统可能会出现问题。

    ln -s /usr/local/python39/bin/python3.9 /usr/bin/python3
    ln -s /usr/local/python39/bin/pip3 /usr/bin/pip3

  7. 验证是否配置成功: python3 --version

创建使用python虚拟环境

  1. pip3 install virtualenv 安装虚拟环境
  2. pip3 install virtualenvwrapper 安装虚拟环境扩展包
  3. 创建一个文件夹,用于存放所有的虚拟环境 (这里存放在/home/python/virtualenvs)

    mkdir -p /home/python/virtualenvs

  4. 编辑~/.bashrc文件,添加下面两行

    vim ~/.bashrc

    • export WORKON_HOME=/home/python/virtualenvs
    • source /usr/local/python39/bin/virtualenvwrapper.sh
  5. 使用srouce ~/.bashrc命令使其生效
    1. 如果提示-bash: /usr/local/python39/bin/virtualenvwrapper.sh: No such file or directory,那么可以使用find / -name virtualenvwrapper.sh 查看这个文件的位置,将该位置替换到~/.bashrc文件中的source对应的路径,再次执行上述命令
    2. 如果提示/usr/bin/python: No module named virtualenvwrapper,是因为安装了2.x和3.x两个版本的python,在安装时使用的是sudo pip3 install virtualenvwrapper
      在我运行的时候默认使用的是python2.x,但在python2.x中不存在对应的模块,只需要在bashrc文件里面加入如下命令即可,再次执行上述命令
    • VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/python/virtualenvs/get_env_details

  1. 创建虚拟环境命令: mkvirtualenv 虚拟环境名
    • mkvirtualenv -p python3 fresh_everyday fresh_everyday:虚拟环境名 -p:指定python版本
  2. 进入虚拟环境工作: workon fresh_everyday 接下来的操作都在虚拟环境中进行
  3. 退出虚拟环境:deactivate

安装项目需要的依赖包

  1. 安装Django pip install django 4.0.4
  2. 安装pymysql pip install pymysql 1.0.2
  3. 安装django-tinymce 富文本编辑器 pip install django-tinymce 3.4.0
  4. 安装itsdangerous 加密模块 pip install itsdangerous 2.1.2
  5. 安装celery 异步任务 pip install -U Celery 5.2.7
  6. 安装redis pip install reids 4.3.4
  7. 安装django-redis pip install django-redis 5.2.0
  8. 安装alipay-sdk-python pip install alipay-sdk-python 3.6.332
  9. 安装django-haystack和whoosh 全文检索 pip install django-haystack 3.2.1 pip install whoosh 2.7.4
    • 安装django-haystack失败,提示ModuleNotFoundError: No module named ‘_ctypes’
      需要退出虚拟环境安装外部函数库(libffi) yum install libffi-devel -y
      然后回到python的安装过程,重新安装python即可
  10. 安装py3Fdfs 分布式文件系统 pip install py3Fdfs==2.1.0
  11. 安装jieba 结巴分词 pip install jieba 0.42.1
  12. 安装Pillow ImageField依赖 python -m pip install Pillow

mysql的安装

mysql 下载安装mysql,mysql数据库设置

项目部署

  1. 在/root目录中创建文件夹fresh-everyday,将https://github.com/lang1427/py_fresh-everyday/tree/main/app中的所有文件放入该fresh-everyday文件中

  2. 全文检索 配置修改

    1. 找到安装目录下的haystack目录 pip show django-haystack
      /home/python/virtualenvs/fresh_everyday/lib/python3.9/site-packages/haystack/backends
    2. 在该目录下创建ChineseAnalyzer.py文件touch ChineseAnalyzer.py,文件内容如下:
      import jieba 
      from whoosh.analysis import Tokenizer, Token
      
      class ChineseTokenizer(Tokenizer):
          def __call__(self, value, positions=False, chars=False,
                      keeporiginal=False, removestops=True,
                      start_pos=0, start_char=0, mode='', **kwargs):
              t = Token(positions, chars, removestops=removestops, mode=mode, **kwargs)
              seglist = jieba.cut(value, cut_all=True)
              for w in seglist:
                  t.original = t.text = w
                  t.boost = 1.0
                  if positions:
                      t.pos = start_pos + value.find(w)
                  if chars:
                      t.startchar = start_char + value.find(w)
                      t.endchar = start_char + value.find(w) + len(w)
                  yield t
      
      def ChineseAnalyzer():
          return ChineseTokenizer()
      
    3. 复制 whoosh_backend.py 文件,改为 whoosh_cn_backend.py
    4. 打开复制出来的新文件,引入中文分析类,内部采用jieba分词 from .ChineseAnalyzer import ChineseAnalyzer
    5. 更改词语分析类

      查找 analyzer=field_class.analyzer or StemmingAnalyzer() 改为 analyzer=field_class.analyzer or ChineseAnalyzer()

  3. 数据库相关

    • 创建数据库: 通过 mysql -uroot -p命令 输入自己设置的mysql密码登录mysql数据库中;通过CREATE DATABASE fresh_everyday DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_bin;命令 创建数据库

    • 修改项目文件中数据库链接的指向 settings.py 对应自己的主机、端口、用户名、密码

      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.mysql',
              'NAME': 'fresh_everyday',
              'HOST':'',
              'PORT':'',
              'USER':'',
              'PASSWORD':'',
              'OPTIONS': {
                  "init_command": "SET foreign_key_checks = 0;" # 关闭外键约束
              }
          }
      }
      
    • 删除各个应用程序中migrations目录下xxxx_initial.py文件,比如 /root/fresh-everyday/goods/migrations/0001_initial.py

    • 执行迁移

      python manage.py makemigrations

      python manage.py migrate

    • 导入数据:将https://github.com/lang1427/py_fresh-everyday/dailyfresh.sql文件传递到服务器上/root/fresh-everyday目录中,连接到数据库后,选择fresh_everyday数据库(use fresh_everyday),通过source dailyfresh.sql;命令导入数据

  4. 配置自己的邮箱规则 settings.py

        # 邮箱配置
        EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
        EMAIL_HOST = 'smtp.qq.com'
        EMAIL_PORT = 587
        EMAIL_HOST_USER = '' # 发送者邮箱
        EMAIL_HOST_PASSWORD = '' # 授权码
    
  5. uwsgi + nginx 搭建服务

    • nginx的安装可参考 CSDN nginx 【安装Nginx】
    1. 安装uwsgi pip install uwsgi
    2. uwsgi的配置
      • 项目部署时,需要设置 settings.py 文件夹下的DEBUG=False,ALLOWED_HOSTS=['*']
      • uwsgi.ini 配置文件(在/root/fresh-everyday目录下)
        [uwsgi]
        #使用nginx连接时使用
        socket=127.0.0.1:8001  # 配置nginx时使用
        #直接做web服务器使用 python manage.py runserver ip:port
        #http=127.0.0.1:8000  # 单独uwsgi时使用
        #项目目录
        chdir=/root/fresh-everyday   # 修改点
        #项目中wsgi.py文件的目录,相对于项目目录
        wsgi-file=/root/fresh-everyday/app/wsgi.py  # 修改点
        #指定启动的工作进程数
        processes=4
        #指定工作进程中的线程数
        threads=2
        master=True
        #保存启动之后主进程的pid
        pidfile=uwsgi.pid
        #设置uwsgi后台运行,uwsgi.log保存日志信息
        daemonize=uwsgi.log
        #设置虚拟环境的路径
        virtualenv=/home/python/virtualenvs/fresh_everyday  # 修改点
        
    3. uwsgi的启动和停止 (此命令在/root/fresh-everyday目录下执行)
      • 启动:uwsgi --ini 配置文件路径 uwsgi --ini uwsgi.ini
      • 停止:uwsgi --stop uwsgi.pid uwsgi --stop uwsgi.pid
    4. 使用nginx,需要将uwsgi.ini配置文件中的http形式变成socket形式,重启uwsgi
    5. 配置收集静态文件
      • settings.py中设置STATIC_ROOT=收集的静态文件路径 例如:/var/www/fresh_everyday/static
      • django收集静态文件的命令:python manage.py collectstatic 执行该命令,会把项目中所使用的静态文件收集到STATIC_ROOT指定的目录下
    6. 修改添加nginx的配置文件vim nginx.conf
      # django 天天生鲜部署 添加server
      server {
          listen  8000;
          server_name localhost;
      
          location / {
              # 包含uwsig请求的参数
              include uwsgi_params;
              uwsgi_pass 127.0.0.1:8001; # 搭配uwsgi的socket值
          }
      
          location /static {
              # 指定静态文件存放的目录
              alias /var/www/fresh-every/static/;
          }
      }
      
    7. 重启nginx
  6. 云服务器开放nginx server中的端口 即8000;就可以通过 服务器ip:8000 访问该项目了

Redis 安装

redis安装说明文档

异步任务

  1. 修改*/root/fresh-everyday/celery_tasks/tasks.py*文件内容

    • 127.0.0.1:8000 => 替换成对应的服务器ip:port
    • F:\\fresh-everyday\\app\\templates\static_index.html => /root/fresh-everyday/templates/static_index.html
  2. 在*/root/fresh-everyday*目录下,调用异步任务: setsid celery -A celery_tasks.tasks worker -l info

  3. 通过 ps -ef | grep celery 查看celery进程状态

  4. 通过kill -9 celery进程id 关闭celery进程

FDFS 分布式文件系统的搭建

  1. 安装解压缩工具

    yum install -y unzip zip

    • unzip 压缩文件.zip 解压缩zip文件夹命令
  2. https://github.com/lang1427/py_fresh-everyday/tree/main/FastDFS压缩文件传递到服务器上/root目录中 (附安装说明文档)
    1. 如果你的nginx是按照上面项目部署第5点安装的,则不需要nginx-1.8.1.tar.gz(下面的操作,我都认为你是按照项目部署第5点已安装好了nginx;如果不是可以移步参考FastDFS分布式存储服务器安装.docx文档)

    2. 安装fastdfs依赖包

      unzip /root/libfastcommon-master.zip
      cd /root/libfastcommon-master
      ./make.sh
      sudo ./make.sh install

    3. 安装fastdfs

      unzip /root/fastdfs-master.zip
      cd /root/fastdfs-master
      ./make.sh
      sudo ./make.sh install

    4. 配置跟踪服务器tracker

      cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
      mkdir –p /home/python/fastdfs/tracker
      vim /etc/fdfs/tracker.conf 修改 base_path=/home/python/fastdfs/tracker

    5. 配置存储服务器storage

      cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
      mkdir –p /home/python/fastdfs/storage
      vim /etc/fdfs/storage.conf 修改 ①:base_path=/home/python/fastdfs/storage ②:store_path0=/home/python/fastdfs/storage ③:tracker_server=搭载存储的服务器ip地址:22122

    6. 启动tracker 和 storage

      sudo service fdfs_trackerd start
      sudo service fdfs_storaged start

      • 验证 fdfs端口是否启用:netstat -apn|grep fdfs
    7. 安装fastdfs-nginx-module

      unzip /root/fastdfs-nginx-module-master.zip
      /usr/local/nginx/sbin/nginx -V nginx运行目录查看nginx版本等信息
      - configure arguments: --with-http_ssl_module 目前configure配置只有这个,复制 –with-http_ssl_module 内容
      cd /root/nginx-1.16.0/ 跳转到nginx的安装目录
      ./configure --with-http_ssl_module --add-module=/root/fastdfs-nginx-module-master/src 添加新模块,同时也要保留原有的配置
      make
      /usr/local/nginx/sbin/nginx -s stop 停止nginx运行
      cp /root/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/nginx
      - cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy 先停止nginx的使用,否则不能覆盖nginx运行程序文件
      cp /root/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/mod_fastdfs.conf
      vim /etc/fdfs/mod_fastdfs.conf 修改 ①:connect_timeout=10 ②:tracker_server=搭载存储的服务器ip地址:22122 ③:url_have_group_name=true ④:store_path0=/home/python/fastdfs/storage
      cp /root/fastdfs-master/conf/http.conf /etc/fdfs/http.conf
      cp /root/fastdfs-master/conf/mime.types /etc/fdfs/mime.types
      vim /usr/local/nginx/conf/nginx.conf

            server {
                listen       8889;   # 原本是8888的,但是端口占用了
                server_name  localhost;
                location ~/group[0-9]/ {
                    ngx_fastdfs_module;
                }
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                    root   html;
                }
            }
      

      /usr/local/nginx/sbin/nginx 启动nginx

  • 修改settings.py
FDFS_URL='http://172.18.255.178:8888/'  # 替换成 fdfs存储服务器上nginx的IP和端口号
  • 修改/root/fresh-everyday/fdfs/client.conf
tracker_server=172.18.255.178:22122  # 替换成你搭建fdfs服务器的ip
  • 重启 tracker 和 storage

    service fdfs_trackerd start
    service fdfs_storaged start

  • 重启python服务,即:uwsgi --stop uwsgi.pid,uwsgi --ini uwsgi.ini

  • 开放服务器端口:8889,22122,23000

    • 23000 端口 是 通过netstat -apn|grep fdfs 看出来的

创建一个超级管理员 更改商品图片 fdfs的url

python manage.py createsuperuser

填写用户名,邮箱,密码;通过该超级管理员访问 部署该网站的ip:port/admin界面,登录管理后台,修改 商品SKU、商品种类表、首页活动表、首页轮播图表 中的各个数据中的图片

生成搜索索引数据

python manage.py rebuild_index

支付宝支付

https://github.com/lang1427/py_fresh-everyday/支付宝支付使用.docx

如果无效,就重启服务(nginx,celery,fdfs,uwsgi等)

你可能感兴趣的:(网站部署,python,django,linux,centos,redis)