Introduction
Python is essentially code executed on our servers to host, create and control web applications using this type of language.
In this article, we will focus on information for beginners. We will start by explaining how to access Python in N0C and provide a detailed example of how to launch and modify an application using this interface.
Then, for more advanced users, we will explain how to do the same operations in command line.
Documentation
Documentation on Python can be found on the Web, in particular at: https://docs.python.org/3/.
You can refer to this site to get more information about the commands of the Python Selector: https://docs.cloudlinux.com/command-line_tools/#new-python-selector.
Debugging an Application
When an error occurs, a page like the one below is displayed:
This page is not very explanatory. Activating the development mode allows you to investigate (how).
Prerequisite
Go to https://mg.n0c.com/en/.
Access to Python in N0C
- In the Sidebar menu, select Languages > Python.
- Applications already set up are displayed in the interface:
Implementation of a New Python Application with N0C
To explain the approach more easily, we will proceed with an example.
Let’s say you want to create the run.py application that displays the words “Hello World!”. The following steps are necessary:
- First of all, you have to create an application in the directory intended to contain it (we will call this directory “codepython” as an example, but the user can give it the name they desire). When it is executed, the application displays the words “It works!”.
- You then need to program it (in our example, to display “Hello World!”).
- Then, call the application environment.
- Finally, you have to activate the Python environment.
Step #1 – Create the Python Application
Make sure the codepython directory is created (how).
- Click on the Create button:
- Choose the desired VERSION of Python:
- Indicate in which APP DIRECTORY you want the application to be created (in our example, we would write codepython).
- Indicate from which APPLICATION DOMAIN/URL the application should be accessible (in our example, we would write codepython).
- Enter the name of the BOOT FILE (optional).
- Click on the CREATE button.
- Confirm the creation of run.py and passenger_wsgi.py1 by opening the codepython directory in the File Manager (how).
- Test the Python application by typing https://DOMAIN/codepython in your browser, DOMAIN having to be replaced by your real domain name.
- The result is (as expected): “It works!”.
Step #2 – Program the Application
Now that the Python application is installed and functional in the directory to be used, you can program it as we wish. The application must be started again to see the modifications in the browser when it is exectued.
Here are the steps to program:
- In the File Manager, open the directory (in our example, codepython) and edit the code run.py (how).
- Program run.py. In our example, copy the following code in the programming window of the File Manager (how), so that the words “Hello World!” are displayed when you run codepython on your domain:
import os import sys sys.path.insert(0, os.path.dirname(__file__)) def app(environ, start_response): start_response('200 OK', [('Content-Type', ‘text/plain')]) message = 'Hello World!n’ version = 'Python %sn' % sys.version.split()[0] response = 'n'.join([message, version]) return [response.encode()]
- After having saved the code, close and open N0C again to apply the changes.
- Test the Python application by typing https://DOMAIN/codepython in your browser, DOMAIN having to be replaced by your real domain name.
- The result shall now be “Hello World!”.
Step #3 – Call the Environment of the Application
Before performing any other command, you must call the application environment by running the following command at the root of your application, which will set the environment variables and allow you to configure your application:
The command must be copied into the shell of the account in question. It is possible to connect via SSH.
To see the command, you must edit the application (please refer to the paragraph Python Application Settings Modification).
Therefore, in our example, the command to be copied is :
source /home/fbkfdayw/virtualenv/codepython/3.8/bin/activate
Where :
- « fbkfdayw » must be replaced by your username;
- « codepython » must be replaced by the name of your application; and
- « 3.8 » must be replaced by the Python version number.
Step #4 – Activate the Python Environment
It is important to make sure that your application has the right versions of plugins.
- Click on the Restart icon next to the application to activate (in our example, codepython):
- The message Successfully executed command must be displayed to confirm the activation is successful.
You can consult the documentation available at https://docs.python.org/3/to help you debug your code.
Actions on Application with N0C
Restart a Python Application
Sometimes it is necessary to restart an application as, for example, when you modify the code.
- Click on the Restart icon next to the application:
- A message shall be displayed to confirm the successful execution of the command.
Python Application Version Modification
- Click on the Change version icon next to the application to modify:
- Select the desired VERSION PYTHON:
- Click on the EDIT button.
Python Application Settings Modification
- Click on the Edit icon next to the application to modify:
- Change the desired settings:
- Click on the EDIT button to save your changes.
- Execute the command to save the application environment.
Stopping and Starting an Application
It is possible to stop an application by clicking on the Stop icon:
It is possible to start an application by clicking on the Start icon:
Delete a Python Application
- Identify the application and click on the Delete icon:
- At the command prompt, confirm the deletion.
Implementation of a New Python Application in Command Line
For more advanced users, the command line approach can be used.
The following steps must be followed in order:
- Connect in SSH.
- Find the availble Python versions.
- Create the functional base application at the CloudLinux level.
Step #1 – Connect in SSH
Connect in SSH (how).
For example, if you are using the Apple MacOSX environment, you can enter:
ssh < USER >@ < DOMAIN > -p PORT
Where :
- USER: Your username.
- DOMAIN: The domain name (make sure it points to the hosting account).
- PORT : Port to use for hosting (unless otherwise specified, use 5022).
At the command prompt, enter your password.
Step #2 – Find the Available Python Versions
This command will return the Python versions available on the server:
cloudlinux-selector get --json --interpreter python | python -m json.tool
Step #3 – Creation of the Basic Functional Node.js Application at CloudLinux Level
The following command creates a functional base:
cloudlinux-selector create --json --interpreter python --app-root < DIRECTORY > --domain < DOMAIN > --app-uri < APP_URI > --version < VERSION > --startup-file < STARTUP_FILE > --entry-point app
Where :
- DIRECTORY: The directory in which you want to install your application. Example: codepython.
- DOMAIN: The domain on which you want to run your application. Example: domain.com.
- APP_URI : The path of your application after your domain. For example, /codepython which would become the following URL: domain.com/codepython.
- VERSION: An available version (according to the return of the command we saw before). Example: 3.3.
- STARTUP_FILE: The name of the “input” file of your application. Default: run.py.
In our example:
cloudlinux-selector create --json --interpreter python --app-root codepython --domain domain.com --app-uri domain.com/codepython --version 3.3 --startup-file run.py --entry-point app
If the return is {“result”: “success”, “timestamp”: 1614167732.675758}, you now have a working basic Python application.
Otherwise, an error message would be displayed and you would have to correct the command accordingly.
Actions on Application in Command Line
It is possible to start, stop or restart an application, to name just a few possible actions. Simply enter the appropriate command:
cloudlinux-selector (start | stop | restart | destroy) --json --interpreter python --app-root <APP_ROOT>
Where:
- start: Allows you to start.
- stop: Allows you to stop.
- restart: Allows you to restart.
- destroy: Allows you to delete.
In our example, if you want to start, you shall write:
cloudlinux-selector start --json --interpreter python --app-root codepython
Command Line Log Out
To log out, enter the following command:
exit
1Note: passenger_wsgi.py is the entry point of the application. It is a file and not an application. In other words, passenger_wsgi.py “redirects” to the file you have chosen (the file run.py in our example).