github源代码
演示链接
安装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工具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
pip3 install virtualenv
安装虚拟环境pip3 install virtualenvwrapper
安装虚拟环境扩展包mkdir -p /home/python/virtualenvs
~/.bashrc
文件,添加下面两行
vim ~/.bashrc
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 virtualenvwrappervirtualenvwrapper.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
pip install django
4.0.4pip install pymysql
1.0.2pip install django-tinymce
3.4.0pip install itsdangerous
2.1.2pip install -U Celery
5.2.7pip install reids
4.3.4pip install django-redis
5.2.0pip install alipay-sdk-python
3.6.332pip install django-haystack
3.2.1 pip install whoosh
2.7.4
django-haystack
失败,提示ModuleNotFoundError: No module named ‘_ctypes’,yum install libffi-devel -y
,pip install py3Fdfs==2.1.0
pip install jieba
0.42.1python -m pip install Pillow
mysql 下载安装mysql,mysql数据库设置
在/root目录中创建文件夹fresh-everyday,将https://github.com/lang1427/py_fresh-everyday/tree/main/app中的所有文件放入该fresh-everyday文件中
全文检索 配置修改
pip show django-haystack
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()
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 = 587
EMAIL_HOST_USER = '' # 发送者邮箱
EMAIL_HOST_PASSWORD = '' # 授权码
uwsgi + nginx 搭建服务
pip install uwsgi
DEBUG=False,ALLOWED_HOSTS=['*']
[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 # 修改点
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 server中的端口 即8000;就可以通过 服务器ip:8000 访问该项目了
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进程
yum install -y unzip zip
unzip 压缩文件.zip
解压缩zip文件夹命令如果你的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
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
FDFS_URL='http://172.18.255.178:8888/' # 替换成 fdfs存储服务器上nginx的IP和端口号
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
netstat -apn|grep fdfs
看出来的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等)