EC2和RDS上的Django应用程序(服务器是否在主机“ localhost”(127.0.0.1)上运行并在端口5432上接受TCP / IP连接?)

时间:2019-06-10 02:32:35

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

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

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

  

/

处的OperationalError      

无法连接到服务器:连接被拒绝服务器正在运行   在主机“ 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安全组:

enter image description here

enter image description here

RDS安全组:

enter image description here

enter image description here

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'

2 个答案:

答案 0 :(得分:0)

答案 我正面临着同样的问题。我重新启动EC2 Machine,它对我有用。 试试吧,也许对您有用。

答案 1 :(得分:0)

  1. 进入安全组
  2. 转到入站
  3. 点击修改并添加规则
  4. 类型:postgresql,源:任何地方
  5. 保存,它对我有用。