PoolByte

Running Node.js and Python applications in cPanel

· PoolByte Technology

Running Node.js and Python applications in cPanel

Shared hosting is often considered for sites running PHP. However, small and medium-sized Node.js and Python applications can also be run on cPanel servers using CloudLinux. It may not be necessary to rent a separate server for a form processing service, a simple API, a webhook receiver, or a small administration tool written in Flask or Express. In this guide, we explain step by step how to install a Node.js application or Python application on cPanel.

These tools usually appear in cPanel as "Setup Node.js App" and "Setup Python App". If you don't see these tools in your package, check with the support team; Whether the feature is enabled depends on the server configuration.

Is it suitable for a shared environment?

First evaluate whether your application is suitable for this environment. In such installations, applications usually run with incoming requests via Phusion Passenger. If no requests are received for a while, the application can be stopped and restarted at the first request.

This structure is suitable for web applications and APIs that work with request-response logic. For background workers that need to work constantly, intensive WebSocket connections, or tasks that use high processors for a long time, dedicated resources such as VPS are a better choice. We explained the resource limits in the shared environment in our CPU, RAM and I/O guide.

Build the app

To create a Node.js application, you enter the "Setup Node.js App" section in cPanel and fill in the following fields when you open a new registration:

  1. Node.js version: Select the version your app supports.
  2. Application mode: Production for live environment.
  3. Application root directory: The folder where the code will be located, for example apps/api. Excluding this folder from public_html prevents your source code from being made directly accessible.
  4. Application address: The domain name or subpath where the application will run.
  5. Startup file: The file that starts the application, for example app.js.

For Python, there are similar fields in the "Setup Python App" section. The startup file is usually passenger_wsgi.py, and the entry point is the name of the WSGI application object.

In its simplest form, a working Node.js application startup file is as follows:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  res.end('Uygulama çalışıyor');
});

server.listen(process.env.PORT || 3000);

The simplest WSGI compatible example for Python is as follows:

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain; charset=utf-8')])
    return ['Uygulama çalışıyor'.encode('utf-8')]

Upload the code and install dependencies

You can transfer the code to the server via SFTP or Git. You can find version management and rollback steps with Git in our Git guide, and secure file transfer in our SFTP guide.

In Node.js applications, after the package.json file is loaded to the root directory, npm install can be run with the button on the interface. The application page also shows the command that will be used to enter the application's virtual environment in the terminal. With this command, you can enter the environment and install dependencies yourself or run scripts.

In Python applications, a separate virtual environment is created for each application. Enter the environment with the activation command shown on the interface and install the dependencies with pip install -r requirements.txt. Installing packages in this virtual environment, rather than system-wide, prevents different applications from affecting each other.

Separate environment variables from code

Do not write API keys, database passwords and secret values in the code. Define it in the environment variables section in the application settings and read it via process.env or os.environ in the code. If you're using a .env file, make sure it's not sitting in a web-accessible folder and hasn't been added to the Git repository.

After the change, reboot and look at the logs

After updating the code or environment variables, restart the application with the restart button in the interface. Otherwise, the old code may continue to work.

If the Node.js application or Python application does not open and you see a 502 or 503 error in the browser, the first place to look is the application's error logs. In Node.js applications, you can define a Passenger log file in the application settings; In Python applications, errors are usually written to the stderr.log file in the application root directory. A missing package, wrong startup file, or incorrect version selection are the most common causes. For the meaning of error codes, you can refer to our 404, 500, 502 and 503 guide.

Test version changes

Changing the Node.js or Python version may require reinstalling dependencies, and some packages may be incompatible with the new version. Try the version change in a separate test application first; We explained the steps in our staging environment guide.

Frequently asked questions

Will Express, Flask or Django work?

Usually yes. Express applications can be run as a standard HTTP server, and Flask and Django applications can be run via Passenger because they are WSGI compatible. Make sure that the version and dependencies required by the Framework are present in the environment.

My application closes automatically after a while, is it normal?

Passenger can stop applications that have not received requests for a while to save resources and restart them at the first request. That's why the first request is a little slow. For tasks that need to run regularly, use cron tasks instead of relying on the application to stay open all the time.

Official sources