In the last post we saw how to install Apache web server in Red-Hat distributions. In this post, we’ll see how to install and configure Apache web server in Ubuntu or any Debian distribution.
Apache in Debian is way different from Apache in Red-Hat systems. In any Red-Hat distribution, to configure a web server, all you need to edit is httpd file, and the hosts file so browser can get the webpage locally. But in Debian distributions, you’ll be given a default settings, we need to make a copy of that file and configure the copy, not the original file. Then enable the site and edit the hosts file.
Lets see how to configure Apache web server in Debian distributions.
sudo apt-get install apache2 -y
Install Apache2 web server. Default location of Apache is ‘/etc/apache2‘sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/yoda
Copy the default file provided by Ubuntu, in thesamelocation. Save it with a name that you want to give to you website. We’lluseyoda in this case.sudo vim /etc/apache2/sites-available/yoda
Open the configuration file with your favourite editor.- This is the default file, without any changes.
- Add the following lines:
ServerAdmin [email address of the admin]
ServerName www.[your-website-name].com
ServerAlias *.[your-website-name].com
DocumentRoot [path of the directory containing all the files]
Save and Quit.ServerAlias is necessary because it will return the webpage even when user doesn’t put www. For eg. the server will respond to ‘http://www.yoda.com‘ or ‘http://yoda.com‘ mkdir -p /var/www/html/[your-website-name]
If the DocumentRoot doesn’t exist, create it. We created ‘html‘ folder in ‘www‘ and then ‘yoda‘ (name of the website) folder in ‘html‘ using one command. If you alrready have your document root, skip this step.sudo vim /var/www/html/yoda/index.html
Create a home page for your website. Remember, if you want to put anything on the website, it should be present in ‘/var/www/html/yoda‘ because it is the document root. Apache will look only in this folders for all html, php image, css and other files.- Create a simple home page.
ifconfig
Run ‘ifconfig‘ to check your machine’s IP address. Note it down somewhere, because we will need it in the next step.sudo vim /etc/hosts
Open the hosts file. When you enter any web address in browser, it’ll first check the browser’s cache. If it doesn’t find the IP address of website, it goes to the hosts page. We need to add ourwebsite’surl here along with our IP address, to tell the browser the website resides in our machine.- www.[your-website-name].com [Your IP-address]
Edit the hosts file, enter the name of your website and your IP-address (Step 9). sudo a2ensite [your-website-name]
Enable your website using ‘a2ensite‘ command. Even if you do all the steps and skip this simple command, apache won’t reply to your request.sudo service apache2 restart
Restart apache 2 server.- Enter the website’s address in the URL.