Key Highlights:
git push
. A free tier is available for low-traffic applications.Procfile
dictates what Heroku executes. After configuring the Procfile
and adding it to the repository, deployment is achieved using git push
. Additional commands manage persistent configuration, scale processes, and handle rollbacks.Special thanks to Matthew Wilkin for his valuable peer review contributions.
This guide explains Heroku and its web application deployment process.
Heroku is a managed platform for rapid web application deployment. It automatically provisions server resources, simplifying deployment to a git push
operation. A free tier allows for easy and cost-free initial deployments (subject to traffic limitations).
While cost-effective compared to dedicated DevOps teams, high-traffic applications can incur significant costs (each dyno costs $25 monthly, with database additions increasing expenses).
Before You Begin:
To follow this guide, ensure you have:
If you have a ready-to-deploy project, skip the next section and proceed to "Creating a Heroku Project."
Example Project:
This example uses a Python Flask application. You can adapt the process for other projects. If you have your own project, skip this section.
Create a project directory (e.g., myproject
):
<code>/myproject /templates index.html app.py requirements.txt</code>
Populate the files as follows:
app.py
:
import os import flask app = flask.Flask(__name__) @app.route("/") def index(): return flask.render_template("index.html") if __name__ == "__main__": app.run(port=os.environ.get('PORT', '5000'))
templates/index.html
:
<!DOCTYPE html> <html> <head> <title>My example project</title> </head> <body> <h1>This is my project.</h1> </body> </html>
requirements.txt
:
<code>Flask==0.10.1</code>
Install dependencies:
pip install -r requirements.txt
Verify functionality by running python app.py
and accessing http://localhost:5000/
.
Creating a Heroku Project:
git init
heroku create
(or heroku create myproject
to specify a name). This generates a name, URL, and Git repository, and initializes the Heroku remote repository.Understanding Buildpacks:
Heroku uses buildpacks to manage projects. These provide instructions for dependency retrieval, building, and execution. Official buildpacks exist for several languages (Node.js, Ruby, Java, Clojure, Scala, PHP, Python, Go). Heroku automatically detects the project type based on conventions (e.g., requirements.txt
for Python). Third-party buildpacks support other languages or build tools.
Configuring the Procfile:
Heroku uses a Procfile
to determine what to run. For a simple web application, add a Procfile
with the following content:
<code>/myproject /templates index.html app.py requirements.txt</code>
(For improved performance, consider Gunicorn: Add it to requirements.txt
and use web: gunicorn app:app -b 0.0.0.0:$PORT
in the Procfile
.)
Deploying Your Project:
Procfile
: git add Procfile && git commit -m "Added Procfile"
git push heroku master
Deployment Complete!
Your application should now be deployed. Access it via the URL provided by Heroku.
Additional Heroku Commands:
heroku config:set MY_ENV_VARIABLE=some_value
: Sets persistent configuration values.heroku ps:scale web=5
: Scales the web process (use cautiously due to cost implications).heroku releases
: Lists app releases.heroku rollback <release_identifier></release_identifier>
: Rolls back to a specific release.heroku rollback
: Undoes the latest release.These can also be managed via the Heroku dashboard.
Frequently Asked Questions (FAQs): (This section has been omitted to keep the response concise, as it was already quite long. The original FAQs can be easily re-integrated if needed.)
The above is the detailed content of Deploying to Heroku: An introduction — SitePoint. For more information, please follow other related articles on the PHP Chinese website!