Deploying an Application Using Apache as a Web Server

Deploying an application using Apache as a web server is a fundamental skill for web developers and system administrators. Apache, an open-source HTTP server, is renowned for its robustness, flexibility, and widespread use. This blog will guide you thr…


This content originally appeared on DEV Community and was authored by Aaditya Kediyal

Deploying an application using Apache as a web server is a fundamental skill for web developers and system administrators. Apache, an open-source HTTP server, is renowned for its robustness, flexibility, and widespread use. This blog will guide you through the steps to deploy a web application using Apache, with relevant code snippets to ensure a smooth deployment process.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Installing Apache
  4. Configuring Apache
  5. Deploying a Static Website
  6. Deploying a Dynamic Website (PHP Application)
  7. Security Considerations
  8. Conclusion

1. Introduction

Apache HTTP Server, commonly referred to as Apache, is a free and open-source web server that delivers web content through the internet. By following this guide, you will learn how to install and configure Apache on a Linux server and deploy both static and dynamic web applications.

2. Prerequisites

Before we begin, ensure you have the following:

  • A server running a Linux distribution (e.g., Ubuntu, CentOS).
  • Root or sudo access to the server.
  • Basic knowledge of the Linux command line.
  • Your application ready for deployment.

3. Installing Apache

To start, we need to install Apache on our server. The installation process varies slightly depending on the Linux distribution you're using. Here, we'll cover the installation for both Ubuntu and CentOS.

On Ubuntu

  1. Update the package index:

    sudo apt update
    
  2. Install Apache:

    sudo apt install apache2
    
  3. Start and enable Apache to run on boot:

    sudo systemctl start apache2
    sudo systemctl enable apache2
    

On CentOS

  1. Update the package index:

    sudo yum update
    
  2. Install Apache:

    sudo yum install httpd
    
  3. Start and enable Apache to run on boot:

    sudo systemctl start httpd
    sudo systemctl enable httpd
    

After installation, you can verify Apache is running by visiting your server's IP address in a web browser. You should see the Apache default welcome page.

4. Configuring Apache

Apache configuration files are typically located in the /etc/apache2 directory on Ubuntu and /etc/httpd on CentOS. The main configuration file is httpd.conf or apache2.conf.

Configuring a Virtual Host

Virtual Hosts allow you to host multiple websites on a single server. Here’s how to set up a virtual host:

  1. Create a directory for your website:

    sudo mkdir -p /var/www/yourdomain.com/public_html
    
  2. Set permissions for the directory:

    sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html
    sudo chmod -R 755 /var/www/yourdomain.com
    
  3. Create a sample index.html file:

    echo "<html><body><h1>Welcome to YourDomain.com!</h1></body></html>" > /var/www/yourdomain.com/public_html/index.html
    
  4. Create a virtual host configuration file:

    sudo nano /etc/apache2/sites-available/yourdomain.com.conf
    

    Add the following content:

    <VirtualHost *:80>
        ServerAdmin webmaster@yourdomain.com
        ServerName yourdomain.com
        ServerAlias www.yourdomain.com
        DocumentRoot /var/www/yourdomain.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  5. Enable the new virtual host:

    sudo a2ensite yourdomain.com.conf
    sudo systemctl restart apache2
    

On CentOS, the steps are similar, but the directory paths differ slightly. For example, the sites-available directory does not exist by default, so you need to create it.

5. Deploying a Static Website

Deploying a static website is straightforward. Simply place your HTML, CSS, and JavaScript files in the DocumentRoot directory defined in your virtual host configuration.

For example, if your virtual host configuration points to /var/www/yourdomain.com/public_html, ensure all your static files are in this directory. Apache will serve these files directly to clients.

6. Deploying a Dynamic Website (PHP Application)

To deploy a PHP application, you need to install PHP and configure Apache to handle PHP files.

Installing PHP

On Ubuntu:

sudo apt install php libapache2-mod-php

On CentOS:

sudo yum install php php-mysql

Restart Apache to apply the changes:

sudo systemctl restart apache2   # On Ubuntu
sudo systemctl restart httpd     # On CentOS

Configuring Apache to Handle PHP

Apache is already configured to handle PHP files after installing libapache2-mod-php (on Ubuntu) or php (on CentOS). Place your PHP application files in the DocumentRoot directory.

For example, if your PHP application is named index.php, place it in /var/www/yourdomain.com/public_html/index.php.

Testing Your PHP Application

Create a simple PHP file to test the configuration:

