|
1 | 1 | # Deploy! |
| 2 | + |
| 3 | +Until now your website was only available on your computer, now we will learn how to deploy it! Deploying is a process of publishing your application on the Internet so people can finally go and see your app :) |
| 4 | + |
| 5 | +As you learned, website has to be located on the server. There is a lot of providers, but we will use a free and very easy one: [Heroku](http://heroku.com/). Heroku is free for small applications that don't have too much visitors, it'll be definitely enough for you too. |
| 6 | + |
| 7 | +We will be following this tutorial: https://devcenter.heroku.com/articles/getting-started-with-django, but we pasted it here so it's easier for you. |
| 8 | + |
| 9 | +## Requirements.txt |
| 10 | + |
| 11 | +We need to create a `requirements.txt` file to tell Heroku what Python packages needs to be installed on our server. |
| 12 | + |
| 13 | +But first, Heroku needs us to install `django-toolbelt` package. Go to your console with `virtualenv` activated and type this: |
| 14 | + |
| 15 | + $ pip install django-toolbelt |
| 16 | + |
| 17 | +After the installation is finished, run this command: |
| 18 | + |
| 19 | + $ pip freeze > requirements.txt |
| 20 | + |
| 21 | +This will create a file called `requirements.txt` with a list of your installed packages. |
| 22 | + |
| 23 | +## Procfile |
| 24 | + |
| 25 | +Another thing we need to create is a Profile. Open up your code editor, create a file called `Procfile` in `mysite` directory and add this line: |
| 26 | + |
| 27 | + web: gunicorn mysite.wsgi |
| 28 | + |
| 29 | +Then save it. Done! |
| 30 | + |
| 31 | +## mysite/settings.py |
| 32 | + |
| 33 | +Another thing we need to modify is our website `settings.py` file. Open `mysite/settings.py` in your editor and add following lines: |
| 34 | + |
| 35 | + import dj_database_url |
| 36 | + DATABASES['default'] = dj_database_url.config() |
| 37 | + |
| 38 | + SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') |
| 39 | + |
| 40 | + ALLOWED_HOSTS = ['*'] |
| 41 | + |
| 42 | + STATIC_ROOT = 'staticfiles' |
| 43 | + |
| 44 | +Then save a file. |
| 45 | + |
| 46 | +## mysite/urls.py |
| 47 | + |
| 48 | +Open `mysite/urls.py` file and add this two line in the begining of the file: |
| 49 | + |
| 50 | + from django.conf.urls.static import static |
| 51 | + from django.conf import settings |
| 52 | + |
| 53 | +And add this line after last `)`: |
| 54 | + |
| 55 | + + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) |
| 56 | + |
| 57 | +The whole thing should look like this: |
| 58 | + |
| 59 | + urlpatterns = patterns('', |
| 60 | + url(r'', include('blog.urls')), |
| 61 | + url(r'^admin/', include(admin.site.urls)), |
| 62 | + ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) |
| 63 | + |
| 64 | +## mysite/wsgi.py |
| 65 | + |
| 66 | +Open `mysite/wsgi.py` file and replace this line: |
| 67 | + |
| 68 | + application = get_wsgi_application() |
| 69 | + |
| 70 | +with this: |
| 71 | + |
| 72 | + from dj_static import Cling |
| 73 | + application = Cling(get_wsgi_application()) |
| 74 | + |
| 75 | +All right! |
| 76 | + |
| 77 | +## Install Heroku toolbelt |
| 78 | + |
| 79 | +You need to install your Heroku toolbelt which you can find here: https://toolbelt.heroku.com/ |
| 80 | + |
| 81 | +You can also create a free Heroku account here: https://id.heroku.com/signup/www-home-top |
| 82 | + |
| 83 | +Then authenticate Heroku account on you computer by running this command: |
| 84 | + |
| 85 | + $ heroku login |
| 86 | + |
| 87 | +## Git |
| 88 | + |
| 89 | +Heroku uses git repository to manage your project files, so we need to use it to. |
| 90 | + |
| 91 | +Create `.gitignore` file with following content: |
| 92 | + |
| 93 | + venv |
| 94 | + *.pyc |
| 95 | + staticfiles |
| 96 | + |
| 97 | +and save it. The dot on the begining of the name file is important! This will tell Heroku to ignore this file and don't use it. |
| 98 | + |
| 99 | +Next, we’ll create a new git repository and save our changes. Go to your console/command-line and run this commands: |
| 100 | + |
| 101 | + $ git init |
| 102 | + Initialized empty Git repository in /Users/kreitz/hellodjango/.git/ |
| 103 | + $ git add . |
| 104 | + $ git commit -m "My Django Girls app" |
| 105 | + [master (root-commit) 2943412] My Django Girls |
| 106 | + 7 files changed, 230 insertions(+) |
| 107 | + create mode 100644 .gitignore |
| 108 | + create mode 100644 Procfile |
| 109 | + create mode 100644 mysite/__init__.py |
| 110 | + create mode 100644 mysite/settings.py |
| 111 | + create mode 100644 mysite/urls.py |
| 112 | + create mode 100644 mysite/wsgi.py |
| 113 | + create mode 100644 manage.py |
| 114 | + create mode 100644 requirements.txt |
| 115 | + |
| 116 | +## Deploy to Heroku! |
| 117 | + |
| 118 | +It's as simple as running this command: |
| 119 | + |
| 120 | + $ heroku create |
| 121 | + |
| 122 | +This automatically added the Heroku remote for our app to our repository. Now we can do a simple git push to deploy our application: |
| 123 | + |
| 124 | + $ git push heroku master |
| 125 | + |
| 126 | +## Visit your application |
| 127 | + |
| 128 | +You’ve deployed your code to Heroku, and specified the process types in a `Procfile`. You can now instruct Heroku to execute a process type. |
| 129 | + |
| 130 | +Let’s ensure we have one dyno running the web process type: |
| 131 | + |
| 132 | + $ heroku ps:scale web=1 |
| 133 | + |
| 134 | +We can now visit the app in our browser with `heroku open`. |
| 135 | + |
| 136 | + $ heroku open |
| 137 | + |
| 138 | +As you can see, there is an error. Heroku created a new database for us and it's empty. We also need to sync it: |
| 139 | + |
| 140 | + $ heroku run python manage.py syncdb |
| 141 | + |
| 142 | +You should now see your website in a browser! Congrats :) |
| 143 | + |
| 144 | + |
0 commit comments