Django开发环境搭建

Django开发环境搭建
1、远程登录服务器工具-ssh
mac:自带ssh
Windows:查看是否安装了ssh客户端:控制面板-》应用-》管理可选功能-》查找openssh
windows打开终端:win+r,输入powershell

2、设置用户和组
添加用户组:groupadd admin
添加用户:useradd bobo -g admin
设置密码:passwd bobo
修改用户权限:
cd /etc
vi sudoers
增加新行:bobo ALL=(ALL)

仅允许bobo账号远程登录:
cd /etc/ssh
vi sshd_config
添加行:PermitRootLogin no
重启ssh服务:systemctl restart sshd.service

3、设置登录证书

在本地机器创建证书:ssh-keygen -t rsa -C bobo -f bobofile(rsa:加密类型;bobo:描述;bobofile:存储的文件)
将公钥文件上传到服务器:scp bobofile.pub bobo@1.1.1.1:.

在服务器创建文件夹:
mkdir .ssh
cd .ssh
touch authorized_keys
cat bobofile.pub >> ~/.ssh/authorized_keys
chmod 700 .ssh

在本地机器.ssh文件夹下创建文件:
vi config
写入如下内容:
Host bobo
HostName 1.1.1.1
Port 22
User bobo
IdentityFile ~/.ssh/bobofile

设置好以后,在命令行输入如下命令即可登录,不再需要密码:
ssh bobo

4、设置防火墙和端口
更新yum: sudo yum -y update(以超级管理员的身份执行命令,回车后需要输入bobo的账户密码)
yum自动更新:sudo yum install -y cronie yum-cron
修改如下配置文件:
sudo vi /etc/yum/yum-cron.conf
apply_updates=yes
重启服务:
sudo systemctl start crond
sudo systemctl start yum-cron

安装防火墙:sudo yum install -y firewalld
启动防火墙:systemctl start firewalld.service
检查是否启动成功(查看防火墙打开的服务和端口):sudo firewall-cmd --list-all

修改ssh端口:
sudo vi /etc/ssh/sshd_config
修改如下:
Port 12345

修改防火墙中ssh的配置:
sudo cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/
sudo vim /etc/firewalld/services/ssh.xml
修改port为22

在浏览器中修改服务器,添加12345端口,删除443、22端口
重启ssh服务:sudo systemctl restart sshd.service
重启防火墙:sudo systemctl restart firewalld.service

在本地修改文件:
cd .ssh
vi config
修改port为12345

5、安装数据库
sudo yum install -y mariadb-server
sudo systemctl start mariadb

设置数据库开机自动启动:sudo systemctl enable mariadb.service

设置root账户的密码:mysql_secure_installation
登录数据库:mysql -uroot -p

6、安装python3
mkdir py3
cd py3
python依赖环境安装:sudo yum install openssl-devel
wget https://……

指定python安装路径:
cd /usr/local/
sudo mkdir python3
cd ~/py3/Python-3.7.3
./configure --prefix=/usr/local/python3

编译:make
安装:sudo make install
将python3添加到系统环境变量中:
sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3
sudo ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

用pip3安装插件:sudo pip3 install mysqlclient


7、安装测试Django
安装django: sudo pip3 install django
创建django项目:
sudo ln -s /usr/local/python3/bin/django-admin /usr/bin
cd /home
sudo mkdir www
cd www
django-admin startproject mysite
sudo yum -y install tree
tree

```
mysite
manage.py
mysite
__init__.py
settings.py
urls.py
wsgi.py
```


sudo vi view.py

```
from django.http import HttpResponse

def index(request):
return HttpResponse('Hello world!')
```

sudo vi urls.py
```
from django.contrib import admin
from django.urls import path

from . import views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
]
```

sodu vi settings.py
```
ALLOWED_HOSTS = ['localhost', '1.1.1.1']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
```


迁移数据库:sudo python3 manage.py migrate

启动服务:sudo python3 manage.py runserver 0.0.0.0:80


8、配置 Django 项目

在vscode中安装sftp插件
查看-》命令面板-》输入sftp-》创建sftp.json文件,文件内容如下
```
{
"name": "MySite"
"host": "",
"protocol": "sftp",
"port": "12345",
"username": "bobo",
"password": "",
"privateKeyPath": "~/.ssh/bobofile",
"remotePath": "/home/www/root",
"uploadOnSave": true,
"ignore": [
".vscode",
".git",
".DS_Store",
"__pycache__"
],
"syncOption": {
"delete": true
},
"watcher": {
"files": "**/*",
"autoUpload": true,
"autoDelete": true
}
}
```
password、privateKeyPath二选一


9、设置 Nginx 和 uWSGI

安装uwsgi: sudo pip3 install uwsgi
sudo -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3

安装nginx: sudo yum install -y nginx

修改settings.py
```
DEBUG: False
TEMPLATES = [
{
'DIRS': [
os.path.join(BASE_DIR, 'templates')
]
}
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '../collected_static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
```

链接静态文件:sudo python3 manage.py collectstatic

启动 uwsgi 需要 uwsgi.ini 文件,内容如下:
```
[uwsgi]
socket = 127.0.0.1:8000
chdir = /home/www/root
module = mysite.wsgi
master = true
processes = 1
threads = 2
max-requests = 2000
chmod-socket = 64
vacuum = true

static = %(chdir)/uwsgi/uwsgi.status
pidfile = %(chdir)/uwsgi/uwsgi.pid
daemonize = %(chdir)/uwsgi/uwsgi.log
```

服务器中配置nginx:
sudo vi /etc/nginx/nginx.conf
```
server {
listen 80;
server_name _;
charset utf-8;

location / {
uwsgi_pass 127.0.0.1:8000;
include /etc/nginx/uwsgi_params;
}

location /static {
alias /home/www/collected_static/;
}

error_page 404 /404.html;
location = /40x.html {

}

error_page 500 502 503 504 /50x.html;
location = /50x.html {

}
}
```

启动uwsgi: sudo uwsgi3 --ini uwsgi.ini
启动nginx: sudo systemctl start nginx

修改任何文件需要重启uwsgi:
cd /home/work/root/uwsgi
sudo uwsgi3 --reload uwsgi.pid

 

posted @ 2022-01-30 13:59  sunnyamy  阅读(241)  评论(0编辑  收藏  举报