Blog by Debabratta Jena
Apache server setup in Ubuntu 20.04 LTS

Apache server configuration in Ubuntu 20.04 LTS

Last modified on Nov 22, 2020

Why Apache on Ubuntu?



A web server is a kind of software which handles all http request on the web. There are several web software available on the market like Apache, Nginx, MS IIS etc. Apache http server is the most used server on the internet. It handles http request, so it is called http server. It can be run on Linux as well as Windows machine. I am giving here a detail guide on how to install and configure apache http server on Ubuntu 20.04 LTS.

Install apacher server via command line

  1. Open cmd. Type sudo -s and enter superuser password.
  2. run sudo -s on ubuntu cmd
  3. Type apt install apache2 in the cmd. It will install apache http server for you. If you dont see any error, apache server is installed successfuly on your machine. Now we start the apache server.
  4. Apache server installation in ubuntu debian via command line Dj Techblog
  5. Type service apache2 start in the command prompt. If it doesnt show any error that means server is started, running and ready to accept client request.
  6. Apache server startup in ubuntu debian via command line Dj Techblog
  7. To stop the server type service apache2 stop.
  8. Apache server shutdown in ubuntu debian via command line Dj Techblog
There is an alternative command to start server also,

Type apachectl -k start.

Open any browser and type localhost. You will see the default home page of the server running.
apache server homepage after setup on ubuntu 20.04


Since, apache server is servicing request now. We need to configure the server according to our need.

To configure the apache server, we need to know three things.

  1. Default installation path of apache server, which is var/local/apache2.

  2. What default location, the server looks for when any request came from client, this is the home directory for all projects files. The default location is /var/www/html

  3. All server configuration files are found at /etc/apache2. The main configuration file is /etc/apache2/apache2.conf, which loads all other configuration files.


Host your website on apache server in Ubuntu 20.04 server



  1. Open cmd and go to the directory /var/www/html by typing cd /var/www/html.

  2. Create a new folder. Type mkdir myfirstwebsite and hit enter. A new directory will be created under var/www/html/myfirstwebsite.

  3. Now go to that directory type cd myfirstwebsite

  4. Create a file with name index.html. Type gedit index.html, a text editor will open.

  5. Type these into that index.html(do not change this name) file and save.

  6. <html>
    <head>
    <title> My first Website <title>
    </head>
    <body>
    <h1> This is a test webpage</h1>
    </body>
    </html>
  7. Now go to browser and type localhost/myfirstwebsite. You will be able to see the content of index.html file. The index.html file is the default file which is served when any directory is requested by the client.
Whether we type localhost/myfirstwebsite/index.html or localhost/myfirstwebsite we will see the same page, that is index.html. This way we can host as many websites by creating different directory for multiple websites under /var/www/html.

But if we want to host only one website then we can replace the default content of /var/www/html/index.html by our version of index.html file. Type localhost in the browser and we will see the content of our home page.
To check, server is running or not, open your browser and type "localhost" in the address bar. You should see something like this,

The apache configuration file : apache2.conf



This is the main Apache server configuration file. It is supposed to contain the configuration directives that give the server its instructions. It is read by the server every time it starts. In debian, apache2.conf file does not contain any configuration as such. It only loads or inserts the different pieces of configuration files into itself. See Apache website for detailed documentation.

The default port address of apache server is 80. When you type localhost in browser it is actually treated as localhost:80. Generally we need not change this port number, but if you want to chnage it you need to edit two configuration files, /etc/apache2/ports.conf and /etc/apache2/sites-enabled/000-default.conf.

ports.conf



In the ports.conf file, search for the line Listen 80, and change it to a number say 8080. Now goto 000-default.conf. The first line in debain version of apache server installation starts with <virtualHost *:80>, change it to 8080. Now save both the files and restart the server to let changes come into effect. You need to have super user control in Ubuntu to change these files.
apache server default.conf on ubuntu 20.04

000-default.conf



Be sure while changing the the port number if you assign any port number, which is already assigned to any other service. A conflict will occur and apache server will not start.