哔哩哔哩弹幕视频网:
2018全新centos6.9+nginx+mysql+uwsgi+python3+django 环境部署视频教程
如何背单词永不遗忘?暴暴暴暴暴暴涨38000单词量的秘诀-->科学记忆法
1. LNMP一键安装包部署Web环境搭建:
LNMP一键安装包是一个用Linux Shell编写的可以为CentOS/RHEL/Fedora/Aliyun/Amazon、Debian/Ubuntu/Raspbian/Deepin/Mint Linux VPS或独立主机安装LNMP(Nginx/MySQL/PHP)、LNMPA(Nginx/MySQL/PHP/Apache)、LAMP(Apache/MySQL/PHP)生产环境的Shell程序。参考下载+安装教程:点击这里
说明:
数据库目录:/home/wwwdata/mysql/var
数据库用户组:mysql
网站根目录:/home/wwwroot/
网站用户组:www
2.编译安装python3:
2.1. 安装系统开发包
# 安装pip报错 ModuleNotFoundError: No module named '_ctypes' 解决方法
yum install libffi-devel -y
# 报错 ModuleNotFoundError: No module named '_bz2' 解决方法
yum install bzip2-devel
2.2编译安装python3.7
进入tmp路径(本人喜欢把东西都下载到这里),执行以下命令:
cd /tmp/
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
下载完成后(如需Python其他版本:点击此处),执行解压命令:
tar -xzvf Python-3.7.9.tgz
进入解压后的Python-3.7.9文件夹,依次执行以下命令
./configure --prefix=/usr/local/python3
(将python3安装到/usr/local/python3/路径下)
make && make install
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
(以上两个ln命令是为了方便在终端中直接使用python3和pip3命令)
# _bz2.cpython-37m-x86_64-linux-gnu.so文件下载:https://pan.baidu.com/s/1GzUY4E0G2yVUfqxHOIzn1A (提取码:oiwh)
mv _bz2.cpython-36m-x86_64-linux-gnu.so _bz2.cpython-37m-x86_64-linux-gnu.so
cp _bz2.cpython-37m-x86_64-linux-gnu.so /usr/local/python3/lib/python3.7/lib-dynload/
3.安装和使用virtualenv:
前提: python3和pip3都已经安装,执行命令:
pip3 install virtualenv
创建名字为django的虚拟环境,执行命令:
python3 -m venv /home/virtualenv/django
进入虚拟环境,执行命令:
cd /home/virtualenv/django/bin
source activate
4.安装第三方库:
安装Django第三方库列表(requirements.txt),如下:
diff-match-patch==20121119
Django==2.0.1
django-crispy-forms==1.7.0
django-formtools==2.1
django-import-export==0.6.1
django-pure-pagination==0.3.0
django-ranged-response==0.2.0
django-reversion==2.0.12
django-simple-captcha==0.5.6
djangorestframework==3.8.2
et-xmlfile==1.0.1
future==0.16.0
httplib2==0.9.2
jdcal==1.3
odfpy==1.3.6
openpyxl==2.4.9
Pillow==5.0.0
pytz==2018.4
PyYAML==3.12
six==1.11.0
tablib==0.12.1
unicodecsv==0.14.1
uWSGI==2.0.17
xlrd==1.1.0
xlwt==1.3.0
mysqlclient==1.3.12
在django虚拟环境下,安装第三方库列表:
cd /tmp
vim requirements.txt # 复制第三方库列表到此文件下,退出并保存:wq!
pip install -r requirements.txt # 安装第三方库列表
如安装mysqlclient第三方库出现报错(“OSError: mysql_config not found”),修改配置文件并手动安装的解决方案:
cd /tmp
wget http://mirrors.163.com/pypi/packages/6f/86/bad31f1c1bb0cc99e88ca2adb7cb5c71f7a6540c1bb001480513de76a931/mysqlclient-1.3.12.tar.gz#sha256=2d9ec33de39f4d9c64ad7322ede0521d85829ce36a76f9dd3d6ab76a9c8648e5
tar -xzvf mysqlclient-1.3.12.tar.gz
cd mysqlclient-1.3.12
vim setup_posix.py
修改setup_posix.py下,mysql_config.path = "mysql_config" 改成 mysql_config.path = "/usr/local/mysql/bin/mysql_config",保存并退出:wq!
python setup.py install
5.Django项目目录(/home/wwwroot/Warframe),并创建并启用warframe_uwsgi.ini,命令如下:
vim warframe_uwsgi.ini
将以下的配置内容拷贝到warframe_uwsgi.ini,退出并保存:wq!
[uwsgi]
#项目目录
chdir=/home/wwwroot/Warframe
# 启动uwsgi的用户名和用户组
uid=www
gid=www
# 指定项目的application
module=Warframe.wsgi
# 指定sock的文件路径
socket= :8001
# 启用主进程
master=true
# 进程个数
workers=2
vacuum=true
#环境
virtualenv = /home/virtualenv/django
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=1028
# 设置日志目录
daemonize=/home/wwwroot/Warframe/uwsgi.log
启动UWSGI命令:
uwsgi --ini warframe_uwsgi.ini
6. 连接uwsgi与nginx
vim /usr/local/nginx/conf/vhost/warframe.conf
将以下的配置内容拷贝到warframe.conf,退出并保存:wq!
upstream django {
server 127.0.0.1:8001;
}
server {
listen 80;
server_name www.warframe.com;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /home/wwwroot/Warframe/media;
}
location /static {
alias /home/wwwroot/Warframe/static;
}
location / {
root /home/wwwroot/Warframe;
uwsgi_pass django;
include uwsgi_params;
}
}
chown -R www:www /usr/local/nginx/conf/vhost/warframe.conf
chmod -R 755 /usr/local/nginx/conf/vhost/warframe.conf
/etc/init.d/nginx restart
6. 网站和数据库分配用户组权限:
网站路径:/home/wwwroot/Warframe
chown -R www:www /home/wwwroot/Warframe
chmod -R 755 /home/wwwroot/Warframe
数据库路径:/home/wwwdata/mysql/var/warframe
chown -R mysql:mysql /home/wwwdata/mysql/var/warframe
chmod -R 755 /home/wwwdata/mysql/var/warframe
7.Django框架js、css样式丢失(xadmin):
1.配置Django的setting.py静态文件+上传文件路径:
# 设置静态文件的路径
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# 设置上传文件的路径
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
2.配置Django的urls.py静态文件+上传文件访问链接:
from django.conf.urls import include, url
from django.urls import path, re_path
from Warframe.settings import MEDIA_ROOT, STATIC_ROOT
from django.views.static import serve
import xadmin
urlpatterns = [
path('xadmin/', xadmin.site.urls),
# 处理图片显示的url,使用Django自带serve,传入参数告诉它去哪个路径找,我们有配置好的路径MEDIAROOT
re_path('media/(?P.)', serve, {"document_root": MEDIA_ROOT}),
# 处理静态文件(js,css)显示的url,使用Django自带serve,传入参数告诉它去哪个路径找,我们有配置好的路径STATIC_ROOT
re_path('static/(?P. )', serve, {"document_root": STATIC_ROOT}),
]
3.运行下面命令把相关文件copy到static静态目录:
python manage.py collectstatic