CentOS下部署Django项目

部署环境

CentOS7下使用nginx+uswgi部署Django(使用virtualevn隔离)项目

步骤:

  1. Python3和Virtualevn的安装
  2. Django项目创建
  3. 安装uswgi
  4. uswgi配置文件设置
  5. 安装nginx
  6. nginx配置文件设置
  7. Django项目部署调整

uSWGI

  • 指令
  • 方式:http socket

安装

pip3 install uwsgi

uwsgi的基本测试

# 创建一个test.py文件,内容如下:
def application(env, start_response):
   start_response('200 OK', [('Content-Type','text/html')])
   return "Hello World"


# 启动uwsgi:
uwsgi --http :9000 --wsgi-file test.py

# 通过浏览器访问http://127.0.0.1:9000,会输出"Hello World"

配置

编写uwsgi配置,在项目根目录下创建uwsgi.ini,内容如下:

[uwsgi]
# 使用nginx连接时使用
socket=127.0.0.1:8080
#http=:8080

#项目目录 制定项目路径
chdir=/home/cjp/DjangoProject/test

#wsgi.py文件,相对于项目路径
module=test.wsgi

# 制定进程数目
processes=4
# 制定线程数目
threads=2
# 主进程
master=True

# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true

# 指定python虚拟环境路径
virtualenv=/home/cjp/pyEnv/testpy

启动

uwsgi -i 你的目录/Mxonline/conf/uwsgi.ini
uwsgi --ini uwsgi.ini             # 启动
uwsgi --reload uwsgi.pid          # 重启
uwsgi --stop uwsgi.pid            # 关闭

Nginx

安装

  • Step One—Add Nginx Repository

To add the CentOS 7 EPEL repository, open terminal and use the following command:

sudo yum install epel-release
  • Step Two—Install Nginx

Now that the Nginx repository is installed on your server, install Nginx using the following yum command:

sudo yum install nginx

After you answer yes to the prompt, Nginx will finish installing on your virtual private server (VPS).

  • Step Three—Start Nginx

Nginx does not start on its own. To get Nginx running, type:

sudo systemctl start nginx

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

You can do a spot check right away to verify that everything went as planned by visiting your server's public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):

http://server_domain_name_or_IP/

配置文件逻辑

  • uwsgi_params
  • 指定用户: root权限

配置

编写nginx配置,在项目根目录下创建uc_nginx.ini,内容如下:

# the upstream component nginx needs to connect to 同uwsgi指定的端口连接
upstream django {
  server 127.0.0.1:8080; 
}
  
# configurssation of the server
server {
  listen      83;
  server_name 182.61.55.219; 
  charset     utf-8;

  client_max_body_size 75M; 

  location ~/static/ {
      root /home/cjp/DjangoProject/onlinetest/; # 指向django的static目录
  }

  location / {
      uwsgi_pass  django;
      include     /home/cjp/DjangoProject/onlinetest/uwsgi_params; # the uwsgi_params file you installed
  }
  
  #location /media  {
  #    alias 你的目录/Mxonline/media;  # 指向django的media目录
  #}
}

将该配置文件加入到nginx的启动配置文件中(或者复制过去)

sudo ln -s 你的目录/Mxonline/conf/nginx/uc_nginx.conf  /etc/nginx/conf.d/uc_nginx.conf

运行nginx

sudo /usr/sbin/nginx

systemclt start nginx.service
systemclt status nginx.service
systemclt stop nginx.service

查看服务是否启动

ps -aux|grep nginx*

Order

# 1.查看防火墙是否运行
firewall-cmd --state

# 2.关闭防火墙
systemctl stop firewalld.service

# 3.启动防火墙
systemctl start firewalld.service

# 4.禁止防火墙开机自启
systemctl disable firewalld.service
/etc/nginx/conf.d/
/etc/nginx/nginx.conf

你可能感兴趣的:(CentOS下部署Django项目)