【问题标题】:Django application on EC2 and RDS ( Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?)EC2 和 RDS 上的 Django 应用程序(服务器是否在主机“localhost”(127.0.0.1)上运行并接受端口 5432 上的 TCP/IP 连接?)
【发布时间】:2019-06-10 02:32:35
【问题描述】:

我在 AWS 上创建了一个 EC2 实例并在其上部署了我的 django 应用程序。

还在 AWS 上创建了 RDS postgres 数据库。每当我访问我的公共 ip 时,我都会收到以下错误,

OperationalError at /

无法连接到服务器:连接被拒绝服务器是否正在运行 在主机“localhost”(127.0.0.1)上并接受 TCP/IP 连接 5432端口?

EC2 和 RDS 实例均已连接。已通过以下验证,

  • 在我的本地 PC 上使用 pgadmin4 连接 RDS postgres。
  • 在 ec2 控制台上创建了超级用户并填充了 postgres。

Nginx 配置:

server {
  listen 80;
  server_name 18.218.45.241;
  location = /favicon.ico { access_log off; log_not_found off; }
  location /static/ {
      root /home/ubuntu/Crowdsocial_project;
  }
  location / {
      include proxy_params;
      proxy_pass http://unix:/home/ubuntu/Crowdsocial_project/crowdsocial.sock;
  }
}

Gunicorn 配置:

[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/Crowdsocial_project
ExecStart=/home/ubuntu/Crowdsocial_project/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/Crowdsocial_project/crowdsocial.sock main.wsgi:application
[Install]
WantedBy=multi-user.target

EC2 安全组:

RDS 安全组:

Settings.py

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secret'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['18.218.45.241']

DATE_INPUT_FORMATS= ['%Y-%m-%d',]

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'example@gmail.com'
EMAIL_HOST_PASSWORD = 'pass'
EMAIL_PORT = 587

# Application definition

INSTALLED_APPS = [
    'users.apps.UsersConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap_modal_forms',

    'main',
    'first_app',
    'campaign',
    'invoice',
    'taggit',
    'corsheaders',
    'taggit_selectize',
    'rest_framework',
    'django_filters',
    'django_extensions',
    'shop',
    'search',
    'cart',
    'orders',
    'widget_tweaks',
    'billing',
]

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
]

AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']

CORS_ORIGIN_ALLOW_ALL = True

ROOT_URLCONF = 'main.urls'

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

WSGI_APPLICATION = 'main.wsgi.application'
AUTH_USER_MODEL = 'users.CustomUser'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'name',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': 'crowdsocial-postgres.c9sefqws77mc.us-east-2.rds.amazonaws.com',
        'PORT': '5432',
    }
}


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

LOGIN_URL = 'users:login'
# LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'first_app:home'

【问题讨论】:

  • 正如错误告诉您的那样,它正在寻找在本地系统中打开的端口 5432(正在运行 Web 服务器的 18.218.45.241),而不是连接到 RDS 实例 crowdsocial-postgres.c9sefqws77mc 的服务器 18.218.45.241。 us-east-2.rds.amazonaws.com.
  • 我通过编辑 postgresql.conf 并设置端口 5432 以接受来自任何地方的连接来允许本地计算机上的远程连接,但它仍然给我错误。

标签: django postgresql amazon-web-services amazon-ec2


【解决方案1】:

回答 我面临同样的问题。我重新启动 EC2 机器,它对我有用。 试试吧,也许它对你有用。

【讨论】:

    【解决方案2】:
    1. 进入安全组
    2. 去入站
    3. 点击编辑并添加规则
    4. 类型:postgresql,来源:任何地方
    5. 保存,它对我有用。

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2017-06-09
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多