日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/10 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CentOS7 python django框架 天天生鲜项目 搭建流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

github源代碼

演示鏈接

CentOS python3 安裝

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

  • 解壓 : tar -zxvf Python-3.9.7.tgz

  • 進入解壓后的目錄 :cd Python-3.9.7

  • 編譯安裝

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

    • 其中--prefix是Python的安裝目錄,指定把python安裝到那里,同時也安裝了setuptools和pip工具
  • 如果編譯安裝失敗:一般情況都是因為缺少編譯環境,通常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

  • 進入安裝目錄 cd /usr/local/python39

  • 創建軟連接 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

  • 驗證是否配置成功: python3 --version

  • 創建使用python虛擬環境

  • pip3 install virtualenv 安裝虛擬環境
  • pip3 install virtualenvwrapper 安裝虛擬環境擴展包
  • 創建一個文件夾,用于存放所有的虛擬環境 (這里存放在/home/python/virtualenvs)

    mkdir -p /home/python/virtualenvs

  • 編輯~/.bashrc文件,添加下面兩行

    vim ~/.bashrc

    • export WORKON_HOME=/home/python/virtualenvs
    • source /usr/local/python39/bin/virtualenvwrapper.sh
  • 使用srouce ~/.bashrc命令使其生效
  • 如果提示-bash: /usr/local/python39/bin/virtualenvwrapper.sh: No such file or directory,那么可以使用find / -name virtualenvwrapper.sh 查看這個文件的位置,將該位置替換到~/.bashrc文件中的source對應的路徑,再次執行上述命令
  • 如果提示/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
  • 創建虛擬環境命令: mkvirtualenv 虛擬環境名
    • mkvirtualenv -p python3 fresh_everyday fresh_everyday:虛擬環境名 -p:指定python版本
  • 進入虛擬環境工作: workon fresh_everyday 接下來的操作都在虛擬環境中進行
  • 退出虛擬環境:deactivate
  • 安裝項目需要的依賴包

  • 安裝Django pip install django 4.0.4
  • 安裝pymysql pip install pymysql 1.0.2
  • 安裝django-tinymce 富文本編輯器 pip install django-tinymce 3.4.0
  • 安裝itsdangerous 加密模塊 pip install itsdangerous 2.1.2
  • 安裝celery 異步任務 pip install -U Celery 5.2.7
  • 安裝redis pip install reids 4.3.4
  • 安裝django-redis pip install django-redis 5.2.0
  • 安裝alipay-sdk-python pip install alipay-sdk-python 3.6.332
  • 安裝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即可
  • 安裝py3Fdfs 分布式文件系統 pip install py3Fdfs==2.1.0
  • 安裝jieba 結巴分詞 pip install jieba 0.42.1
  • 安裝Pillow ImageField依賴 python -m pip install Pillow
  • mysql的安裝

    mysql 下載安裝mysql,mysql數據庫設置

    項目部署

  • 在/root目錄中創建文件夾fresh-everyday,將https://github.com/lang1427/py_fresh-everyday/tree/main/app中的所有文件放入該fresh-everyday文件中

  • 全文檢索 配置修改

  • 找到安裝目錄下的haystack目錄 pip show django-haystack
    /home/python/virtualenvs/fresh_everyday/lib/python3.9/site-packages/haystack/backends
  • 在該目錄下創建ChineseAnalyzer.py文件touch ChineseAnalyzer.py,文件內容如下:import jieba from whoosh.analysis import Tokenizer, Tokenclass 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 = wt.boost = 1.0if 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 tdef ChineseAnalyzer():return ChineseTokenizer()
  • 復制 whoosh_backend.py 文件,改為 whoosh_cn_backend.py
  • 打開復制出來的新文件,引入中文分析類,內部采用jieba分詞 from .ChineseAnalyzer import ChineseAnalyzer
  • 更改詞語分析類

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

  • 數據庫相關

    • 創建數據庫: 通過 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;命令導入數據

  • 配置自己的郵箱規則 settings.py

    # 郵箱配置EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'EMAIL_HOST = 'smtp.qq.com'EMAIL_PORT = 587EMAIL_HOST_USER = '' # 發送者郵箱EMAIL_HOST_PASSWORD = '' # 授權碼
  • uwsgi + nginx 搭建服務

    • nginx的安裝可參考 CSDN nginx 【安裝Nginx】
  • 安裝uwsgi pip install uwsgi
  • 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 # 修改點
  • uwsgi的啟動和停止 (此命令在/root/fresh-everyday目錄下執行)
    • 啟動:uwsgi --ini 配置文件路徑 uwsgi --ini uwsgi.ini
    • 停止:uwsgi --stop uwsgi.pid uwsgi --stop uwsgi.pid
  • 使用nginx,需要將uwsgi.ini配置文件中的http形式變成socket形式,重啟uwsgi
  • 配置收集靜態文件
    • settings.py中設置STATIC_ROOT=收集的靜態文件路徑 例如:/var/www/fresh_everyday/static
    • django收集靜態文件的命令:python manage.py collectstatic 執行該命令,會把項目中所使用的靜態文件收集到STATIC_ROOT指定的目錄下
  • 修改添加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/;} }
  • 重啟nginx
  • 云服務器開放nginx server中的端口 即8000;就可以通過 服務器ip:8000 訪問該項目了

  • Redis 安裝

    redis安裝說明文檔

    異步任務

  • 修改*/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
  • 在*/root/fresh-everyday*目錄下,調用異步任務: setsid celery -A celery_tasks.tasks worker -l info

  • 通過 ps -ef | grep celery 查看celery進程狀態

  • 通過kill -9 celery進程id 關閉celery進程

  • FDFS 分布式文件系統的搭建

  • 安裝解壓縮工具

    yum install -y unzip zip

    • unzip 壓縮文件.zip 解壓縮zip文件夾命令
  • 將https://github.com/lang1427/py_fresh-everyday/tree/main/FastDFS壓縮文件傳遞到服務器上/root目錄中 (附安裝說明文檔)
  • 如果你的nginx是按照上面項目部署第5點安裝的,則不需要nginx-1.8.1.tar.gz, (下面的操作,我都認為你是按照項目部署第5點已安裝好了nginx;如果不是可以移步參考FastDFS分布式存儲服務器安裝.docx文檔)

  • 安裝fastdfs依賴包

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

  • 安裝fastdfs

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

  • 配置跟蹤服務器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

  • 配置存儲服務器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

  • 啟動tracker 和 storage

    sudo service fdfs_trackerd start
    sudo service fdfs_storaged start

    • 驗證 fdfs端口是否啟用:netstat -apn|grep fdfs
  • 安裝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等)

    總結

    以上是生活随笔為你收集整理的CentOS7 python django框架 天天生鲜项目 搭建流程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。