Setting Up Virtual Hosts for Java, HTML, and PHP Applications with Nginx

Introduction

This guide will walk you through configuring virtual hosts on a Linux cloud server to run different types of applications (Java, HTML, and PHP) on Nginx. It includes installing Nginx, setting up free SSL with Let’s Encrypt, and configuring reverse proxying to applications running on port 5000 to be accessible over port 443.


1. Installing Nginx and Setting Up SSL

1.1 Nginx Installation

To install Nginx on your Linux server, run the following commands:


sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
            

1.2 Setting Up Free SSL with Let’s Encrypt

Let’s Encrypt offers free SSL certificates. Use Certbot to install SSL and configure automatic renewal:


sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d linuxcloudservers.com -d www.linuxcloudservers.com
            

This setup automatically configures SSL for your Nginx server on port 443 and sets up renewal for the certificate.


2. Virtual Host Configurations for Different Application Types

2.1 Configuring a Virtual Host for a Java Application

To set up a Java application running on port 5000:

  1. Open Nginx config for your Java application domain:
    sudo nano /etc/nginx/sites-available/linuxcloudservers_java
  2. Add the following configuration:
    
    server {
        listen 80;
        listen [::]:80;
        server_name linuxcloudservers.com www.linuxcloudservers.com;
    
        location / {
            proxy_pass http://localhost:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
                        
  3. Enable the config and reload Nginx:
    
    sudo ln -s /etc/nginx/sites-available/linuxcloudservers_java /etc/nginx/sites-enabled/
    sudo systemctl reload nginx
                        

2.2 Configuring a Virtual Host for a Static HTML Page

For a static HTML page:

  1. Create a directory for HTML files:
    sudo mkdir -p /var/www/linuxcloudservers_html
  2. Copy your HTML files to this directory:
    sudo cp index.html /var/www/linuxcloudservers_html/
  3. Configure Nginx for the HTML site:
    sudo nano /etc/nginx/sites-available/linuxcloudservers_html
  4. Add this configuration:
    
    server {
        listen 80;
        listen [::]:80;
        server_name linuxcloudservers.com www.linuxcloudservers.com;
    
        root /var/www/linuxcloudservers_html;
        index index.html;
    }
                        
  5. Enable the configuration and reload Nginx:
    
    sudo ln -s /etc/nginx/sites-available/linuxcloudservers_html /etc/nginx/sites-enabled/
    sudo systemctl reload nginx
                        

2.3 Configuring a Virtual Host for a PHP Application

For a PHP-based application:

  1. Install PHP and required extensions:
    sudo apt install php-fpm php-mysql -y
  2. Create a directory for the PHP application:
    sudo mkdir -p /var/www/linuxcloudservers_php
  3. Copy PHP files to the directory:
    sudo cp index.php /var/www/linuxcloudservers_php/
  4. Configure Nginx for the PHP site:
    sudo nano /etc/nginx/sites-available/linuxcloudservers_php
  5. Add this configuration:
    
    server {
        listen 80;
        listen [::]:80;
        server_name linuxcloudservers.com www.linuxcloudservers.com;
    
        root /var/www/linuxcloudservers_php;
        index index.php;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php-fpm.sock;
        }
    }
                        
  6. Enable the configuration and reload Nginx:
    
    sudo ln -s /etc/nginx/sites-available/linuxcloudservers_php /etc/nginx/sites-enabled/
    sudo systemctl reload nginx
                        

3. Setting Up Reverse Proxy and SSL

To reverse proxy backend applications on port 5000 to port 443 with SSL:

  1. Update each virtual host to listen on port 443 and add SSL settings. Here’s an example for the Java application:
    
    server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name linuxcloudservers.com www.linuxcloudservers.com;
    
        ssl_certificate /etc/letsencrypt/live/linuxcloudservers.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/linuxcloudservers.com/privkey.pem;
    
        location / {
            proxy_pass http://localhost:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
                        
  2. Test and reload Nginx after updating configurations:
    
    sudo nginx -t
    sudo systemctl reload nginx
                        

Conclusion

With these steps, you’ll have Nginx configured to serve a Java application, an HTML page, and a PHP application with reverse proxying and SSL encryption. Each configuration is tailored for applications running on port 5000, allowing them to be securely accessed over port 443.

Related articles

Blockchain and Cloud Computing: Empowering the Tech Revolution

Blockchain and Cloud Computing: Empowering the Tech Revolution Blockchain is a groundbreaking technology that acts as a decentralized and...

AWS Step Functions for Workflow Orchestration

⚙️AWS Step Functions for Workflow Orchestration Modern applications often require complex workflows involving multiple services, APIs, and processes. Managing...

Update Kubernetes ConfigMaps

Update Kubernetes ConfigMaps Kubernetes ConfigMaps allow you to manage configuration data for your applications outside the code. Automating the...

Operating System Based Virtualization

Operating System Based Virtualization Introduction Operating system-based virtualization, commonly known as containerization, is a technology that allows multiple isolated user-space...