如何在托管服务上部署 Django 应用程序

How to deploy django app on hosting service

我是网络开发的新手。我从 Siteground 购买了网络托管启动包来托管我的平面设计作品集网站。这是一个简单的 Django 应用程序,它是一个单页投资组合,带有一个使用 Django 的 sendEmail through gmail 服务的联系表。

我从未部署过 Django 应用程序,更不用说我自己的网站了。我只制作并部署了一个不言自明的 Wordpress 网站。

我知道如何通过 FTP 将我的网站上传到服务器,但我不知道如何配置 Django 应用程序进行部署(除了他们文档中的简要说明)或如何连接应用程序到我的服务器到域上的 运行。

我想我应该在 wsgi 中配置一些东西,但我真的不明白它是如何工作的,而且我在任何地方都找不到关于在 Siteground(或类似的东西)上部署 Django 应用程序的支持,尽管他们有 python 支持。

我连接到 Sitegrounds 的 SSH 以遵循一些关于 Django 部署的教程,但即使它是 Linux 我也不能使用(或不明白如何)sudo apt-get 来遵循教程.

这是我的 settings.py:

import os

with open('cfsite/key.txt') as f:
    SECRET_KEY = f.read().strip()

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Debug Settings
DEBUG = True

ALLOWED_HOSTS = ['']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
    'cfapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'cfsite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'cfsite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = '/home/iamcrys8/public_html/static'

# Crispy Forms settings

CRISPY_TEMPLATE_PACK = 'bootstrap4'


# Email Backend settings

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_SSL = True
EMAIL_PORT = 465
EMAIL_HOST_USER = '(email go here)'
EMAIL_HOST_PASSWORD = '(email pass go here)'

我知道我必须将 'Debug=True' 更改为 'Debug=False' 并将我域的 IP 插入 'Allowed_Hosts' 并将我的目录指向服务器目录 (public_html),但这就是我所知道的。

任何人都可以为我指明正确的方向以了解如何在 Siteground 上部署 python 应用程序吗?或者至少给我指出一个资源,该资源解释了 python 应用程序在服务器托管服务上的部署?

是否可以保留 sqlite 还是我必须学习 mysql(Siteground 支持)?

编辑:这是 Sitegrounds 关于非 Wordpress 或 Joomla 网站的部署信息(他们几乎没有关于这些网站的任何信息)。

  1. Move your files: First, you will need to move your website files from your previous host to SiteGround by using FTP (File Transfer Protocol).
  2. Move your database: Then, you will have to move your database. Start with exporting the database from your previous hosting account. If you’re not sure how to do this, contact the support of your previous host to get more info. When you get your data exported, create a new database on your new SiteGround hosting account and import your content into it.
  3. Reconfigure your application: The last step of transferring your site is to reconfigure your application to work from the new place. In WordPress, that's the wp-config.php file, while in Joomla it is configuration.php. Replace the values with your actual database, database username and password for it. Save this file and your site should be up and running from your new SiteGround hosting account. The configuration steps for every application are different.

关于部署的唯一其他信息特定于 Wordpress 或其他 PHP 框架。

Can anyone please point me in the right direction to understanding how to deploy python apps on Siteground? Or at least point me to a resource that explains the deployment of python apps on server hosting services?

几周前我刚刚在 CentOS 上部署了一个 Django 应用程序到生产环境。 SSH 告诉我您将使用某种虚拟机,因此这些教程应该非常适合您:

https://www.vultr.com/docs/how-to-install-django-on-centos-7

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7

我用过这些。我们使用与 DigitalOcean 相同的堆栈。

Is it possible to keep sqlite or do I have to learn mysql (which Siteground has support for)?

您可以继续使用SQLite。我不确定它需要什么额外的配置。