echo "<?php phpinfo(); ?>" > /var/www/yourdomain.com/public_html/info.php

Visit http://yourdomain.com/info.php in your web browser. If PHP is configured correctly, you will see the PHP information page.

Deploying Your PHP Application

Upload your entire PHP application to the DocumentRoot directory. Ensure your application files and directories have the correct permissions:

sudo chown -R www-data:www-data /var/www/yourdomain.com/public_html
sudo chmod -R 755 /var/www/yourdomain.com/public_html

Configuring MySQL (Optional)

If your PHP application requires a MySQL database, you need to install and configure MySQL.

On Ubuntu:

sudo apt install mysql-server

On CentOS:

sudo yum install mariadb-server
sudo systemctl start mariadb
sudo systemctl enable mariadb

Secure the MySQL installation:

sudo mysql_secure_installation

Create a database and user for your application:

sudo mysql -u root -p

Inside the MySQL shell:

CREATE DATABASE yourdatabase;
CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON yourdatabase.* TO 'youruser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Configure your PHP application to connect to the MySQL database using the database name, user, and password created above.

7. Security Considerations

Configuring Firewalls

Ensure your firewall allows HTTP and HTTPS traffic. On Ubuntu, you can use ufw:

sudo ufw allow 'Apache Full'

On CentOS, use firewalld:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Enabling SSL

For secure connections, enable SSL on your Apache server. Use Certbot to obtain and install a free SSL certificate from Let's Encrypt.

Install Certbot:
On Ubuntu:

sudo apt install certbot python3-certbot-apache

On CentOS:

sudo yum install certbot python3-certbot-apache

Obtain and install the SSL certificate:

sudo certbot --apache

Follow the prompts to configure SSL for your domain. Certbot will automatically configure Apache to use the new SSL certificate.

Securing Apache

Edit the Apache configuration file to improve security. Open /etc/apache2/apache2.conf (Ubuntu) or /etc/httpd/conf/httpd.conf (CentOS) and make the following changes:

  • Disable directory listing:

    <Directory /var/www/>
        Options -Indexes
    </Directory>
    
  • Hide Apache version and OS details:

    ServerTokens Prod
    ServerSignature Off
    
  • Limit request size to prevent DoS attacks:

    LimitRequestBody 10485760
    

Restart Apache to apply the changes:

sudo systemctl restart apache2   # On Ubuntu
sudo systemctl restart httpd     # On CentOS

8. Conclusion

Deploying an application using Apache as a web server involves installing and configuring Apache, setting up virtual hosts, and securing your server. Whether you're deploying a static website or a dynamic PHP application, Apache provides a robust and flexible platform for web hosting.

By following the steps outlined in this guide, you should be able to deploy your web application with confidence. Remember to keep your server and applications up to date with the latest security patches to protect against vulnerabilities.

Happy deploying!


This content originally appeared on DEV Community and was authored by Aaditya Kediyal


Print Share Comment Cite Upload Translate Updates
APA

Aaditya Kediyal | Sciencx (2024-06-23T18:33:59+00:00) Deploying an Application Using Apache as a Web Server. Retrieved from https://www.scien.cx/2024/06/23/deploying-an-application-using-apache-as-a-web-server/

MLA
" » Deploying an Application Using Apache as a Web Server." Aaditya Kediyal | Sciencx - Sunday June 23, 2024, https://www.scien.cx/2024/06/23/deploying-an-application-using-apache-as-a-web-server/
HARVARD
Aaditya Kediyal | Sciencx Sunday June 23, 2024 » Deploying an Application Using Apache as a Web Server., viewed ,<https://www.scien.cx/2024/06/23/deploying-an-application-using-apache-as-a-web-server/>
VANCOUVER
Aaditya Kediyal | Sciencx - » Deploying an Application Using Apache as a Web Server. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/23/deploying-an-application-using-apache-as-a-web-server/
CHICAGO
" » Deploying an Application Using Apache as a Web Server." Aaditya Kediyal | Sciencx - Accessed . https://www.scien.cx/2024/06/23/deploying-an-application-using-apache-as-a-web-server/
IEEE
" » Deploying an Application Using Apache as a Web Server." Aaditya Kediyal | Sciencx [Online]. Available: https://www.scien.cx/2024/06/23/deploying-an-application-using-apache-as-a-web-server/. [Accessed: ]
rf:citation
» Deploying an Application Using Apache as a Web Server | Aaditya Kediyal | Sciencx | https://www.scien.cx/2024/06/23/deploying-an-application-using-apache-as-a-web-server/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.