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
Introduction
Prerequisites
Installing Apache
Configuring Apache
Deploying a Static Website
Deploying a Dynamic Website (PHP Application)
Security Considerations
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
Update the package index:
sudo apt update
Install Apache:
sudo apt install apache2
Start and enable Apache to run on boot:
sudo systemctl start apache2 sudo systemctl enable apache2
On CentOS
Update the package index:
sudo yum update
Install Apache:
sudo yum install httpd
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:
Create a directory for your website:
sudo mkdir -p /var/www/yourdomain.com/public_html
Set permissions for the directory:
sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html sudo chmod -R 755 /var/www/yourdomain.com
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
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>
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!