apache - internet server
- Details
- Last Updated: Wednesday, 21 July 2021 00:44
- Published: Tuesday, 08 January 2019 05:24
- Hits: 996
Setting internet server:
You can host a website on any of the computers called as servers. The hosting services are provided for a yearly fee by a lot of hosting companies as godaddy.com, ipage.com, hostinger.com to name a few. If you do not want to pay anyone money, and want to use your own computer for hosting a website, it's pretty easy too.
Before hosting a website on your personal computer in your home, you will need to configure the router. Then you need to install a server software as "apache", and then get a name assigned to your website. We will talk about installing apache in this section. In next section, we'll talk about all the other steps.
Here, I'll talk about hosting a website on a laptop running Linux OS "Linux Mint 18". These steps should work on latest Ubuntu release too.
Install apache:
First, we need to install software, that will make our regular laptop dual work as a server. This software is called apache (or httpd process, d stands for daemon or process). Here's a good link explaining the steps (steps are the same on any debian based system, as Ubuntu, Linux Mint, etc):
First, install apache. We install apache2, since it's the latest apache.
- sudo apt-get update => we could also use more modern cmd "apt" instead of "apt-get", i.e: "sudo apt update"
- sudo apt-get install apache2 => could also run "sudo apt install apache2". It will ask for password, and then intsall Apache,utilities, configuration files, etc.
Once installed, run cmds as below:
which apache2 => should show /usr/sbin/apache2 as the path
Debian/Ubuntu start Apache automatically after installation, so we do not need to start apache separately. Now, goto web browser, and type "localhost" or "127.0.0.1" in the address tab. localhost resolves to the IP address 127.0.0.1, which is the most commonly used IPv4 loopback address. This address is used to establish an IP connection to the same machine or computer being used by the end-user. So, typing this makes computer look for httpd process running on same machine and fetch default website page from there.It should show "Apache2 Ubuntu Default page" with important dir info. This page is located at /var/www/html/index.html.
apache2 dir:
/usr/sbin/apache2 => apache2 is the executable. Normally running this executable directly should start the apache2 pgm, but due to use of env var, in it's default configuration, apache2 can't be started/stopped directly by running the binary. Cmds to start/stop it are discussed later.
/usr/share/doc/apache2/README.Debian.gz => apache2 documentation
/etc/apache2/ => this dir has many important configuration files
- envvars => contains default environent variables for apache2ctl scrpt
- apache2.conf => apache2.conf is the main configuration file. It puts the pieces together by including all remaining configuration files when starting up the web server. It includes following file:
- ports.conf: determines listening ports for incoming connections which can be customized anytime. By default, port 80 is assigned to http, and port 443 to https (secure http).
- Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/ directories contain particular configuration snippets which manage modules, global configuration fragments, or virtual host configurations respectively.
- mods_enabled => This dir contains many *.load and *.conf file for each module (i.e alias.conf and alias.load for alias module, and so on). *.load file just loads the module *.so from appr dir (i.e LoadModule dir_module /usr/lib/apache2/modules/mod_dir.so). *.conf sets config for modules, and is written in xml format.
- conf_enabled => few more *.conf files for customizing
- sites_enabled => There is usually just 1 file here, 000-default.conf. This is very important file, if you plan to host more than 1 virtual host on same ip address. In that case, for each virtual host, we need to specify "ServerName", "ServerAdmin", "DocumentRoot", and log file locations.
- Configuration files that we saw above are links from dir mods-available, conf-available and sites-available. Whenever we put a soft link from *-available dir to corresponding *-enabled dir, those files get activated. For Linux Mint, these files are enabled/disabled by using helper scripts: a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf.
/var/log/apache2 => stores log files for all requests apache processes.
apache2 cmds:
Now, depending of which linux distro we have, we have bunch of cmds that are used to monitor/control apache server. Due to use of env var, calling binary apache2 directly to start/stop apache2 doesn't work.
This is a good link showing diff ways to do that:
https://www.cyberciti.biz/faq/ubuntu-linux-start-restart-stop-apache-web-server/
Most modern Linux OS use systemd linux cmds to check status of pgm. On earlier versions of linux, init process was started as 1st process, but RedHat introduced systemd to replace init. All derivatives of red hat, and other linux distro (latest versions) use systemd. "systemctl" cmd is used for systemd based systems. Ubuntu uses systemd starting from "Ubuntu 16.04 LTS" but earlier version of Ubuntu still uses init. For more details, see in "init vs systemd" link in "linux intro" section.
Init for apache2:
On Linux Mint 19, I see apache2 files getting installed in /etc/init.d. Basically installation of Apache essentially runs this cmd during some point of installation:
sudo update-rc.d apache2 defaults
=> This creates the appropriate symlinks in the /etc/rc*.d/
folders. Ubuntu uses scripts in the /etc/init.d/
folder to start/stop services.For apache2 script, we see that they have links starting with S in rc2.d, rc3.d,rc4.d and rc5.d, but have links starting with K in rc0.d, rc1.d and no link for rcS.d. Since runlevel for ubuntu is 2, all links in rc2.d that start with S get started, so apache2 gets started by default anytime computer boots up (since it's link starts with S).
If we want to disable apache2 on startup, we can run this cmd:
sudo update-rc.d apache2 disable
which removes all the "S" symlinks and replaces them with "K" symlinks
to re-enable Apache: sudo update-rc.d apache2 enable
ex: /etc/init.d/apache2 status => Here, apache2 process init script is called to check status of apache2
systemd for apache2:
We'll use "service" or "apache2ctl" cmds, as they work on all linux distro. apache2ctl is apache server ctl i/f cmd, so it ships with apache, and will work anywhere apache is installed. However, on linux mint 19, apach2ctl is not installed by default, when installing apache2, so we'll stick with service cmd. Currently, apach2ctl uses /etc/init.d/apache2 script, but in future, it may use native systemd unit file.
Find if apache2 is running: (similarly we can use start/stop to start/stop apache server. However start./stop requires root privileges, so need to use sudo before each cmd)
service apache2 status => displays "* apache2 is active (running)". shows detailed o/p on linux mint
/etc/init.d/apache2 status => same o/p as above
systemctl status apache2.service => same o/p as above, for systemd based systems
Customizing website:
We saw above that index.html file is the one fetched to display the webpage for your website. It's located at /var/www/html/index.html. It has default ubuntu info. We can move this file to a backup file (index.back.html), and start from scratch with a new index.html file, and put very simple html code in it.