As we know shared hosting doesn’t have python script on the cPanel. But the base system has python and already installed. If we can access SSH on cPanel we can run flask python and other python scripts too. It’s not too tough and for this process, we don’t need root user permission.
How to install virtualenv python for flask app on cPanel base shared hosting
- SSH login (example: HostGator)
By default Hostgator SSH supported login port 2222
Here is how to login connect to SSH. Goto your terminal (Mac or Linux) and type following command by giving your domain and cPanel username.
ssh -p 2222 firstname.lastname@example.org
Then type your Cpanel password. After login, you will see your terminal shell change to your cPanel shell. Type the command to see your files and folders
ls command for showing files and folders, however adding with -al you can see all the files and folders including hidden and special characters also.
Let’s check first in your cPanel python installed or not. It is most important. To see python info type following command.
This command will show you a directory where python installed by default in a system. In my case, my default python installed on the directory that shows in this picture.
You can check your python version by typing this command.
We can’t write in our root directory where python installed using shared hosting. So we need to install virtual environment for python. First, we need to create a folder for that. We can create a folder by using cPanel login then file manager. I use the terminal for my case. Here is the command for creating and entering the created folder directory.
First check where are you now. To know your current directory type the command.
This command will show you where are you now. In my case, I’m in my user folder. Let’s create a folder.
a new folder will create a name of “foldername“. You can use any name for that folder. In my case, my folder name “alam_scripts”
To enter that folder type
Now we will download the scripts for a virtual environment of python. following commands
curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz
This command will download a python package of virtualenv.
after that let’s extract and enter the downloaded directory.
gzip -cd virtualenv-12.0.7.tar.gz |tar xf -
virtualenv-12.0.7.tar.gz extracted now enter the folder of virtualenv-12.0.7
install python on your virtual environment following this command.
python2.7 setup.py install --user
after that create a virtual environment for your python project.
For that follow the commands. We are in our downloaded folder. using this command it will create a virtual python environment folder. In my case “venv” is the virtualenv of my python.
~/.local/bin/virtualenv venv --python=python2.7
Let’s activate our virtualenv python.
Did you see any changes of your terminal? Here are the changes after activation of python virtualenv
Now install flask python packages.
pip install flask
This command will install python flask packages on your venv folder.
For now, deactivate your python virtualenv. Commands are below.
We are almost done.
Read my second post.