Saleor 是一个基于 Django 的前后端分离的电商网站框架,包含后端核心服务、前端又分别包含后台和前台,本文所使用操作系统为CentOS 7。官方文档目前首推的是使用 Docker启动服务,这样显然更为简单,本文还尝试分别在服务器上以源代码方式安装启动服务。就目前来看,可以考虑的部署方式是后端使用 Docker 打包镜像部署,只需对官方的 Dockerfile 进行部分修改即可,这样可避免依赖包安装之类的麻烦;PostgreSQL在服务器上进行安装,而前后台前端打包后可以直接在服务器上通过 Nginx 配置访问静态页面,也可打包为 Docker 在服务器上部署。
基于 Python 和 Django 的电商解决方案网上总结如下:
- Oscar — Domain-driven e-commerce for Django, open-source.
- Saleor — An e-commerce storefront written in Python, open-source.
- Django-SHOP — A Django based shop system.
- Shuup — A single and multi-vendor application.
https://github.com/mirumee/saleor
Docker 安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
sudo yum update -y # 安装Docker curl -sSL https://get.docker.com/ | sh systemctl start docker systemctl enable docker # sudo usermod -aG docker username sudo curl -L https://github.com/docker/compose/releases/download/1.26.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose # 安装最新版 Git:https://mirrors.edge.kernel.org/pub/software/scm/git/ yum install -y wget wget -O /tmp/git-2.27.0.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.27.0.tar.gz yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker tar -zxf /tmp/git-2.27.0.tar.gz -C /tmp/ cd /tmp/git-2.27.0 ./configure --prefix=/usr/local/git make && make install yum remove git -y # vim /etc/profile GIT_HOME=/usr/local/git export PATH=$PATH:$GIT_HOME/bin source /etc/profile # 进行如下操作前请先进行 GitHub 权限设置 git clone https://github.com/mirumee/saleor-platform.git --recursive --jobs 3 cd saleor-platform docker-compose build # 数据库、静态文件迁移 docker-compose run --rm api python3 manage.py migrate docker-compose run --rm api python3 manage.py collectstatic --noinput # 创建后台用户 docker-compose run --rm api python3 manage.py createsuperuser docker-compose up |
非 Docker 安装启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
sudo yum update -y sudo yum install gcc openssl-devel bzip2-devel libffi-devel libxml2-devel libxslt-devel libjpeg-turbo-devel openldap-devel -y # 安装 Python wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz tar xzf Python-3.8.3.tgz cd Python-3.8.3 ./configure --enable-optimizations sudo make altinstall python3.8 -m venv saleor-venv # 安装 NodeJS curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash sudo yum install nodejs -y curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo sudo yum install yarn -y sudo yum install redhat-rpm-config python-devel python-pip python-cffi libffi-devel cairo pango gdk-pixbuf2 git bzip2 -y git clone https://github.com/mirumee/saleor.git # 在虚拟环境内安装所需 Python依赖 source saleor-venv/bin/activate pip install -r saleor/requirements.txt # PostgreSQL参见数据库安装部分 export SECRET_KEY='<mysecretkey>' cd saleor # 数据库密码等信息可在saleor/settings.py 中进行修改 python manage.py migrate npm install npm run build-schema npm run build-emails # 启动服务 python manage.py runserver |
此时我们的 core 服务就运行起来了
1 2 3 4 5 6 7 |
# 创建后台用户 python manage.py createsuperuser git clone https://github.com/mirumee/saleor-dashboard.git cd saleor-dashboard git checkout 2.10.0 npm i export API_URI='http://localhost:8000/graphql/' |
前台
1 2 3 4 5 |
git clone https://github.com/mirumee/saleor-storefront.git cd saleor-storefront git checkout 2.10.1 npm i npm start |
数据库安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 安装 PostgreSQL sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm sudo yum -y install epel-release yum-utils sudo yum-config-manager --enable pgdg12 sudo yum -y install postgresql12-server postgresql12 postgresql-devel postgresql12-contrib sudo /usr/pgsql-12/bin/postgresql-12-setup initdb # 为超级用户设置密码 sudo passwd postgres systemctl start postgresql-12 systemctl enable postgresql-12 # 创建数据库&用户 su - postgres psql create user saleor with password 'xxxxxxx'; create database saleor owner saleor; grant all on database saleor to saleor; # 将本地的 ident 改为trust |
历史文档:https://docs.saleor.io/docs/2.8.0/getting-started/configuration
https://gitter.im/mirumee/saleor
AWS部署前端
S3配置
参考文章:medium.com
常见问题
1、ImportError: failed to find libmagic. Check your installation
1 |
brew install libmagic # macOS |
2、could not open extension control file “/usr/pgsql-12/share/extension/hstore.control”
1 |
yum install postgresql12-contrib -y |
3、django.db.utils.ProgrammingError: permission denied to create extension “hstore”
HINT: Must be superuser to create this extension.
1 2 |
ALTER USER saleor WITH SUPERUSER; # ALTER USER saleor WITH NOSUPERUSER; |
4、ENOSPC: System limit for number of file watchers reached centos
1 |
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p |
5、pg_dump: server version: 12.3; pg_dump version: 9.2.24
pg_dump: aborting because of server version mismatch
这是因为系统存在多个版本的 PostgreSQL,因此需指定 pg_dump,如:
1 |
/usr/pgsql-12/bin/pg_dump -h 127.0.0.1 -U saleor -W -F t saleor > saleor.tar |
5、no module named saleor
使用 uwsgi 启动时出现如下错误,这是因为没有项目根目下进行启动,可以在uwsgi.ini文件中指定项目根目录:chdir=/xxx/xxx
6、Nginx 日志: Permission denied while connecting to upstream
这是因为开启了 selinux
1 2 3 4 5 6 7 8 |
# 不关闭方案 sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx semodule -i mynginx.pp # 上面的命令所输出的结果 # 命令行关闭 selinux(临时方案) setenforce 0 # 配置文件中关闭 selinux # vi /etc/selinux/config SELINUX=disabled |
Spree
调研时发现了一个基于 Ruby on Rails 的电商框架,国内似乎很少有人使用,但非常优秀,Mark 一下。
https://github.com/spree/spree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
yum install ImageMagick ImageMagick-devel -y # 自行安装 NodeJS # 安装 Ruby yum install gcc-c++ patch readline readline-devel zlib zlib-devel libffi-devel \ openssl-devel make bzip2 autoconf automake libtool bison sqlite-devel -y curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import - curl -L get.rvm.io | bash -s stable source /etc/profile.d/rvm.sh rvm requirements run rvm install 2.7 # rvm list known # ruby --version # 创建应用 gem install rails rails new my_store # -d postgresql可指定数据库,配置文件conf/database.yml cd my_store # vi Gemfile gem 'spree', '~> 4.1' gem 'spree_auth_devise', '~> 4.1' gem 'spree_gateway', '~> 3.7' bundle install # bundle update # 设置环境变量 export MY_STORE_DATABASE_PASSWORD="xxxxx" export RAILS_ENV="production" export PATH="/usr/pgsql-12/bin/:$PATH" rails g spree:install --user_class=Spree::User rails g spree:auth:install rails g spree_gateway:install # bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java rails s # 启动 rails g spree:install --migrate=false --sample=false --seed=false --copy_storefront=false bundle exec rails s |
Your version of SQLite (3.7.17) is too old. Active Record supports SQLite >= 3.8. (RuntimeError)
1 2 3 4 5 6 7 8 9 10 |
wget https://www.sqlite.org/2020/sqlite-autoconf-3320300.tar.gz tar -zxvf sqlite-autoconf-3320300.tar.gz cd sqlite-autoconf-3320300 ./configure --prefix=/opt/sqlite/sqlite3 make && sudo make install /opt/sqlite/sqlite3/bin/sqlite3 --version gem uninstall sqlite3 gem install sqlite3 -- --with-sqlite3-include=/opt/sqlite/sqlite3/include \ --with-sqlite3-lib=/opt/sqlite/sqlite3/lib |
ActionView::Template::Error The asset is not present in the asset pipeline.
1 2 |
# vi config/environments/production.rb config.assets.compile = true |