In this post, we will provide a tutorial on how to install Python 3.6 with a simple web server on a WebApps hosting account.

Our WebApps platform is designed to support all features of our shared hosting plans. On top of that, it also allows users to deploy projects using custom runtime environments such as Node.js. On a single WebApps account, you can have:

  • Standard PHP/Perl applications like WordPress, for example.
  • Node.js with out-of-the-box support.
  • Custom environments such as Django, Ruby.
  • MongoDB instances.
  • Full support of ICDSoft’s email features, including spam protection and Webmail software.

All the above is combined within a single hosting account on the WebApps platform.

Background processes are usually forbidden on shared hosting platforms, but the power of the WebApps platform comes from this exact feature. You can run background processes (daemons) on the WebApps platform. In effect, this means that you can run almost any type of software that runs on Debian Linux.

Of course, the platform has its specifics; you can find installation instructions for a simple “Hello World” Node.js app, Django, MongoDB, and Ruby on Rails in our knowledge base: https://www.icdsoft.com/en/kb/webapps_node_js

The essence of the WebApps platform is to provide a supervisor process handling the network connections to the background service, and monitoring the background process itself. You can configure the supervisor to do a proxy redirection from a URL on your site to the port where your service is running.

In the following tutorial, we will provide instructions on how to install Python 3.6 (a package that doesn’t come standard with the current Debian version on our servers), start a simple web server, and run this web server through a WebApps project.

Before proceeding, you need to enable SSH access for your account. You can do that through the “SSH Access” section of the hosting Control Panel.

Step 1 – Install Python 3.6

First, we will install Python 3.6. Log in through SSH to your hosting account, and run the following commands:

cd ~/private                     # the private/ folder isn't web accessible
mkdir python-temp python36       # create a temporary directory
pip install --user virtualenv    # use the pip package manager to install the "virtualenv" package
cd python-temp
wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz   # download python 3.6
tar -xf Python-3.6.9.tgz                                        # extract the archive
cd Python-3.6.9
./configure --prefix="$HOME/private/python36"          # configure & compile Python 3.6 
make -j 8
make install
cd ~/private
rm -rf python-temp/                                     # remove the temporary dir and files

Step 2 – Run a simple web server

At this point, you have Python 3.6.9 installed. Now, we will run a simple web server with some sample pages:

~/.local/bin/virtualenv myPyApp1 --python ~/private/python36/bin/python3

. myPyApp1/bin/activate
python --version                      # verify that we are running the correct version - "3.6.9"    

cd myPyApp1
mkdir html
cd html
echo "Page1" > page1.html             # create sample pages
echo "Page2" > page2.html             

python -m http.server 8888            # we have chosen 8888, but if it is taken already, choose a random number higher than 1024

Now you can open http://www.your_domain.com:8888/ (the address where we are running this project), and you should be able to see a listing with the sample pages page1.html and page2.html.

At this point, we have a simple web server running Python 3.6. Stop the server by pressing CTRL+C in the shell window.

Step 3 – Create a WebApps project to manage our server

Now, we will create a WebApps project that will manage your web server – it will start the web server automatically if it crashes, and will allow access via your domain name to this web server.

You can create your WebApps project either through the WebApps section of the hosting Control Panel, or through the command line.

To do it via the command line, use the following command:

sureapp project create --engine custom --engine-version - --release-dir ~/private/myPyApp1 --port 8888 --subdomain www --url-path / --start-cmd ~/private/myPyApp1/webapps-start pytest

In this command, the project is set to run in your main domain (www.your_domain.com). If you want to run the web server at another location, you can create a subdomain through the Subdomains section of your hosting Control Panel (eg. python.your_domain.com) and create the project with “–subdomain python” instead of “–subdomain www”.

You can also enable a free Let’s Encrypt certificate for your domain/subdomain, so that you have the web server running over https:// as well.

Start the web server for a test on the right port that you just created the project with:

python -m http.server 8888

Now open http://www.your_domain.com/ with your browser, and you should be able to see your test pages. Stop the server with CTRL+C. Run:

exit  #exit "virtualenv"

Step 4 – Making an executable for the WebApps supervisor

Now that we know that the project is running smoothly, we have to configure it to run with the WebApps supervisor service. To do that, we need to create a file which the webapps supervisor will run. Create a file named “~/private/myPyApp1/webapps-start” and paste the following in it:

#!/bin/bash
. $HOME/private/myPyApp1/bin/activate
cd $HOME/private/myPyApp1/html
exec python -m http.server 10242

Make the file executable:

chmod +x ~/private/myPyApp1/webapps-start

and test it to make sure there are no errors in the code:

~/private/myPyApp1/webapps-start

To verify the server is running properly, test it again at http://www.your_domain.com/. You can stop the server with CTRL+C in the SSH window.

Now we will enable the WebApps project, so that it manages our web server:

sureapp project shell pytest         # enter the context of the WebApps project "pytest"
sureapp service status               # check the status of your project "pytest"
sureapp service manage --enable      # this command enables your project
sureapp service status               # check the status of your project "pytest" again, to see it running
sureapp log follow                   # monitor the log of your project

You can now see your Python-enabled web server at: https://www.your_domain.com.

To stop the supervisor, use the following commands:

sureapp service manage --stop
sureapp service status

and to start the service again use:

sureapp service manage --start
sureapp service status

If you would like to experiment with this system yourself, open your WebApps account now at:

https://www.icdsoft.com/en/hosting/webapps

Author