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.
Also read,
Apache server setup in windows 10
Install apacher server via command line
- Open cmd. Type
sudo -s
and enter superuser password. - 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. - 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. - To stop the server type
service apache2 stop
.
There is an alternative command to start server also,
Type
Open any browser and type localhost. You will see the default home page of the server running.
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.
Type
apachectl -k start.
Open any browser and type localhost. You will see the default home page of the server running.
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.
- Default installation path of apache server, which is
var/local/apache2
. - 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
- 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
- Open cmd and go to the directory
/var/www/html
by typingcd /var/www/html
. - Create a new folder. Type
mkdir myfirstwebsite
and hit enter. A new directory will be created undervar/www/html/myfirstwebsite
. - Now go to that directory type
cd myfirstwebsite
- Create a file with name index.html. Type
gedit index.html
, a text editor will open. - Type these into that index.html(do not change this name) file and save.
- 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.
<html>
<head>
<title> My first Website <title>
</head>
<body>
<h1> This is a test webpage</h1>
</body>
</html>
Whether we type
But if we want to host only one website then we can replace the default content of
To check, server is running or not, open your browser and type "localhost" in the address bar. You should see something like this,
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.
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.
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.