My Bank Wants You To Be Safe From Scams

My bank just sent out a notice about keeping safe from scams. They are trying to inform the public so their customers can keep from needlessly losing money to scammers. They sent the notice to their…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Deploying Flask Application on VPS Linux Server using Nginx

Python, Flask, VPS, Web, Nginx

I shared steps that I followed while deploying my flask application on VPS. If you are looking for a step-by-step tutorial, Let’s go!

You can publish easily your static sites or sites developed using backend APIs determined by hosting services you bought. However, that program usually is not appropriate for dynamic sites which has a python backend. So, you will be looking for another solution: VPS. You can simply run your python application like in local and deploy by making some configurations on a VPS. You can make it the same on a physical machine also. VPS enables us to reach the service at a lower cost and we can do things quickly. So, I am sharing my experiences that I followed the steps while deploying my application on a VPS.

You can rent a VPS service from anywhere you want. (AWS VPS, some other local companies, or smth). I recommend you to start with a beginner package, then you can extend your service by your site’s needs. I prefer to rent an Ubuntu Virtual Private Server. So, I will explain the other steps on it.

When you run the application, you will face the window shown below. We fill Host Name(or IP address) blank with the IP of our server then press the Open button.

putty açılış ekranı
PuTTY opening window

We log in as root users on the terminal window that opens. We can now access the machine from the terminal.

Run the command below to create a new user:

Add superuser privileges to the user:

Switch between users:

If you got help from different modules when working with python, probably you struggled with version conflicts and the libraries forgotten somewhere on your computer. Python Virtualenv saves you from that problem. It prepares a virtual space to work where you install necessary libraries for a specific project. The libraries are kept in this environment, not in the computer as available the reach of all projects.

Firstly, set up the pip3 module so that we can use it for other installations:

Install virtualenv:

Test the installation:

Continue with creating the virtual environment for our development:

Path of python3.6 is given as a parameter so that installing environment by this python version. Otherwise, the environment is created by using the default python version. You can change the parameter with the required version for your project. After running the command, a folder is created with the name of “webapp”. We will continue our work in this folder. Let’s go in.

Run this command to activate the working environment:

The environment name appears at the beginning of the command lines after activation.

virtualenv activated

Just deactivate to stop running in that environment.

Continue installing in the activated virtual environment:

Assuming flask project is in flaskapp.py :

When you run the above code, the “Hello World” web page will be reachable in localhost:5000. You can test it by running that code:

WSGI(Web Server Gateway Interface) is a python standard that describes how a web server communicates with web applications.[4] The proxy server enables applications to create responses for requests coming from clients. It builds the communication bridge between clients and applications. However, python frameworks (such as bottle, flask, Django) isn’t able to communicate with proxy server directly. In the other words, common servers like apache, Nginx don’t know how to communicate with our app. So, we need a translator: WSGI.[3,5] some libraries allow us to use this standard. We will continue our journey with the “gunicorn” library.

Create a wsgi.py file like a crossover point to use in configuration.

This will be in the same directory with the flaskapp.py main module, and it runs by calling the app object. Thus, it runs our app in a long way. We can control Gunicorn with belove command by giving IP and port parameters information that we want to run on.

Now, we can continue by deactivating (cmd:deactivate) the virtual environment.

Let’s create a service so that our application continues running when the machine alive, and rerun automatically when the machine restarts. Assuming that the name of service is flaskapp, run the following command:

The service file is open in the terminal. Now is time to write down service information.

Note: in terminal; “i” for insert mod, insert+shift for pasting, esc than 2 times shift+z for leaving with saving changes.

Yes, our service is ready to run. Control with the following commands:

The status of the service is printed on the screen. Check your service file if there is an error condition.

7- Nginx Configuration

Install the Nginx server:

There are two different directories names sites-available and sites-enabled. Sites under available are defined but inactive sites. So, we will make a connection with a symbolic link to the enabled directory after making the definition available. Thus, our website will be active.

By the above command, edit the file in the terminal.

Nginx should be restart:

Need to give Nginx full access:

If there is no point that we missed, insha’Allah, our application is now live with the port number we set in the IP address of our VPS server!

Let’s check! 😉

[1] https://burcus.medium.com/nginx-ve-gunicorn-ile-python-flask-uygulamas%C4%B1n%C4%B1-linux-sunucuda-yay%C4%B1nlama-349d8da3618c

Add a comment

Related posts:

Usher Dj Got Us Fallin In Love Mp3 Download

Musicas para download, download de musicas, download mp3. Ads 468x60px…. Garden (Feat. Robin Thicke) 17. DJ Got Us Fallin’ In Love (Feat. Pitbull) 18. Love ’Em All 19. My Boo (Feat. Alicia Keys) 20…

Evolving role of Hotel Asset Managers in India

While the hotel industry has become increasingly specialized in recent years, the owners still tend to play an active role in day-to-day operations. In the last ten years, Operations and Projects…

The Best Budget Hosting for All Your Needs

If you were thinking of trying your hand at blogging to attempt to generate an e-based income you will need to acquire budget hosting solutions first in order to do so. Without a budget hosting…