在阿里云ECS上部署 Django+MySQL+uWSGI+Nginx 项目的基本流程
程序员文章站
2022-07-15 11:37:59
...
django==1.10.6
python==3.5.2
Mysql==5.6
uWSGI==2.0.15
Nginx==我也不知道是啥版本的
阿里云ECS服务器好像是自带 SSH 的,否则的话要安装 SSH 才能用 Xshell 进行连接:
apt-get install ssh
使用 xshell 连接 阿里云服务器:
使用 xftp 向 服务器传送文件:
在使用 xshell 成功连接 服务器后,更新一下系统软件:
sudo apt-get update
sudo apt-get upgrade
接下来安装 MySQL-Server:
Ubuntu16.04 下安装 MySQL 及配置
配置好 MySQL ,使其能够被远程连接:
在windows10环境下远程连接阿里云Ubunutu下MySQL的方法
配置 python 虚拟环境
在Ubuntu16.04下创建只有python3的虚拟环境
在**虚拟环境的状态下 ,使用 pip 安装 requirements.txt
使用 pip 安装 uswgi:
测试 uwsgi :
(mxonline)~/Documents/MxOnline# uwsgi --http :8000 --module MxOnline.wsgi
在Django 项目的目录下创建 一个 名为 conf 的目录,和一个 uc_nginx.conf 的文件:
(mxonline) ~/Documents/MxOnline# mkdir conf
(mxonline) ~/Documents/MxOnline# vim uc_nginx.conf
uc_nginx.conf 文件的内容如下:
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 你的ip地址 ; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias 你的目录/Mxonline/media; # 指向django的media目录
}
location /static {
alias 你的目录/Mxonline/static; # 指向django的static目录
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
}
将该配置文件加入到nginx
的启动配置文件中:
(mxonline) ~/Documents/MxOnline# cp uc_nginx.conf /etc/nginx/conf.d/
重启 nginx 服务,再 查看 nginx 的状态:
(mxonline) ~/Documents/MxOnline# service nginx restart
(mxonline)~/Documents/MxOnline# ps aux|grep nginx
root 30455 0.0 0.0 117060 1444 ? Ss 16:56 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 30456 0.0 0.1 117420 3116 ? S 16:56 0:00 nginx: worker process
root 30458 0.0 0.0 14224 936 pts/1 S+ 16:56 0:00 grep --color=auto nginx
编辑 Django 项目下的 settings.py
:
(mxonline) ~/Documents/MxOnline# vim MxOnline/settings.py
在文件的最后加上这行:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
拷贝静态文件:
python manage.py collectstatic
新建 uwsgi 配置文件:
(mxonline) :~/Documents/MxOnline/conf# vim uwsgi.ini
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/bobby/Projects/MxOnline
# Django's wsgi file
module = MxOnline.wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
virtualenv = /home/bobby/.virtualenvs/mxonline
使用 uwsgi 命令启动项目:
(mxonline) :~/Documents/MxOnline/conf# uwsgi -i uwsgi.ini
修改settings.py
下的 DEBUG=False
:
(mxonline) aaa@qq.com:~/Documents/MxOnline# vim MxOnline/settings.py
修改 nginx 配置文件:
vim /etc/nginx/nginx.conf
将第一行改为:
user root
重启 nginx:
pkill -f nginx
再用启动 nginx:
nginx
查看 uwsgi 状态:
ps aux|grep uwsgi
重启 uwsgi:
pkill -f uwsgi
查看 uwsgi 状态并重启:
ps aux|grep uwsgi &