第一步安装Docker
PS:主机如果已经安装Docker,《第一步安装Docker》可跳过。
1.更新Ubuntu的apt源
sudo apt-get update
2.安装包允许apt通过HTTPS使用仓库
sudo dpkg --configure -a
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
3.添加Docker官方GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4.设置Docker稳定版仓库
sudo add-apt-repository "deb [arch=arm64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- [arch=arm64] 注意系统的平台选择
5.更新apt源
sudo apt-get update
6.安装最新版本Docekr CE(社区版)
sudo apt-get install docker-ce
7.查看安装Docker的版本
docker --version
8.检查Docker CE是否安装正确
sudo docker run hello-world
9.更新为国内源
sudo vim /etc/docker/daemon.json
写入以下文本
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"https://hub-mirror.c.163.com/"
]
}
10.重启docker
sudo systemctl restart docker
第二步新建容器web-v1,镜像centos8.4.2105
1.新建docker容器web-v1
sudo docker run -d -it -p 80:80 -p 8000:8000 --name web-v1 centos:8.4.2105
2.进入docker容器
sudo docker exec -it web-v1 bash
3.安装 yum-utils 工具
yum install yum-utils
出现错误:
执行以下操作
cd /etc/yum.repos.d/
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
- 重新安装
yum install yum-utils
第二步安装 Nginx
vi /etc/yum.repos.d/nginx.repo
写入文本
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
安装Nginx
yum install nginx
启动Nginx
nginx
第三步安装Python虚拟环境
1.安装依赖
yum install -y gcc automake autoconf libtool make libffi-devel
yum install -y gcc gcc-c++
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel xz-devel
yum install -y patch
yum install -y git wget unzip
2.安装pyenv
mkdir ~/.pyenv
//git下载
git clone git://github.com/pyenv/pyenv.git ~/.pyenv
//本地下载
wget -P ~/.pyenv http://192.168.157.1/Share/pyenv-master.zip
unzip ~/.pyenv/pyenv-master.zip -d ~/.pyenv
mv ~/.pyenv/pyenv-master/* ~/.pyenv/
cd ~/.pyenv && src/configure && make -C src
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
exec $SHELL -l
3.安装pyenv virtualenv
//git下载
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
//本地下载
wget -P $(pyenv root)/plugins/pyenv-virtualenv http://192.168.157.1/Share/pyenv-virtualenv-master.zip
unzip ~/.pyenv/plugins/pyenv-virtualenv/pyenv-virtualenv-master.zip -d ~/.pyenv/plugins/pyenv-virtualenv
mv ~/.pyenv/plugins/pyenv-virtualenv/pyenv-virtualenv-master/* ~/.pyenv/plugins/pyenv-virtualenv/
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
exec "$SHELL"
4.创建虚拟环境
// 下载并安装python3.11.3
v=3.11.3;wget https://npm.taobao.org/mirrors/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v
// 查看pyenv版本
pyenv versions
//新建test环境
pyenv virtualenv 3.11.3 test
pyenv activate test
pyenv deactivate test
5.虚拟环境test中安装Django
安装Django
pyenv activate test
pip install django
python -m django --version
/root/.pyenv/versions/test/bin/pip list
第四步Django创建项目
创建项目
cd /usr/share/nginx/html
django-admin startproject mysite
在setting.py里面需要添加ALLOWED_HOSTS=["*"]
vi /usr/share/nginx/html/mysite/mysite/settings.py
启动项目
python manage.py runserver 0.0.0.0:8000
报错
提示执行
python manage.py migrate
重启系统,访问http://192.168.157.129:8000/
python manage.py runserver 0.0.0.0:8000
第五步安装uwsgi
pyenv activate test
pip install uwsgi
//使用douban源安装
pip install uwsgi -i https://pypi.douban.com/simple/
配置uwsgi.ini文件
cd /usr/share/nginx/html/mysite/mysite
vi /usr/share/nginx/html/mysite/mysite/uwsgi.ini
#添加配置选择
[uwsgi]
#配置和nginx连接的socket连接
socket = 127.0.0.1:8000
#配置项目路径,项目的所在目录
chdir = /usr/share/nginx/html/mysite
#配置wsgi接口模块文件路径
wsgi-file = mysite/wsgi.py
#配置启动的进程数
#配置启动管理主进程
master = True
#配置存放主进程的进程号文件
pidfile =/usr/share/nginx/html/mysite/uwsgi.pid
#配置dump日志记录
daemonize = /var/log/mysite_uwsgi.log
# 指定python 环境
home = /root/.pyenv/versions/test/
post-buffering = 65536
max-requests = 10000
vacuum = true
#自动更新
#touch-reload = /usr/share/nginx/html/mysite/.git/index
后台启动uwsgi
/root/.pyenv/versions/test/bin/uwsgi -d -i /usr/share/nginx/html/mysite/mysite/uwsgi.ini
配置Nginx
cd /etc/nginx/conf.d/
vi /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name 192.168.157.129;
location / {
#add_header 'Access-Control-Allow-Origin' '*';
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_param UWSGI_PYHOME /root/.pyenv/versions/test/bin/python; # 指向虚拟环境目录
uwsgi_param UWSGI_MAX_TEMP_FILE_SIZE 1000m;
uwsgi_param UWSGI_MAX_TEMP_WRITE_SIZE 1000m;
uwsgi_param UWSGI_SCRIPT app:app; # 指定启动程序
}
location /static/ {
root /usr/share/nginx/html/mysite/mysite;
break;
}
}
重启Nginx
nginx -s reload