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:
- Open Nginx config for your Java application domain:
sudo nano /etc/nginx/sites-available/linuxcloudservers_java - 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; } } - 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:
- Create a directory for HTML files:
sudo mkdir -p /var/www/linuxcloudservers_html - Copy your HTML files to this directory:
sudo cp index.html /var/www/linuxcloudservers_html/ - Configure Nginx for the HTML site:
sudo nano /etc/nginx/sites-available/linuxcloudservers_html - Add this configuration:
server { listen 80; listen [::]:80; server_name linuxcloudservers.com www.linuxcloudservers.com; root /var/www/linuxcloudservers_html; index index.html; } - 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:
- Install PHP and required extensions:
sudo apt install php-fpm php-mysql -y - Create a directory for the PHP application:
sudo mkdir -p /var/www/linuxcloudservers_php - Copy PHP files to the directory:
sudo cp index.php /var/www/linuxcloudservers_php/ - Configure Nginx for the PHP site:
sudo nano /etc/nginx/sites-available/linuxcloudservers_php - 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; } } - 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:
- 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; } } - 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.
