达达商城是一个综合性的在线购物平台,它提供了一个用户友好的界面和高效的后端服务,以确保用户能够享受到流畅的购物体验。以下是对达达商城的详细介绍,包括其前端和后端的技术实现。
达达商城是一个提供各类商品的在线购物平台,包括但不限于电子产品、服装、家居用品、图书等。它的目标是为用户提供便捷、安全、高效的购物体验,同时提供竞争力的价格和优质的客户服务。
前端技术Nginx:Nginx作为Web服务器,负责处理HTTP请求和响应。它以其高性能、稳定性和丰富的功能而闻名,能够处理大量的并发连接和请求,非常适合作为电商网站的前端服务器。配置:Nginx通常配置为静态文件服务器,提供图片、CSS、JavaScript等静态资源。同时,它也负责将动态请求代理到后端应用服务器,前端使用的JS框架为React,。
后端技术使用Python,Python是一种高级编程语言,以其简洁的语法和强大的功能而受到开发者的喜爱。在达达商城中,Python用于编写后端逻辑,包括业务逻辑处理、数据库操作、API开发等,框架:可能使用的Python Web框架包括Django或Flask。Django提供了一个全栈式的解决方案,而Flask则提供了更多的灵活性和轻量级的选择。本次项目采用Django框架。同时使用uWSGI作为应用服务器,负责运行Python编写的Web应用程序。它将Nginx的请求转换为WSGI调用,并将应用程序的响应返回给Nginx。uWSGI可以配置为多个工作进程和线程,以提高应用程序的处理能力。它还支持热重载,允许在不中断服务的情况下部署新的代码。
-
基础环境准备[root@DadaShop ~]# systemctl stop firewalld.service –now
[root@DadaShop ~]# setenforce 0
[root@DadaShop ~]# yum -y install zip
[root@DadaShop ~]# unzip dashopt.zip
查看本次项目所需软件,如下所示:
[root@DadaShop ~]# ls
anaconda-ks.cfg dashopt_back.tar.gz dashopt_front.tar.gz dashopt.sh dashopt.zip requirement.txt -
数据库准备(MySQL和Redis)安装mysql数据库软件包并启动服务
[root@DadaShop ~]# yum -y install mysql mysql-server
[root@DadaShop ~]# systemctl enable mysqld –now
配置项目用户
[root@DadaShop ~]#mysqladmin -uroot -p password ‘123456’
[root@DadaShop ~]#mysql -uroot -p123456
mysql> create user prouser@’%’ identified by ‘123456’;
mysql> grant all privileges on *.* to prouser@’%’ ;
安装redis数据库软件包并启动服务
[root@DadaShop ~]# yum -y install redis
修改redis配置文件配置密码
[root@DadaShop ~]# vim /etc/redis.conf
[root@DadaShop ~]# sed -n ’69p;507p’ /etc/redis.conf
bind 127.0.0.1 192.168.10.110
requirepass 123456
[root@DadaShop ~]# systemctl enable redis –now
[root@DadaShop ~]# ss -anptul | grep 6379
tcp LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* users:((“redis-server”,pid=14371,fd=6))
[root@DadaShop ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
-
项目后端程序部署创建项目目录,解压后端程序代码
[root@DadaShop ~]#mkdir -p /usr/local/project/dadashop
[root@DadaShop ~]#mv dashopt_back.tar.gz /usr/local/project/dadashop/
[root@DadaShop ~]#cd /usr/local/project/dadashop/
[root@DadaShop ~]#tar -xf dashopt_back.tar.gz
[root@DadaShop dadashop]# ls dashopt
carts dashopt dashopt.ini db.sqlite3 demo goods goods_data.json key_files manage.py media orders pays users utils
#更新本机pip版本
[root@DadaShop dadashop]# python3 -m pip install –upgrade pip -i
https://pypi.tuna.tsinghua.edu.cn/simple/
#创建项目虚拟环境
[root@DadaShop dadashop]# python3 -m venv dadaenv
#激活dadaenv虚拟环境
[root@DadaShop dadashop]# source dadaenv/bin/activate
(dadaenv) [root@DadaShop dadashop]#python3 -m pip install –upgrade pip -i
https://pypi.tuna.tsinghua.edu.cn/simple/
(dadaenv) [root@DadaShop dadashop]# python3 -m pip install –upgrade pip -i
https://pypi.tuna.tsinghua.edu.cn/simple/
(dadaenv) [root@DadaShop dadashop]# python -m pip list
Package Version
———- ——-
pip 21.3.1
setuptools 39.2.0
(dadaenv) [root@DadaShop dadashop]#
#安装达达商城项目依赖
#(如果安装过程中出现报错,在requirement.txt中将报错模块注释即可)
(dadaenv) [root@DadaShop dadashop]# cp /root/requirement.txt ./
(dadaenv) [root@DadaShop dadashop]# ls requirement.txt
requirement.txt
(dadaenv) [root@DadaShop dadashop]# yum -y install python3-devel mysql-devel gcc make
cairo-devel gobject-introspection-devel cairo-gobject dbus-devel dbus-glib-devel
(dadaenv) [root@DadaShop dadashop]# python -m pip install -r requirement.txt -i
https://pypi.tuna.tsinghua.edu.cn/simple/安装完成模块,进行验证
(dadaenv) [root@DadaShop dadashop]# python3 -m pip list
Package Version
——————- ———
amqp 5.1.1
asgiref 3.4.1
billiard 3.6.4.0
cached-property 1.5.2
celery 5.1.2
certifi 2022.12.7
cffi 1.15.1
charset-normalizer 2.0.12
click 7.1.2
click-didyoumean 0.3.0
click-plugins 1.1.1
click-repl 0.2.0
cryptography 40.0.1
Django 3.2.18
django-cors-headers 3.10.1
django-redis 5.2.0
idna 3.4
importlib-metadata 4.8.3
kombu 5.1.0
mysqlclient 2.1.1
Pillow 8.4.0
pip 21.3.1
prompt-toolkit 3.0.36
pycairo 1.20.1
pycparser 2.21
pycryptodomex 3.17
PyJWT 2.4.0
pyOpenSSL 23.1.1
pyparsing 3.0.9
python-alipay-sdk 3.2.0
python-dateutil 2.6.1
pytz 2023.3
pyudev 0.21.0
redis 3.5.3
redis-py-cluster 2.1.3
requests 2.27.1
ronglian-sms-sdk 1.0.0
setuptools 39.2.0
six 1.11.0
sqlparse 0.4.3
typing_extensions 4.1.1
urllib3 1.26.15
uWSGI 2.0.21
vine 5.0.0
wcwidth 0.2.6
wheel 0.37.1
zipp 3.6.0
WARNING: You are using pip version 21.3.1; however, version 24.3.1 is available.
You should consider upgrading via the ‘/usr/local/project/dadashop/dadaenv/bin/python3 -m pip install –upgrade pip’ command.
(dadaenv) [root@DadaShop dadashop]##调整达达商城配置文件
(dadaenv) [root@DadaShop dadashop]# vim dashopt/dashopt/settings.py
(dadaenv) [root@DadaShop dadashop]# sed -rn ‘83,92p;170,222p;237p’
dashopt/dashopt/settings.py
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’,
‘NAME’: ‘dashopt’,
‘HOST’: ‘192.168.10.110‘,
‘USER’: ‘prouser’,
‘PASSWORD’: ‘123456’,
‘PORT’: ‘3306’,
}
}
CACHES = {
# db1: 存储邮件激活随机数
“default”: {
“BACKEND”: “django_redis.cache.RedisCache”,
“LOCATION”: “redis://192.168.10.110/1″,
“OPTIONS”: {
“CLIENT_CLASS”: “django_redis.client.DefaultClient”,
“PASSWORD”: “123456”
}
},
# db2: 存储短信验证码
“sms”: {
“BACKEND”: “django_redis.cache.RedisCache”,
“LOCATION”: “redis://192.168.10.110:6379/2″,
“OPTIONS”: {
“CLIENT_CLASS”: “django_redis.client.DefaultClient”,
“PASSWORD”: “123456”
}
},
# db3: 首页缓存
“index”: {
“BACKEND”: “django_redis.cache.RedisCache”,
“LOCATION”: “redis://192.168.10.110:6379/3″,
“OPTIONS”: {
“CLIENT_CLASS”: “django_redis.client.DefaultClient”,
“COMPRESSOR”: “django_redis.compressors.zlib.ZlibCompressor”,
“PASSWORD”: “123456”
}
},
# db4: 详情页缓存
“detail”: {“BACKEND”: “django_redis.cache.RedisCache”,
“LOCATION”: “redis://192.168.10.110:6379/4″,
“OPTIONS”: {
“CLIENT_CLASS”: “django_redis.client.DefaultClient”,
“COMPRESSOR”: “django_redis.compressors.zlib.ZlibCompressor”,
“PASSWORD”: “123456”
}
},
# db5: 存储购物车数据
“carts”: {
“BACKEND”: “django_redis.cache.RedisCache”,
“LOCATION”: “redis://192.168.10.110:6379/5″,
# 默认300秒,None为永久存储
# 购物车数据长驻内存
“TIMEOUT”: None,
“OPTIONS”: {
“CLIENT_CLASS”: “django_redis.client.DefaultClient”,
“COMPRESSOR”: “django_redis.compressors.zlib.ZlibCompressor”,
“PASSWORD”: “123456”
}
},
}
PIC_URL=”http://192.168.10.110:8000/media/”#生成数据迁移文件 (由于上一步骤有些python模块没有安装成功,则生成数据迁移命令python3 manage.py makemigrations可能报错,若报错则需要重新安装python模块,安装指定清华大学的源,安装命令如:python -m pip install corsheaders -i https://pypi.tuna.tsinghua.edu.cn/simple/,其他模块如django-cors-headers、mysqlclient、libmysqlclient-devel、Pillow 可能也需要安装
(dadaenv) [root@DadaShop dadashop]# cd dashopt/
(dadaenv) [root@DadaShop dashopt]# pwd
/usr/local/project/dadashop/dashopt
(dadaenv) [root@DadaShop dashopt]# ls
carts dashopt.ini demo goods_data.json manage.py orders users
dashopt db.sqlite3 goods key_files media pays utils
(dadaenv) [root@DadaShop dashopt]# python3 manage.py makemigrations
#导入迁移文件生成数据表
(dadaenv) [root@DadaShop dashopt]# python3 manage.py migrate
…
Applying goods.0001_initial… OK
Applying users.0001_initial… OK
Applying users.0002_address… OK
Applying users.0003_weiboprofile… OK
Applying orders.0001_initial… OK
Applying sessions.0001_initial… OK
(dadaenv)[root@DadaShop dadashop]# mysql -hlocalhost -uroot -p’123456′ -e “SHOW TABLES FROM dashopt;” | wc -l
mysql: [Warning] Using a password on the command line interface can be insecure.
26
#导入商品数据
(dadaenv) [root@DadaShop dashopt]# python3 manage.py loaddata goods_data.json
(dadaenv) [root@DadaShop dashopt]# mysql -hlocalhost -uroot -p’123456′ -e “SELECT id,name FROM dashopt.goods_sku;”
mysql: [Warning] Using a password on the command line interface can be insecure.
+—-+————————+
| id | name |
+—-+————————+
| 1 | 安踏A蓝色小尺寸|
| 2 | 安踏A灰色大尺寸|
| 3 | 安踏B蓝色小尺寸|
+—-+————————+
#创建超级管理员用户
(dadaenv) [root@DadaShop dashopt]# python manage.py createsuperuser
/usr/local/project/dadashop/dadaenv/lib64/python3.6/site-packages/jwt/utils.py:7:
CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core
team. Therefore, support for it is deprecated in cryptography. The next release of
cryptography will remove support for Python 3.6.
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurve
用户名(leave blank to use ‘root’): admin
电子邮件地址: admin@tedu.cn
Password: #admin
Password (again): #admin
密码跟用户名太相似了。
密码长度太短。密码必须包含至少8 个字符。
这个密码太常见了。
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.#测试运行达达商城项目
(dadaenv) [root@DadaShop dashopt]# python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks…
#打开浏览器,访问测试http://192.168.10.110:8000/admin如下:
#测试uWSGI ,uWSGI可以作为一个WSGI容器,运行Python编写的Web应用程序,测试如下:
(dadaenv) [root@DadaShop dashopt]# vim test.py
(dadaenv) [root@DadaShop dashopt]# cat test.py
def application(env, res):
res = (‘200 OK’, [(‘Content-Type’,’text/html’)])
return [b’hello worldn’]
(dadaenv) [root@DadaShop dashopt]# uwsgi –http 127.0.0.1:8001 –wsgi-file test.py
#另开终端访问测试
[root@DadaShop ~]# curl http://localhost:8001
hello world
#清理测试文件
(dadaenv) [root@DadaShop dashopt]# rm -rf test.py
(dadaenv) [root@DadaShop dashopt]#
#编写ini文件运行uWSGI
(dadaenv) [root@DadaShop dashopt]# vim dashopt.ini
(dadaenv) [root@DadaShop dashopt]# cat dashopt.ini
[uwsgi]
# IP和端口,IP可省略不写,默认为本机IP
http=0.0.0.0:8000
# 设置监听队列
listen=120
# 启用主进程
master=True
# 线程数达达商城前端部署
processes=4
# 进程数
threads=4
# 项目目录
chdir=/usr/local/project/dadashop/dashopt/
# 指定项目的wsgi文件
wsgi-file=/usr/local/project/dadashop/dashopt/dashopt/wsgi.py
# 当服务停止的时候自动移除unix Socket和pid文件
vacuum=True
# 每个进程最大的请求数
max-requests=5000
# 设置日志目录
daemnotallow=/var/log/dashopt.log
# 指定pid文件
pidfile=/var/run/dashopt.pid
# 设置buffer大小
buffer-size=65535
# 如果有修改代码,则服务器上立即reload(重启)
py-autoreload=1
# 设置运行环境
env = DJANGO_SETTINGS_MODULE=dashopt.settings
#env = DJANGO_SETTINGS_MODULE=dashopt.settings-prod
(dadaenv) [root@DadaShop dashopt]# uwsgi –ini dashopt.ini
[uWSGI] getting INI configuration from dashopt.ini
(dadaenv) [root@DadaShop dashopt]# ss -antpul | grep 8000
tcp LISTEN 0 120 0.0.0.0:8000 0.0.0.0:* users:
((“uwsgi”,pid=43457,fd=6),(“uwsgi”,pid=43442,fd=6))
(dadaenv) [root@DadaShop dashopt]#
-
配置项目前端程序,发布服务#解压前端页面代码,移动至项目目录
(dadaenv) [root@DadaShop dadashop]# mv /root/dashopt_front.tar.gz ./
(dadaenv) [root@DadaShop dadashop]# tar xf dashopt_front.tar.gz
(dadaenv) [root@DadaShop dadashop]# ls
dadaenv dadashop dashopt dashopt_back.tar.gz dashopt_front.tar.gz requirement.txt
(dadaenv) [root@DadaShop dadashop]# ls dadashop/
static templates
(dadaenv) [root@DadaShop dadashop]#
(dadaenv) [root@DadaShop dadashop]# cd
(dadaenv) [root@DadaShop ~]# deactivate
#调整js配置数据流转
[root@DadaShop ~]# vim /usr/local/project/dadashop/dadashop/static/js/init.js
[root@DadaShop ~]# cat /usr/local/project/dadashop/dadashop/static/js/init.js
// 线上的URL
//baseUrl=”http://114.116.244.115:7000″
// 本地测试django_URL
baseUrl=”http://192.168.10.110:8000″
// 本地虚拟机测试
//baseUrl=”http://vhost_ip:8000″
// 本地测试nginx_URL
NbaseUrl=”http://192.168.10.110″
// 本地图片BASEURL
imgUrl= “http://192.168.10.110:8000/media/”
//本地虚拟机图片
//imgUrl=”http://vhost_ip:8000/media/”
##部署nginx发布达达商城前端页面
#安装nginx
[root@DadaShop ~]# yum -y install nginx
#配置nginx
[root@DadaShop ~]# vim /etc/nginx/conf.d/dadashop.conf
[root@DadaShop ~]# cat /etc/nginx/conf.d/dadashop.conf
server {
listen 7000 default_server;
server_name __;
location / {
root “/usr/local/project/dadashop/”;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
[root@DadaShop ~]# systemctl enable nginx.service –now
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service →
/usr/lib/systemd/system/nginx.service.
[root@DadaShop ~]# ss -antpul | grep 7000
tcp LISTEN 0 128 0.0.0.0:7000 0.0.0.0:* users:
((“nginx”,pid=45427,fd=6),(“nginx”,pid=45426,fd=6),(“nginx”,pid=45425,fd=6))
- 访问测试验证
由于本次项目前端没有设置代理服务器,所以测试商城时需要使用绝对路径,具体访问路径如下:
http://192.168.10.110:7000/dadashop/templates/index.html
注册用户如下
登录测试如下
最后总结一下本项目的架构优势。
高性能:Nginx和uWSGI的组合提供了高性能的处理能力,能够应对高流量和高并发的场景。
高可用性:通过Nginx的负载均衡和uWSGI的工作进程管理,达达商城能够实现高可用性。
易维护性:Python和JS的广泛社区支持和丰富的库使得维护和扩展达达商城变得更加容易。
安全性:Nginx和uWSGI都提供了多种安全特性,如SSL/TLS支持,帮助保护数据传输的安全。
达达商城通过结合这些技术和工具,旨在提供一个稳定、快速、安全的在线购物环境,满足不同用户的需求。