How to run anything in your web server?
Tags: backend, webdev, linux
Published: 2020-05-01 08:16:24

Web applications are all around us, they allow us to communicate in unimaginable manners. Best of all, it is straightforward to set up your own web site and post any content you like. But what happens when you wish to run more complex stuff in your web server, maybe you need a fast application that replies to millions of requests, maybe you are just curious and wish to test your self in the web jungle, maybe you are in quarantine and don't have anything better to do. Whatever the reason (I don't judge you) this is the post for you, here you'll learn the basics of CGI programming.  


# Common Gateway Interface 
A common gateway interface or CGI is the most common way to run scripts in the cloud. Commonly you would send **POST**/**GET** requests to your web server and get a response in HTML that is displayed by your web browser. On the other hand, when you configure your server with a CGI every post request will instead execute a specific file in your server. This file can be anything, from a **Python** script to a **C++** binary (as long as your server can run it). This script will also get a response of course, in fact, you can run an entire web page on CGI; although you can create every component of your page manually it is easier to use some **framework** (e.g. Pistache, Wt for C++). 

# Simple setup with Apache 
Before trying to send an HTTP request to the specific file we want to execute we need to set up the web server, for this, we'll use Apache. 
### Step 1 Locate your CGI directory
Scripts can only be executed in a specific directory, otherwise, you could run everything... Thus, the first we need to do is locate it, in any of the following directories 

* `/etc/apache2/httpd.conf`
* `/etc/apache2/apache2.conf`
* `/etc/httpd/httpd.conf`
* `/etc/httpd/conf/httpd.conf`

Inside your config file, you'll need to find the **ScriptAlias** variable, this variable defines the location of your CGI files (you can change it if you want) 

### Step 2 Writing your CGI program
There are many ways to write a CGI program but remember it needs to deliver a response to the user, the response will be the text that write in the **Standard Output**. A simple program would be the following 
```python
#!/dir/to/python/env
# script.py / script.cgi 
print("Content-type: text/html\n") # write text content type(e.g. text/plain, image/gif)
print("<h1>This is a text header</h1>") # write the content 
```
### Step 3 Set permissions 
For apache to execute the script for you, you should first set permissions for it to run it 
`chmod a+x script.py`

### Step 4 Test it 
Now to try your new CGI script, it is as easy as sending an HTTP request, for simplicity you can use **curl**. 
`curl example.com/cgi-bin/script.py`

# Extra
There are two things you may be wondering by now. First of all, how can I use the data that was sent using a POST request from my python file? The data that is sent using a post request is saved in Linux **environment variables**, so it is a simple as reading them, probably in something like QUERY or PATH. Another thing that can be easily spotted is that every time a user sends a request a new process is created in the server, so if you have a lot of requests it is easy for the server to collapse. An alternative to CGI is FastCGI, it enables you to execute a single process that processes each request. A better idea would be to code your own web API, but that is beyond the scope of this post!

# Conclusion 
At this point you should have successfully executed your app in the cloud, if that was not the case, contact me and I'll help you as I can.