欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Django项目部署

程序员文章站 2022-06-11 10:45:50
...

注意事项:

  1. 要确保项目的settings.py文件中的数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': ' netshop',
        'HOST':'192.168.93.128',
        'PORT':3306,
        'USER':'root',
        'PASSWORD':'123456',
        'OPTIONS':{
                    'init_command':"SET sql_mode='STRICT_TRANS_TABLES'",
                    'charset':'utf8'
                }
    }
}

ALLOWED_HOSTS = ['*']

  1. 在项目settings.py文件中添加
#将所有的静态文件收集到指定的目录下
STATIC_ROOT = 'd://static'
  1. 在终端中运行以下命令(将static文件收集到d://static目录下)
python manage.py collectstatic

  1. 在centos6中安装redis

  2. 在centos6中安装mysql

单进程运行

  1. 实现步骤:

    1. 通过xftp将本机的项目源码拷贝到远程服务器相应目录下

    2. 通过xshell连接远程服务器
      安装虚拟环境模块

     pip install virtualenv
    
    1. 通过命令创建商城项目运行的虚拟环境
    virtualenv netshop-env --python=python2.7
    
    1. **环境(切换到虚拟环境中的bin目录下)
    source activate
    
    1. 在虚拟环境中安装项目所需的所有类库
      (可以对比本机上的settings进行安装)
    pip install django 
    
    1. 进入到商城项目目录下启动服务
    python manage.py runserver 0.0.0.0:9000
    

多进程运行

目的:supervisor为一个项目管理多个进程

  1. 实现步骤

    1. 安装supervisor模块
    pip install supervisor
    
    1. 通过命令产生默认配置文件
    echo_supervisord_conf > supervisord.conf 
    
    1. 修改supervisord.conf配置文件
            ;[include]
    		;files = relative/directory/*.ini	
    		修改为
    		[include]
    		files = /etc/supervisor/*.conf
    		
    
    1. 将当前配置文件拷贝到/etc/目录下
    cp supervisord.conf /etc/
    
    1. 在/etc/目录下创建/supervisor/django.conf文件
      django.conf的内容为:
[group:djangos]
programs=django-8000,django-8001,django-8002,django-8003

[program:django-8000]
command=/home/laozhang/codes/netshop/bin/python2.7 /home/laozhang/codes/netshop/manage.py runserver 0.0.0.0:8000
directory=/home/laozhang/codes/netshop
user=laozhang
autorestart=true
redirect_stderr=true
stdout_logfile=/home/laozhang/codes/netshop/django.log
loglevel=info

[program:django-8001]
command=/home/laozhang/codes/netshop/bin/python2.7 /home/laozhang/codes/netshop/manage.py runserver 0.0.0.0:8001
directory=/home/laozhang/codes/netshop
user=laozhang
autorestart=true
redirect_stderr=true
stdout_logfile=/home/laozhang/codes/netstore/django.log
loglevel=info


[program:django-8002]
command=/home/laozhang/codes/netshop/bin/python2.7 /home/laozhang/codes/netstore/manage.py runserver 0.0.0.0:8002
directory=/home/laozhang/codes/netstore
user=laozhang
autorestart=true
redirect_stderr=true
stdout_logfile=/home/laozhang/codes/netstore/django.log
loglevel=info


[program:django-8003]
command=/home/laozhang/codes/netshop/bin/python2.7 /home/laozhang/codes/netstore/manage.py runserver 0.0.0.0:8003
directory=/home/laozhang/codes/netstore
user=laozhang
autorestart=true
redirect_stderr=true
stdout_logfile=/home/laozhang/codes/netshop/django.log
loglevel=info


  1. 启动/查看进程
            #启动进程
            supervisord -c /etc/supervisord.conf
            
            #查看进程
			ps aux | grep supervisord #查看进程是否存在
			
			#杀死进程
			kill 杀死进程pid
  1. 客户端使用
            supervisorctl  #客户端命令

			status    # 查看程序状态
			stop djangos:*   # 关闭 djangos组 程序
		    start djangos:*  # 启动 djangos组 程序
			restart djangos:*    # 重启 djangos组 程序
			update    # 重启配置文件修改过的程序
  1. 在主机访问不同端口下的项目即可

ngnix多进程

  1. nginx的安装http://www.runoob.com/linux/nginx-install-setup.html

  2. 编辑/usr/local/webserver/nginx/conf/nginx.conf文件

user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
  
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  #tcp_nopush on;
  keepalive_timeout 60;
  #gzip on;

  upstream djangos{
  server 0.0.0.0:8000;
  server 0.0.0.0:8001;
  server 0.0.0.0:8002;
  server 0.0.0.0:8003;
	
  }


 
 
  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虚拟主机的配置
 server
  {
    listen 80;#监听端口
    server_name localhost;#域名
    
    location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_pass http://djangos;
    }
    location /static {
    autoindex on;
    alias /var/www/static/;
    }
    location /media {
    autoindex on;
    alias /var/www/media/;
    }
	

   # access_log off;

     error_page 500 502 503 504 /50x.html;
     location = /50x.html{
         root html;
     }



    }

}

  1. 通过xftp将项目中的静态文件拷贝到www用户下

4.启动nginx服务访问页面内容

5.确定redis服务是否启动

/usr/local/redis/bin/各种命令
/usr/local/redis/etc/redis.conf配置文件

#启动redis服务
redis-server etc/redis.conf

supervisor:

https://blog.csdn.net/jerk_zhang/article/details/26349919

相关标签: Django