![](https://img.kancloud.cn/41/e0/41e066af9a6c25a24868d9667253ec98_1241x333.jpg)
*****
## 阿里云安全组规则
端口范围
80/80
3306/3306
6379/6379
23/23
443/433
22/22
80/80
3389/3389
### nginx + uwsgi
![](https://img.kancloud.cn/1a/9a/1a9ae346ac7d634b50ef94bdb4aee583_1022x304.png)
Nginx默认是80端口
### 1.安装Python3.7
1.安装依赖包
```
yum install opensll-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ opensll-devel libffi-devel python-devel mariadb-devel
```
2.下载Python源码
```
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
tar -xzvf Python-3.7.3.tgz -C /tmp
cd /tmp/Python-3.7.3
```
3.把Python3.7安装到 /usr/local目录
```
./configure --prefix=/usr/local
make
make altinstall # 这一步比较耗时
```
4.更改/usr/bin/python链接
```
ln -s /usr/local/bin/python3.7 /usr/bin/python3
ln -s /usr/local/bin/pip3.7 /usr/bin/pip3
```
### 2.maridb和redis
mariadb跟MySQL是一样的,centos中已经集成了,安装非常简单
1.安装
```
sudo yum install mariadb-server
```
2.启动,重启
```
sudo systemctl start mariadb
sudo systemctl restart mariadb
```
设置安全规则,配置MySQL端口
```
访问MySQL mysql -uroot
```
3.设置bind-ip
```
vim /etc/my.cnf
在[mysqld]: 下面加一行
bind-address = 0.0.0.0
```
4.设置外部ip可以访问
```
先进入MySQL才能运行下面命令
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
```
5.设置阿里云的对外端口
![](https://img.kancloud.cn/47/f1/47f1a20f6c414a25580368bbee82c224_1386x459.png)
![](https://img.kancloud.cn/ed/0b/ed0b1370a125fe21e0caade2f99f17d5_1083x693.png)
6.安装mysqlcilent出问题
```
centos 7: yum install python-devel mariadb-devel -y
pip install mysqlclient
```
将本地的数据库文件传到服务器上
6.安装redis
```
yum install redis
service redis start
```
### 3.安装Nginx
```
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
```
### 4.安装virtualenvwrapper
```
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
yum install python-setuptools python-devel
pip install virtualenvwrapper
```
编辑.bashrc文件
```
vim ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
```
重新加载.bashrc文件
```
source ~/.bashrc
```
如果报错,就查找一下文件,将查找到的文件路径复制到source中
```
sudo find / -name virtualenvwrapper.sh
```
新建虚拟环境
```
mkvirtualenv logic_online
mkvirtualenv -p python3 logic_online # 用Python3新建虚拟环境
```
进入虚拟环境
```
workon logic_online
```
退出虚拟环境
```
deactivate
```
如果安装虚拟环境报错,先更新pip
```
pip3 install -i https://pypi.douban.com/simple --upgrade pip
```
安装pip包
```
将requireements.txt文件上传到服务器之后运行
pip install -r requireements.txt
安装依赖包
```
### 4.安装uwsgi
```
pip install uwsgi
```
### 6.测试uwsgi
```
uwsgi --http :8000 --module logic_online.wsgi
```
### 7.配置Nginx
```
新建uc_nginx.cong
# 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:8001; # 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 47.106.209.215; # 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 /root/logic_online/media; # 指向django的media目录
}
location /static {
alias /root/logic_online/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
}
}
```
### 8.将改配置文件加入到Nginx的启动配置文件中
```
sudo ln -s 你的目录/logic_online/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/
```
### 9.拉取所有需要的static file到同一目录
```
在django的setting文件中,添加下面一行内容
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
运行命令
python manage.py collectstatic
```
### 10.运行Nginx
```
sudo /usr/sbin/nginx
```
这里注意一定是直接用Nginx命令启动,不要用systemctl启动Nginx不然会有权限问题
### 11.通过配置文件启动uwsgi
新建uwsgi.ini配置文件
```
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/logic_online
# Django's wsgi file
module = logic_online.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:8001
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
virtualenv = /root/.virtualenvs/logic_online
#logto = /tmp/mylog.log
注:
chdir:表示需要操作的目录,也就是项目的目录
module:wsgi文件的路径
processes:进程数
virtualenv:虚拟环境的目录
workon logic_online
uwsgi -i 你的目录/logic_online/conf/uwsgi.ini &
```
### 12.访问
```
http://你的IP地址/
```
- 空白目录
- 1-Django前导知识
- 1-1-虚拟环境
- 1-2-Django框架介绍与环境搭建
- 2-URL与视图
- 2-1-URL与视图
- 3-模板
- 3-1-模板介绍
- 3-2-模板变量
- 3-3-常用标签
- 3-4-模板常用过滤器
- 3-5-模板结构优化
- 3-6-加载静态文件
- 4-数据库
- 4-1-操作数据库
- 4-2-图书管理系统
- 4-3-ORM模型介绍
- 4-4-ORM模型的增删改查
- 4-5-模型常用属性
- 4-6-外键和表
- 4-7-查询操作
- 4-8-QuerySet的方法
- 4-9-ORM模型练习
- 4-10-ORM模型迁移
- 5-视图高级
- 1-Django限制请求method
- 2-页面重定向
- 3-HttpRequest对象
- 4-HttpResponse对象
- 5-类视图
- 6-错误处理
- 6-表单
- 1-用表单验证数据
- 2-ModelForm
- 3-文件上传
- 7-session和cookie
- 1-session和cookie
- 8-memcached
- 1-memcached
- 9-阿里云部署
- 阿里云部署