Search Our Database
How to Install PHP (Laravel) and Node.js on the Same Instance
Introduction
Running both PHP (Laravel framework) and Node.js on a single instance is a common requirement for full-stack applications where Laravel serves as the backend API or web framework and Node.js is used for front-end build tools (such as Webpack, Vite, or npm) or real-time services (such as Socket.io). Deploying both technologies on the same server allows developers to consolidate resources and reduce operational overhead by maintaining a single environment.
This setup is particularly useful for small to medium-sized projects or in scenarios where budget constraints limit the use of multiple servers. It is applicable in development environments, staging, and even production, provided that proper resource management and isolation (e.g., through Docker or different service ports) are in place.
Laravel, a powerful PHP framework, is widely used for building robust web applications, REST APIs, and backend services. Node.js, on the other hand, is typically employed to manage JavaScript tooling (such as npm or yarn), front-end builds, and for providing real-time capabilities like WebSocket communication.
Some challenges that administrators and developers may face when setting up both PHP (Laravel) and Node.js on the same instance include dealing with conflicting ports, managing process control (for example, running both the PHP-FPM service and Node.js applications simultaneously), and setting proper permissions for Laravel to execute Node.js-based commands like npm run build.
This guide provides a comprehensive step-by-step process for installing both PHP (Laravel) and Node.js on a single server instance, ensuring that both services can coexist without issues. It will focus on a typical Ubuntu-based system, though the principles can be applied to other Linux distributions with slight modifications.
Prerequisites
- An Ubuntu 22.04 LTS server instance (or a similar Linux distribution)
- A non-root user with sudo privileges
- Basic knowledge of Linux command-line operations
- Apache or Nginx web server installed (Apache 2.4+ or Nginx 1.18+)
- PHP 8.1 or higher installed (along with PHP-FPM if using Nginx)
- Composer (version 2.0+)
- OpenSSL 1.1.1 or higher
- MySQL 8.0+ or PostgreSQL 13+ (optional, for database)
- Curl and Git installed
Step-by-step Guide
Step 1: Update the System
Begin by updating the system packages to ensure that all software is current.
sudo apt update && sudo apt upgrade -y
Step 2: Install PHP and Required PHP Extensions
Install PHP along with the necessary extensions commonly used by Laravel.
sudo apt install php php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-bcmath unzip curl git -y
Step 3: Install Composer
Composer is required to manage Laravel dependencies.
curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer composer --version
Step 4: Install Laravel
Navigate to the desired directory (e.g., /var/www/ ) and create a new Laravel project.
cd /var/www/ composer create-project laravel/laravel my-laravel-app
Step 5: Install Node.js and npm
Use NodeSource to install the latest stable version of Node.js.
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install -y nodejs node -v npm -v
Step 6: Configure Laravel Front-End Dependencies
Navigate into the Laravel project directory and install the default front-end dependencies (typically Vite and other npm packages).
cd /var/www/my-laravel-app npm install
Step 7: Configure Permissions and Ownership
Ensure the Laravel project directory is writable by the web server.
sudo chown -R www-data:www-data /var/www/my-laravel-app sudo chmod -R 755 /var/www/my-laravel-app/storage sudo chmod -R 755 /var/www/my-laravel-app/bootstrap/cache
Conclusion
This guide has detailed the process of installing PHP (Laravel) and Node.js on a single server instance, ensuring they coexist efficiently. By following the steps outlined, a fully functional environment is now prepared to serve both Laravel applications and manage Node.js-based front-end assets or services.
For further learning, consider exploring topics such as configuring Supervisor to manage Node.js processes in production or setting up SSL with Let’s Encrypt.
Should you have any inquiries about the guidelines, please feel free to open a ticket through your portal account or contact us at support@ipserverone.com. We’ll be happy to assist you further.