部署Django项目Nginx+uwsgi

部署方式:Nginx代理+uwsgi应用服务

操作系统:CentOS7

Django项目:https://github.com/zelinhehe/mysite.git为例:

 

1. 安装 Python3.6

 

获取安装包

wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz

tar -xzvf Python-3.6.2.tgz -C /tmp

cd /tmp/Python-3.6.2/

 

安装到 /usr/local 目录

./configure --prefix=/usr/local

make

make altinstall

更改/usr/bin/python链接

ln -s /usr/local/bin/python3.6 /usr/bin/python3

 

2. MariaDB

安装 sudo yum install mariadb-server

启动 sudo systemctl start mariadb

设置bind-ip

vim /etc/my.cnf 在 [mysqld]下添加

bind-address = 0.0.0.0

 

设置访问权限
mysql命令直接进入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;

 

4. 安装virtualenvwrapper

yum install python-setuptools python-devel

pip install virtualenvwrapper

在.bashrc文件中,添加

export WORKON_HOME=$HOME/.virtualenvs

source /usr/local/bin/virtualenvwrapper.sh(此路径可通过 which virtualenvwrapper.sh查看)

 

重新加载.bashrc文件

source ~/.bashrc

 

新建虚拟环境 mkvirtualenv mysite-env

进入虚拟环境 workon mysite-env

 

安装Django项目依赖

在开发环境通过 pip freeze > requirements.txt 将mysite的虚拟环境安装包导出来

在部署服务器的虚拟环境中运行:pip install -r requirements.txt

 

5. uwsgi

安装

pip install uwsgi

测试uwsgi

uwsgi --http :8000 --module mysite.wsgi

 

6. Nginx

安装

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7

 

配置

新建mysite_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 /root/mysite/media;  # 指向django的media目录
}

location /static {
    alias /root/mysite/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的启动配置文件中

sudo ln -s /root/mysite/mysite_nginx.conf /etc/nginx/conf.d/

 

7. 拉取所有需要的static file 到同一个目录

在django的setting文件中,添加下面一行内容:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

运行命令 python manage.py collectstatic

 

8. 重启Nginx

如果systemctl restart nginx 有权限问题,则直接用nginx命令启动 sudo /usr/sbin/nginx

 

9. 配置并启动uwsgi

新建uwsgi.ini 配置文件, 内容如下:
 

# mysite_uwsgi.ini file

[uwsgi]

# Django-related settings

# the base directory (full path)

chdir = /root/mysite

# Django's wsgi file

module = mysite.wsgi

# the virtualenv (full path)

virtualenv = /root/.virtualenvs/mysite-env



# 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



logto = /tmp/mylog.log

注:

chdir: 表示需要操作的目录,也就是项目的目录

module: wsgi文件的路径

processes: 进程数

virtualenv:虚拟环境的目录

 

运行uwsgi uwsgi -i /root/mysite/uwsgi.ini &

 

10.访问

http://{IP}/polls/

 

Q:

安装mysqlclient出问题

centos 7: yum install python-devel mariadb-devel -y

ubuntu: sudo apt-get install libmysqlclient-dev

然后: pip install mysqlclient

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux系统上部署Django + nginx + uWSGI的步骤如下: 1. 安装必要的软件 在Linux系统上安装必要的软件包,包括Python、pip、nginxuWSGI等。 2. 创建Django项目 使用Django创建一个新项目或使用现有的Django项目。 3. 配置uWSGIDjango项目的根目录下创建一个uwsgi.ini文件,用于配置uWSGI。示例配置如下: ``` [uwsgi] # 指定运行模式为WSGI http = :8000 # 指定Django应用的wsgi模块 wsgi-file = myproject.wsgi # 指定进程数 processes = 4 # 指定线程数 threads = 2 # 指定静态文件路径 static-map = /static=/path/to/static # 指定日志路径 logto = /path/to/logfile ``` 其中,http参数指定了监听的端口号,wsgi-file参数指定了Django应用的wsgi模块,processes参数指定了进程数,threads参数指定了线程数,static-map参数指定了静态文件的路径,logto参数指定了日志文件的路径。 4. 配置nginxnginx的配置文件中添加以下内容: ``` server { listen 80; server_name example.com; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { uwsgi_pass 127.0.0.1:8000; include uwsgi_params; } location /static { alias /path/to/static; } } ``` 其中,server_name指定了域名,access_log和error_log指定了日志文件的路径,uwsgi_pass指定了uWSGI的地址和端口号,include指定了uWSGI的参数。 5. 启动uWSGI服务 使用以下命令启动uWSGI服务: ``` uwsgi --ini uwsgi.ini ``` 6. 启动nginx服务 使用以下命令启动nginx服务: ``` sudo service nginx start ``` 这样就完成了Django + nginx + uWSGI部署。可以通过访问该网站的域名来验证是否部署成功。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值