Setting Up a Linux Dedicated Server for Web Development

Setting Up a Linux Dedicated Server for Web Development

14.3K views
Summary
This article provides a comprehensive guide on setting up a Linux-based dedicated server for hosting PHP websites and Node.js websites. It is divided into two sections, one for PHP website hosting and the other for Node.js website hosting.

Setting Up a Linux Dedicated Server for Web Development

In this article, we'll explore the process of setting up a Linux-based dedicated server for hosting PHP websites and Node.js websites. We'll cover the necessary steps, commands, and configurations to ensure a smooth and efficient development environment.

Section 1: PHP Website Hosting

PHP is a widely used server-side scripting language for web development. Here's how you can set up a dedicated server to host PHP websites:

  1. Install Apache Web Server

    Apache is a popular web server for hosting PHP websites. To install Apache, run the following command:

    sudo apt-get install apache2
        
  2. Install PHP

    Next, you'll need to install PHP. Run the following command to install PHP and the necessary modules:

    sudo apt-get install php libapache2-mod-php php-mysql
        
  3. Configure Apache

    After installing Apache and PHP, you'll need to configure Apache to work with PHP. Edit the Apache configuration file using the following command:

    sudo nano /etc/apache2/mods-enabled/dir.conf
        

    Find the following line:

    DirectoryIndex index.html
        

    And replace it with:

    DirectoryIndex index.php index.html
        

    Save the changes and restart Apache:

    sudo systemctl restart apache2
        
  4. Test PHP Installation

    Create a test PHP file in the Apache document root directory:

    sudo nano /var/www/html/info.php
        

    Add the following code to the file:

    <?php phpinfo(); ?>
        

    Save the file and visit http://your-server-ip/info.php in your web browser. If you see the PHP information page, your PHP installation is successful.

Section 2: Node.js Website Hosting

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, allowing developers to run JavaScript on the server-side. Here's how you can set up a dedicated server to host Node.js websites:

  1. Install Node.js

    First, you'll need to install Node.js on your server. Run the following commands to install Node.js from the official repository:

    sudo apt-get install curl
    curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
    sudo apt-get install -y nodejs
        
  2. Create a Node.js Application

    Create a new directory for your Node.js application and initialize a new npm project:

    mkdir my-node-app
    cd my-node-app
    npm init -y
        

    Install any required dependencies for your application using npm.

  3. Configure a Process Manager

    To keep your Node.js application running, you'll need a process manager like PM2. Install PM2 globally using npm:

    sudo npm install -g pm2
        

    Once PM2 is installed, you can start your Node.js application with the following command:

    pm2 start app.js
    

    Replace app.js with the entry point of your Node.js application.

  4. Set up a Reverse Proxy

    To serve your Node.js application over the web, you'll need to set up a reverse proxy. We'll use Nginx as an example:

    sudo apt-get install nginx
        

    Configure Nginx to forward requests to your Node.js application by editing the default Nginx configuration file:

    sudo nano /etc/nginx/sites-available/default
        

    Add the following server block to the file:

    server {
        listen 80;
        server_name your-domain.com;location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }}
    

    Replace your-domain.com with your actual domain name, and 3000 with the port your Node.js application is running on.

    Save the changes, restart Nginx, and your Node.js application should now be accessible over the web.

By following these steps, you'll have a Linux-based dedicated server set up and ready to host both PHP websites and Node.js websites. Remember to adjust the configurations and commands based on your specific requirements and server environment.