本篇博客是腾讯云服务器(阿里云服务器也可以)的搭建流程,所需的软件为CentOS 7.5、Python 3.7.8、Django 3.1、Nginx、uWSGI,如果使用Django开发网站的话,这是一个不错的选择,通过本教程,你可以将Django开发的网站部署到服务器上。
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel gcc automake autoconf libtool make wget
cd /usr/local
wget https://www.python.org/ftp/python/3.7.8/Python-3.7.8.tgz
tar -zxvf Python-3.7.8.tgz
cd Python-3.7.8
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
python3 -V
pip3 -V
pip3 install --upgrade pip
pip3 install virtualenv
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
mkdir -p /data/env
mkdir -p /data/wwwroot
cd /data/env/
virtualenv --python=/usr/bin/python3 pyweb
cd /data/env/pyweb/bin
source activate
deactivate
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
systemctl start mysqld.service
systemctl status mysqld.service
grep "password" /var/log/mysqld.log
mysql -u root –p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
pip3 install django
pip3 install uwsgi
pip3 install pymysql
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
# 查看防火墙是否开启
systemctl status firewalld
# 开启防火墙
systemctl start firewalld
需要在服务器管理界面配置一下安全组,加入8001端口
firewall-cmd --zone=public --add-port=8001/tcp --permanent
firewall-cmd --reload
netstat -tunlp
netstat -lnp| grep 8001
cd /data/wwwroot
mkdir ChatRobot
cd ChatRobot
vim test_uwsgi.py
# 文件内容如下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
# 内容写入之后 ESC 退出输入模式,切换到命令模式
# :w 保存 :q 退出
uwsgi --http :8001 --wsgi-file /data/wwwroot/ChatRobot/test_uwsgi.py
uwsgi --chdir /data/wwwroot/ChatRobot --home /data/env/pyweb --http :8001 --module=ChatRobot.wsgi:application
此时在浏览器输入ip,不出意外的话网站已经加载进来,只是缺少了静态文件。
在安装之前,先说一下网站的目录,下面我们要添加几行代码:
# 在urls.py文件中添加如下代码
from django.urls import path
from tencent import views
urlpatterns = [
path('wx', views.TencentView, name='TencentView')
]
# 在views.py文件中添加如下代码
from django.http import HttpResponse
def TencentView(request):
return HttpResponse(content="haha this is a tencent")
yum install epel-release –y
yum -y install nginx
systemctl start nginx.service
或
service nginx start
find / -name nginx.conf
#user nobody;
worker_processes 2;
error_log /data/wwwroot/ChatRobot/error.log;
error_log /data/wwwroot/ChatRobot/error.log notice;
error_log /data/wwwroot/ChatRobot/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log /data/wwwroot/ChatRobot/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzip on;
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location /static {
alias /data/wwwroot/ChatRobot/static;
proxy_connect_timeout 18000;
proxy_send_timeout 18000;
proxy_read_timeout 18000;
}
location /media {
alias /data/wwwroot/ChatRobot/media;
}
location / {
uwsgi_pass 127.0.0.1:8001;
include /etc/nginx/uwsgi_params;
}
}
nginx -t
[uwsgi]
chdir = /data/wwwroot/ChatRobot
home = /data/env/pyweb
module = ChatRobot.wsgi:application
master = true
processes = 4
harakiri = 600
max-requests = 5000
socket = 127.0.0.1:8001
uid = 1000
gid = 2000
pidfile = uwsgi.pid
daemonize = uwsgi.log
vacuum = true
uwsgi --ini /data/wwwroot/ChatRobot/chatrobot_uwsgi.ini
service nginx start
ps -aux | grep uwsgi
ps -aux | grep nginx
pkill -9 uwsgi
pkill -9 nginx
django-admin startproject ChatRobot(工程名)
tencent
的数据库 net start MySQL80(我的本地MySQL是8.0版本的)
mysql -uroot -p
create database tencent;
__init__.py
中添加代码,以兼容MySQL8.0版本 import pymysql
pymysql.install_as_MySQLdb()
settings.py
文件中配置使用MySQL DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tencent',
'USER': '用户名',
'PASSWORD': '密码',
'HOST': '数据库服务器IP,本地是localhost',
'PORT': '端口名,默认是3306',
}
}
python manage.py startapp tencent(应用名)
settings.py
文件中,将'tencent'
加入到INSTALLED_APPS
中 # ip可以不写,即默认本机ip
# 端口号可以不写,默认为8000,可以修改
python manage.py runserver ip:port