繁体   English   中英

如何修复部署在AWS EC2中的Django项目,但CSS和照片无法正常工作

[英]How to fix Django project deployed in AWS EC2 but CSS and photos not working

我在localhost开发了我的项目,一切正常。 然后我将项目推送到github,最后在我的AWS EC2 Ubuntu服务器中克隆它。 这是网站公共IP:http: //3.16.1.224 在所述公共IP中,除了图像和CSS之外,我的所有内容都在显示。 我试图登录管理面板,但同样,没有CSS和图像。

我的文件夹结构是这样的:

jangooCMS
 accounts  /* an app for user creation */
 articles  /* an app for article creation */
 assets    
   logo-jangoo.png
 db.sqlite3
 jangooCMS  /* my main app */
   settings.py
   urls.py
   views.py
   wsgi.py
 manage.py
 media   /* all the articles images inside here */
   4-kitties-low.jpg
 requirements.txt
 templates
   base-layout.html

Now my base-layout.html have this on top:
    {% load static from staticfiles %}


现在转到我的AWS,我的settings.py如下:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'assets'),
)

MEDIA_URL = '/media/'

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

STATIC_ROOT = os.path.join(BASE_DIR, "assets/")

请帮助..我第一次在这里发帖。 谢谢!

这是我将项目从github部署到AWS EC2时所遵循的指令:

cd Downloads/
mv blogoo.pem ~/Desktop/
cd ..
cd desktop

chmod

ssh

yes

sudo apt-get update
sudo apt-get install python3-pip python3-dev nginx git

Y

sudo apt-get update
sudo pip3 install virtualenv 
git clone https://github.com/madonnadevt/jangooCMS.git
cd jangooCMS
virtualenv venv
source venv/bin/activate
pip3 install -r requirements.txt
pip3 install django bcrypt django-extensions
pip3 install gunicorn
cd jangooCMS
sudo vim settings.py


# Inside settings.py modify these lines allowed host public IP address I for INSERT

i


ALLOWED_HOSTS = ['3.16.1.224']

# add the line below to the bottom of the file

STATIC_ROOT = os.path.join(BASE_DIR, "assets/")

Save your changes and quit. ESC :wq

cd .. 
python manage.py collectstatic
gunicorn --bind 0.0.0.0:8000 jangooCMS.wsgi:application

ctrl+c

sudo vim /etc/systemd/system/gunicorn.service

i

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

ESC :wq

deactivate

(directory should be in ubuntu)
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo vim /etc/nginx/sites-available/jangooCMS

i

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

ESC :wq

sudo ln -s /etc/nginx/sites-available/jangooCMS /etc/nginx/sites-enabled
sudo nginx -t
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart


您的网站正在请求http://13.59.50.215/static/styles.css - 请注意其中的static/路径 - 但它找不到它。 那是因为您已在settings.py中将文件夹名称声明为assets

STATIC_ROOT = os.path.join(BASE_DIR, "assets/")

在nGinx配置中,您将其称为static/

 location /static/ {
  root /home/ubuntu/jangooCMS;
 }

因此,为了解决它,更优选使用

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM