In this tutorial, I will explain about the world's most popular web server software Apache and how to install Apache in Ubuntu. This post will cover all bits and pieces, that you need to understand Apache HTTP software.
Additionally, I will also cover the WordPress installation step by step. After reading this post, you will be able to install Apache with WordPress locally. Also, you can publish your life's first web post.
Table of Contents
- 1 What is Apache (pronunciation)
- 2 Pre-requisite to install Apache in ubuntu
- 3 Apache download
- 4 How to Install Apache in Ubuntu
- 5 Configure your firewall and enable port 80 for Apache
- 6 How to start, stop, restart and reload Apache in ubuntu
- 7 Apache configuration files and directories
- 8 How to host a website in Apache server on Ubuntu
- 9 How to Secure Apache server
- 10 How to install WordPress on Ubuntu
- 11 Uninstall apache2 on Ubuntu
- 12 Video Tutorial
- 13 Conclusion -
What is Apache (pronunciation)
Apache (uh·pa·chee) is an open-source web server software. It has been taken from native American tribe "Apache", famous for its skills in warfare and strategy.
its official name is Apache HTTP server and managed by Apache software foundation. As per stats, it's powering around >40% of the website around the globe.
It is one of the old and most reliable web server software. Which started in 1995 and still helping people to serve web content. So whenever you type any domain name in your browser, the request reaches to the webserver. As a result, you see the content.
Approximately all the giant web hosting companies use Apache. For example, like Bluehost and Godaddy. Apache is a cross-platform software, so it supports both Windows and Linux.
You can use apache webserver with different programming languages like Python, Perl and PHP etc. In most cases, you just need to enable apache modules like mod_python, mod_perl and mod_php etc.
Apache uses thread-based architecture. So performance may get impact sometimes, in case of heavy traffic.
There are few other web server software that exists in the market like Nginx and Tomcat. But Apache is still one of the most reliable and flexible software due to its module-based structure. We will discuss other web servers on some other day. Today's post is all about Apache.
Apache can be one of the marvellous choices to run your web services but it also comes with pros and cons.
- Open source and free for commercial use
- Tested and reliable software
- Flexible due to module-based structure
- Frequent security patches and updates
- Very easy to configure and works with Windows and Linux
- Works well with many known C-panels, like WordPress
- A large community and easy support available.
- Known performance problems due to the heavy traffic (c10k problem)
- Contains a lot of configuration options, that can lead to security vulnerabilities
Pre-requisite to install Apache in ubuntu
- A system with Ubuntu 16.04, 18.04 or 20.04
- sudo or root access
- Internet access
- Apache bundle or binaries for Ubuntu
Your favourite Apache can be downloaded as a package, binaries or source bundle. So, using source file provides flexibility to customize the installation and also it's platform-independent.
If you love simplicity, then for Ubuntu you can install it from apt repository. No manual download required. While writing this post, Apache HTTP Server 2.4.46 is the latest version available.
How to Install Apache in Ubuntu
Steps to install Apache using Binary package
One of the easiest method to install any software on Ubuntu is using apt command.
Open a terminal in your Ubuntu. <Right click> on desktop and select Terminal or type Ctrl + T.
Run sudo apt-get update command and let the updates complete.
[root@server ~] sudo apt-get update
Install Apache on Ubuntu using apt command.
[root@server ~] sudo apt install apache2
Verify your Apache installation by typing "http://your-ip-address" in your favourite browser. Then, you will see the default page of the Apache2 Ubuntu default page. For example, my IP is "http://184.108.40.206". This default page validates, that your installation is perfectly fine.
Configure your firewall and enable port 80 for Apache
To enable port 80 in your system, follow these commands one by one
[root@server ~] sudo ufw allow 'Apache'
[root@server ~] sudo ufw status
How to start, stop, restart and reload Apache in ubuntu
Now you have installed Apache and verified, whether it's functioning fine or not. So, it's time to learn about Apache service controls.
*Start Apache service
[root@server ~] sudo systemctl start apache2.service
*Stop Apache service
[root@server ~] sudo systemctl stop apache2.service
*Restart Apache service
[root@server ~] sudo systemctl restart apache2.service
*Reload Apache service
Start, stop and restart service include service disruption. But, if you have made changes to Apache2.conf and want to refresh service without disruption. Then reload is the saviour.
[root@server ~] sudo systemctl reload apache2.service
*Check Apache service status
[root@server ~] sudo systemctl status apache2.service
Apache configuration files and directories
Now, when you know how to install Apache and handle services. Few more things related to the Apache file and directory structure, you must know.
List of Important directories and files
There is one default document root directory "/var/www/html", created by Apache. You can also create subdirectories inside this, if you have more than one website to host.
This directory will help you to publish your content over the Web. For example, all posts, pages, plugins and even your CMS like WordPress will be inside this root directory.
Below mentioned are the important directories and files of Apache -
- /var/log/apache2/ - All logs can be found inside this Apache directory.
- /etc/apache2/ - Contains all configuration files of Apache.
- /etc/apache2/sites-available - This directory works as storage for the virtual host file. For example, any website hosted will have one virtual record.
- /etc/apache2/sites-enabled - Directory holds records of the website, those are ready to server clients.
- /var/log/apache2/error.log - contains all error logs inside this file.
- /var/log/apache2/access.log - All client-related access will be logged into this file.
- /etc/apache2/apache2.conf - This file can be treated as brain and heart of Apache. Basically, it consists of all configuration parameter and controls. Any change in this file will impact your website.
- /etc/apache2/ports.conf - You can provide your customized port into this file. In case, you are not happy with the default HTTP port (80) or to secure your site.
Checkout for more information on apache files and directories for ubuntu.
How to host a website in Apache server on Ubuntu
So, we have installed and verified Apache installation. So, now time to create your own first website inside it.
Let's see steps involved in hosting a static website manually. In this example, I will host my website as "WWW.test.example.com".
Step1 - Create your own directory test inside /var/www/ directory.
[root@server ~] sudo mkdir /var/www/test/
Step2 - Create your index.html file inside it. I have used vi, you can use your favorite editor.
[root@server ~] cd /var/www/test/ [root@server ~] sudo vi index.html
Step3 - Paste this code in your index.html file.
<html> <head> <title> Apache Test website </title> </head> <body> <p> This is my first test website </p> </body> </html>
Step4 - Setup "virtual host configuration" file
Got to site-available directory and copy default file with your site name. For example, i have copied 000-default.conf file to test.conf.
[root@server ~] cd /etc/apache2/sites-available/ [root@server ~] sudo cp 000-default.conf test.conf [root@server ~] sudo mv 000-default.conf 000-default.conf.orig
Edit virtual host configuration file test.conf with your favourite editor.
[root@server ~] sudo vi test.conf
Change below mentioned parameters inside the conf file
- Provide your email id to get the notification, In case Apache generates an error.
- Point Document root directory to your website directory.
- Add the "ServerName" directive, as default file doesn't have it.
- Activate virtual host to enable your configuration
[root@server ~] sudo a2ensite test.conf
- You will see the following output
Enabling site test.
To activate the new configuration, you need to run: service apache2 reload
- Reload apache service and type your IP or hostname in browser "http://220.127.116.11/". Hope you have hosted your first website.
[root@server ~] service apache2 reload
How to Secure Apache server
Security is the most important concern in today's internet world. Especially, when it's your website. Hacker always tries to grab low hanging fruits. So, follow these steps to save your website from those hackers.
System or Server level security
- Create sudo user.
- Disable root login.
- Update to newest patches always.
- Install fail2ban software.
- Setup Ubuntu firewall properly.
- Encrypt swap, if you like (optional).
Apache level security
- Hide Apache and OS version.
- Disable directory listing.
- Disable unnecessary modules.
- Restrict access to files and directories outside the webroot directory.
- Enable mod_evasive to screw Dos attack.
- Activate mode_security to enhance Apache security.
- Restrict request size.
- Redirect HTTP to https
How to install WordPress on Ubuntu
WordPress is an opensource and free content management system. It is the most popular way to create your website or blog. Also, It's based on PHP and MySQL. Additionally, no manual hassles 🙂
WordPress features can be extended with a number of plugins and themes. Also, migration from one domain to another is also easy, if you use WordPress. Let me show you, how you can install WordPress locally on Ubuntu for your Apache server to host your website or blog.
Step one - Install WordPress locally
To install WordPress on Ubuntu, Run below mentioned commands -
[root@server ~] sudo apt update [root@server ~] sudo apt install wordpress php libapache2-mod-php mysql-server php-mysql
Step Two - Create an Apache site for WordPress
Edit and Insert following lines to /etc/apache2/sites-available/wordpress.
conf. Because this file doesn't exist by default, you have to make one.
[root@server ~] cd /etc/apache2/sites-available/ [root@server ~] sudo vi wordpress.conf
#Add these lines in wordpress.conf Alias /blog /usr/share/wordpress <Directory /usr/share/wordpress> Options FollowSymLinks AllowOverride Limit Options FileInfo DirectoryIndex index.php Order allow,deny Allow from all </Directory> <Directory /usr/share/wordpress/wp-content> Options FollowSymLinks Order allow,deny Allow from all </Directory>
Activate WordPress site
sudo a2ensite wordpress
Enable URL rewriting and reload apache2 service.
sudo a2enmod rewrite
sudo service apache2 reload
As we discussed earlier also, WordPress required MySQL also.
Step Three - Configure MySQL Database
Let's create MySQL database -
Below mentioned MySQL syntax is for MYSQL 8 or greater. In this example, Password "test1234" has been set for the WordPress user. You can set your own.
sudo mysql -u root # Once you get mysql prompt, Run create, grant, Flush and quit command as follows - mysql> CREATE DATABASE wordpress;
mysql> create user 'wordpress'@'localhost' IDENTIFIED BY 'test1234';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON wordpress.* TO 'wordpress'@'localhost'; mysql> FLUSH PRIVILEGES; mysql> quit
Now, configure "/
etc/wordpress/config-localhost.php file to link MySQL DB "WordPress" created above. In this example, I have created the config-localhost.php file, as it doesn't exist by default.
<?php define('DB_NAME', 'wordpress'); define('DB_USER', 'wordpress'); define('DB_PASSWORD', 'test1234'); define('DB_HOST', 'localhost'); define('DB_COLLATE', 'utf8_general_ci'); define('WP_CONTENT_DIR', '/usr/share/wordpress/wp-content'); ?>
Time to start MySQL services -
sudo service mysql start
Step Four - Configure WordPress in Ubuntu manually
Open "localhost/blog" in your browser and provide username, password, title of your site and email address.
Once done, you can now Login to your WordPress by opening "
localhost/blog/wp-login.php" on your browser.
Enjoy!! your WordPress Dashboard and write your first blog. Then, publish and check out your post with "http://localhost/blog".
Uninstall apache2 on Ubuntu
Due to any reason, if you want to uninstall Apache from your server. Then, I won't let you scratch your head. Follow this sequence of commands to clean it completely.
- Stop Apache service
[root@server ~] sudo service apache2 stop
- Remove Apache
[root@server ~] sudo apt remove apache2
- Purge Apache to remove dependent packages also.
[root@server ~] sudo apt purge apache2
Remove directories /var/www and /etc/apache2 manually, if still exists due to custom files inside it.
If you are facing difficulty to understand steps, watch out this video on "How To Install Apache in Ubuntu and WordPress locally".
I have tried to cover the complete life cycle of Apache in this tutorial. Starting from What is Apache, installation of Apache, security, WordPress installation and Apache un-installation.
I hope, now you can easily host your first website on Apache with WordPress in Ubuntu. Let me know, your thoughts by sharing your comments.
See you soon in the next post!.