|
0. Select the Heroku cloud platform
With cloud computing the wave of domestic cloud services can be described as varied, although the price is not high, but it can really provide a permanent free use, even if some restrictions or, it seems to be found.
For a variety of needs study / research / experimental / or Real Application Testing, etc., then we might use to deploy applications to overseas Heroku cloud platform, Heroku's free version in addition to providing the Postgres database is limited outside (less than 10000 small database records do not pay you can add to your own Web application), the other is free to use, this is definitely the best choice.
Another point is, Heroku supports Python is very good, so it will be a lot easier to deploy, which eliminates a lot of unnecessary trouble.
Recent applications should be deployed on Heroku, where the complete deployment process for everyone to share.
Explanation: The operating system I am using Ubuntu 15.10, deploy the Python Web application based Flask application, following the deployment process is in the operating environment for deployment, we recommend that you also deploy Linux environment will be a lot easier. (Of course, you have a Mac Book better, but bloggers have no money, can not afford it)
1. Prepare Git environment
To use Heroku, your Web application must be hosted in a Git repository, If you've been using Git as a way to develop, and that problem is easy to solve, I believe you understand the truth. If not, please follow my method to generate a Git repository, it is very simple.
Many people think that using Git a lot of trouble, so he abandoned the use Heroku platform, say that this is a tremendous waste:
1. First, the waste is such a good platform Heroku resources;
2. Then use Git is to give up such a good version control system
3. Furthermore, that is to renounce the use of Linux environment.
In fact, these are very helpful for the development of it! So do not feel trouble, as long as you do not completely developed after the Windows platform, which is something you should want to know, just getting used to, and behind it all very natural.
Step 1: Install Git
In Ubuntu, for example, you can use the following command to install:
sudo apt-get install git
Of course, if you are using other versions of Linux distributions, we believe it is not difficult to install.
Step Two: Put your complete Web application into a new directory
For example, here I have developed a complete Web application, it mainly has the following of these files and applications:
drwxrwxr-x 6 xpleaf xpleaf 4096 at 16:04 on January 29.
drwxr-xr-x 38 xpleaf xpleaf 4096 at 16:01 on January 29 ..
drwxrwxr-x 7 xpleaf xpleaf 4096 1 Yue 29 03:31 app
-rw-rw-r-- 1 xpleaf xpleaf 3295 1 Yue 29 03:31 config.py
-rw-rw-r-- 1 xpleaf xpleaf 1083 1 Yue 29 03:31 LICENSE
-rwxrwxr-x 1 xpleaf xpleaf 2391 1 Yue 29 03:31 manage.py
drwxrwxr-x 3 xpleaf xpleaf 4096 1 Yue 29 03:31 migrations
-rw-rw-r-- 1 xpleaf xpleaf 25 1 Yue 29 03:13 Procfile
-rw-rw-r-- 1 xpleaf xpleaf 376 1 Yue 29 03:31 README.md
-rw-rw-r-- 1 xpleaf xpleaf 76 1 Yue 29 03:13 requirements.txt
drwxrwxr-x 2 xpleaf xpleaf 4096 1 Yue 29 03:31 tests
Then create a file called Heroku_pro folder, and complete the above Web applications into them, as follows:
xpleaf @ leaf: ~ / Heroku_pro $ pwd
/ Home / xpleaf / Heroku_pro
xpleaf @ leaf: ~ / Heroku_pro $ ls
app config.py LICENSE manage.py migrations README.md requirements.txt tests Procfile
This is done!
The third step: Generate a Git repository at Heroku_pro directory
xpleaf @ leaf: ~ / Heroku_pro $ git init
Initialize an empty Git repository in /home/xpleaf/Heroku_pro/.git/
Then you will find in the current directory more than a .git directory:
xpleaf @ leaf: ~ / Heroku_pro $ ls -a
. App .git manage.py README.md tests
.. Config.py LICENSE migrations requirements
This step is completed.
Step four: all files in the current directory to a local Git repository hosting all
xpleaf @ leaf: ~ / Heroku_pro $ git add.
xpleaf @ leaf: ~ / Heroku_pro $ git commit -m "ver1.0"
[Master (root submit) a7cea3f] ver1.0
78 files changed, 3350 insertions (+)
create mode 100644 LICENSE
create mode 100644 README.md
create mode 100644 app / __ init__.py
create mode 100644 app / api_1_0 / __ init__.py
create mode 100644 app / api_1_0 / authentication.py
......
Ok, this is done.
Focus on the use of Git is clearly not here to discuss, here only to provide a temporary solution not used Git friends.
2. Registration Heroku account
With the current domestic most cloud platforms, first you need to register an account, you can register at the following links:
https://www.heroku.com/
wKiom1arIdeCH2S0AAAwrabARcc665.png
3. Install the Heroku Toolbelt client
Heroku Toolbelt is a command-line tool, the benefits of using this client is that we can locally by way of command-line operations to manage our hosted on Heroku Web applications.
In Ubuntu, for example, refer to the official installation documentation:
wKioL1arJNPyFfuxAAFTt-ic4Oo654.png we use the following command to install it:
wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh
Can refer to the official documentation here: https: //toolbelt.heroku.com/
4. Login Heroku
Run the following command in the directory just the Heroku_pro, landing on Heroku:
xpleaf @ leaf: ~ / Heroku_pro $ heroku login
Enter your Heroku credentials.
Email: flasky_mini@sina.com
Password (typing will be hidden):
Logged in as flasky_mini@sina.com
xpleaf @ leaf: ~ / Heroku_pro $
Note that after the successful landing Tip: Logged in as flasky_mini@sina.com
Note, login command will automatically create a default for your current host SSH public key and upload, SSH public key is very important, when executed git push command behind you must use to, but do not worry, heroku of clients will automatically help us to deal with. Of course, you can also manually upload only need to execute the command: heroku keys: add.
5. Using Heroku client program created app
The so-called app program is actually herokuapp.com a subdomain, when you create app program called my-heroku-app-cn in Heroku, if you are successful deployment, you can directly through the address https: // my-heroku-app-cn.herokuapp.com to access your Web application, hence the name to note that you can not use someone else has used. Here we have to create one.
xpleaf @ leaf: ~ / Heroku_pro $ heroku create my-heroku-app-cn
Creating my-heroku-app-cn ... done, stack is cedar-14
https://my-heroku-app-cn.herokuapp.com/ | https://git.heroku.com/my-heroku-app-cn.git
Appears above tips, it shows the program creates success! Heroku also assigned a Git server to us at https://git.heroku.com/my-heroku-app-cn.git
Of course, in fact, it is the name of app program can be changed, as long as you log in Guan Ju net, in your personal page where you can modify.
6. Configuration Database
Heroku Postgres support ways to expand the database, but some restrict it, as I said before. Here we use Postgres database, which also means that you need to add the appropriate database path in your Web application source code, this would be to say we.
Use the following commands to configure the database:
xpleaf @ leaf: ~ / Heroku_pro $ heroku addons: create heroku-postgresql: hobby-dev
Creating postgresql-rectangular-17531 ... done, (free)
Adding postgresql-rectangular-17531 to my-heroku-app-cn ... done
Setting DATABASE_URL and restarting my-heroku-app-cn ... done, v3
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! Data from another database with pg: copy
Use `heroku addons: docs heroku-postgresql` to view documentation.
Appears above tips also explains the success of the database configuration, detailed information about the database, you can also go to the official website of individual centers of view, there are very detailed, as follows:
How to deploy Python Web application: Heroku deployment process complete records
How to deploy Python Web application: Heroku deployment process complete records
How to deploy Python Web application: Heroku deployment process complete records
Here we need to note saying: Setting DATABASE_URL and restarting my-heroku-app-cn ... done, v3
That is, Heroku automatically created for my-heroku-app-cn program we've just created a project called DATABASE_URL environment variable, the variable is in Heroku platform path address of the database, which also means that if you want after your Web application deployment success can be properly connected to the database, you must set the address of the database in your source code as: DATABASE_URL, nothing more, other Heroku will complete us, do not worry.
7. Configure the necessary environment variables
This is not necessary, depending on your Web application's source code if there is need to get the system environment variables, if any, you can make the environment variables by the following manner.
For example, I have the following two lines of code in my source code configuration file:
MAIL_USERNAME = os.environ.get ( 'MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get ( 'MAIL_PASSWORD')
Apparently the e-mail user name and password for sensitive information should not appear in the source code, so I was by getting the value of the environment variable to know the user name and password, so I need to set the corresponding environment variables on Heroku, the following command :
xpleaf @ leaf: ~ / Heroku_pro $ heroku config: set MAIL_USERNAME = "xpleaf"
Setting config vars and restarting my-heroku-app-cn ... done
MAIL_USERNAME: xpleaf
xpleaf @ leaf: ~ / Heroku_pro $ heroku config: set MAIL_PASSWORD = "***"
Setting config vars and restarting my-heroku-app-cn ... done
MAIL_PASSWORD: ***
Of course, if you need to set other environment variables, set in this fashion can be.
8. Use the production Web server
We know that using Django or Flask for the development of the time, they built a development Web server to connect to our Web application, which in the development process is, but right then up. But now we have to do is to deploy in a production environment to our Web application, so you can not say they have to use the original Web development framework that comes with the server, because the performance can not guarantee it is because they are designed for the development environment the Web server, rather than production environments. So, we want to use the Web server production environment. (PS: Please note the difference between Web applications and Web server)
I want to deploy a Web application is based on Flask, so of course you want to use a Web server software supports Flask, here I am using Gunicorn. Of course, it depends on what you are using the Web framework, on this point, they are free to learn.
Gunicorn chose to use as my production environment, I would be just need to add it to the needs of dependent files back on.
Of course, you can put Gunicorn downloaded for local test, because Python Web framework that not everyone use are Flask or use raw Web server software is Gunicorn, so here is not mentioned.
9. Add files and depend on demand file Profile
This is necessary also very important, otherwise it is difficult to deploy successfully!
Heroku require our Web program (here for the Python Web application) directory must have the following two files:
Required File Description
requirements.txt Web applications rely on various third-party expansion pack
Procfile which contains the command to start execution of my Web application server
Just as in my Heroku_pro directory to see:
xpleaf @ leaf: ~ / Heroku_pro $ ls -l requirements.txt Procfile
-rw-rw-r-- 1 xpleaf xpleaf 25 1 Yue 29 03:13 Procfile
-rw-rw-r-- 1 xpleaf xpleaf 76 1 Yue 29 03:13 requirements.txt
Note that both files must be located in the current directory Heroku_pro.
Requirements.txt contents of the file looks like this:
Flask == 0.10.1
Flask-Bootstrap == 3.0.3.1
Flask-HTTPAuth == 2.7.0
Flask-Login == 0.3.1
...
SQLAlchemy == 0.9.9
WTForms == 1.0.5
Werkzeug == 0.10.4
alembic == 0.6.2
bleach == 1.4.0
Which contains support my Web applications running on a variety of expansion packs. As for the content of course, what is depending on you are developing a Web project.
The contents of the Profile file like this:
web: gunicorn manage: app
As I mentioned earlier, which put the commands, such as the command used to start here is what I said earlier Gunicorn production Web server.
Again, these two documents is very important, if not, it will fail to be deployed.
10. The Executive git push to deploy
Front after confirmation, can be deployed, of course, if you modify your temporary files Heroku_pro current directory, use the following command to commit your changes:
git add.
git commit -m "ver1.0"
Ok, we began to deploy the following:
xpleaf @ leaf: ~ / Heroku_pro $ git push heroku master
Object count: 97, complete.
Delta compression using up to 4 threads.
Compressed objects: 100% (90/90), is completed.
Write objects: 100% (97/97), 35.04 KiB | 0 bytes / s, complete.
Total 97 (delta 22), reused 0 (delta 0)
remote: Compressing source files ... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing runtime (python-2.7.11)
remote: -----> Installing dependencies with pip
remote: Collecting Flask == 0.10.1 (from -r requirements / common.txt (line 1))
..........
remote: -----> Preparing static assets
remote: Collectstatic configuration error To debug, run.:
remote: $ heroku run python manage.py collectstatic --noinput
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing ...
remote: Done: 37.2M
remote: -----> Launching ...
remote: Released v6
remote: https://my-heroku-app-cn.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy .... done.
To https://git.heroku.com/my-heroku-app-cn.git
* [New branch] master -> master
You see the middle of a lot of output, which is Heroku for our environment to install the required packages, and these packages that is specified in the demand in front of our file. Of course, these operations are performed in Heroku, we just see a local process of its operation only.
When deployment is complete, according to the way I started the Flask application of the selected Web server, use the following command to start:
# Enable Web server-related initial configuration
xpleaf @ leaf: ~ / Heroku_pro $ heroku run python manage.py deploy
Running python manage.py deploy on my-heroku-app-cn ... up, run.7690
INFO [alembic.migration] Context impl SQLiteImpl.
......
INFO [alembic.migration] Running upgrade 288cd3dc5a8 -> 2356a38169ea, followers
INFO [alembic.migration] Running upgrade 2356a38169ea -> 51f5ccfba190, comments
#Restart
xpleaf @ leaf: ~ / Heroku_pro $ heroku restart
Restarting dynos ... done
After completion of the above, you can access the Web application we created the:
https://my-heroku-app-cn.herokuapp.com/
Of course, when you visit it when I could have turned it off, you can access the address of another application using the same method of deployment:
https://flasky-mini.herokuapp.com/
11. Upgrade
If you later need to modify your source code in order to add or remove certain features in the revised Heroku_pro directory, execute the following command sequence:
# Tell Heroku, you want to upgrade the
heroku maintenance: on
# Submit Deployment
git push heroku master
# Rerun server
heroku run python manage.py deploy
heroku restart
# Tell Heroku, the upgrade is complete
maintenance: off
Ok, here, then, the paper concludes, in fact, about to deploy applications on Heroku, regardless of your Web application is based on the Python development, or other languages are similar, but here focuses on talking about Python Web application .
Of course, in the actual deployment process may encounter a variety of problems, then we need to give full play to your ability to adjust to changing circumstances, and when problems arise, you can find Google (Baidu to find little information, not just on Google I want to approach it), or to Stack Overflow above to find or ask questions at the same time to go read the official document according to the error message prompts to resolve the existing problems.
But anyway, the main process deployment Python Web application is given the operation similar to the above, I believe that this article first deployed on Heroku Python Web application will help friends, write this, it really is to help more friends, because the current domestic deployment resources about Python Web is very small, much less to deploy overseas cloud platforms go up. |
|
|
|