Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Jane Alam

Web Developer & Designer, Code Analyzer & Bug Fixer

How to install virtualenv python for flask app on cPanel base shared hosting

/
/
/
175 Views

 

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.

  1. SSH login (example: hostgator)

By default hostgator SSH supported login port 2222

Here is how to login connect with SSH. Goto your terminal (Mac or Linux) and type following command by giving your domain and cPanel username.

ssh -p 2222 username@domainname.com

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 -al

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.

which python

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.

python -V

or

python --version

We can’t write on 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.

pwd

This command will show you where are you now. In my case, I’m in my user folder. Let’s create a folder.

mkdir foldername

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

cd foldername

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

cd 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 on 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.

source venv/bin/activate

Did you see any changes of your terminal? Here is 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.

deactivate

We are almost done.

#Last Part: How to run Flask python app on shared hosting cPanel using SSH

Read my second post of this post for flask app run on shared hosting base cPanel.

Leave a Comment

Your email address will not be published. Required fields are marked *