What is a web server, and how does it work?
The popularization of the Internet in the 1990s was the beginning of an explosive growth of websites, hosted by countless individuals and businesses (hosts) around the world. Now that the Internet is ubiquitous, you might not even think about how or where this information is stored and hosted. What is a web server, anyway?
The short answer is that a web server stores all of the information, processes it, and sends it to you using various Internet-related protocols once you request a particular piece of information.
Let’s take a look at what, exactly, a web server is and how it works. After that, I’ll show you how to set up a Linux+Apache+MySQL+PHP (LAMP) stack in Ubuntu 16.04 and CentOS 6/7.
A web server is a program that serves files or web pages using the
HTTP (Hypertext Transfer Protocol) protocol in response to a request from a user via an
HTTP client, such as a web browser.
The web server typically runs all the time to serve a client’s request in various outputs like
XML, which are otherwise known as web pages. Before serving web pages to the client, it needs to put collect some other resources, like
The most widely-used web servers are Apache, IIS from Microsoft and Nginx (pronounced engine-x).
Celebrate 8 years of SSD hosting with incredible deals!
We can’t believe it, but it’s been 8 years since we here at SSD Nodes launched our all-SSD VPS cloud. To celebrate, we’re offering amazing deals on all of our most popular plans– like a 16GB RAM SSD VPS loaded with resources for as little as $7.99/month!
Before we try to explore the way a web server operates, let’s try to understand the term URL. A URL (Uniform Resource Locator) is a unique address for a file that’s stored on a web server that’s connected to the Internet. The file may be an
HTML file, a
CSS file, an image file, or any other file related to a web page, and is referred to as a resource for a web server. An URL comprises of following structure:
- The protocol (
https) used to access a resource. Want to know more about HTTPS?
- An IP address or domain name to identify the web server.
- A port number on which the web server is running. The default port number is 80, and on that case, there is no need to specify the port number in the URL.
- The path of the resource in the web server.
When you type a URL into your web browser, the web server that you refer to in the URL responds with the requested page. In the background, web browser breaks the URL in the three parts: the protocol, the IP address or domain name, and the resource path in the web server.
If you specified an IP address in the URL, the web browser will directly establish a connection with the web server using the
HTTP protocol through the default port number 80 and request the web page. The web server then finds the requested document and sends back to the web browser, also through
HTTP. In case the web server can’t find the requested page/document, the web server responds with a 404 status code to inform the web browser that the requested material is not available.
If you specified a domain name instead of an IP address in the URL, then the web browser will convert the domain name—such as
https://blog.ssdnodes.com/— to an IP address first. The web browser will contact a DNS (domain name system) server to translate the domain name to an IP address. Once the DNS server responds with the IP address of the domain name, the web browser will fetch the web page just like before.
Michael Kunze initially coined the term LAMP in an article for the German computing magazine Computertechnik in December 1998. A LAMP stack is a framework for developing a web application or dynamic websites using Linux as the operating system, Apache as the Web server, MySQL/MariaDB as the database and PHP as the scripting language.
P was traditionally meant to stand for PHP, but over time both Perl and Python have been added to the list of possibilities. Depending on requirements for a particular application, sometimes Perl or Python is substituted for PHP. Each component of a LAMP stack is open source and freely available to anyone who wishes to use this stack to develop and deploy their web application or a dynamic website.
Let’s walk through each component of a LAMP stack.
Linux is an open-source and free operating system kernel written by Linus Torvalds when he was a student in Finland. Technically, Linux is just the kernel. The other open source system libraries, software, and utility programs are packaged to run above the kernel to make it a complete operating system.
Linux is a highly reliable and secure operating system preferred all over the world by universities, government organizations, small and large business organizations, and even for personal use. There are a wide variety of Linux distributions available to choose from—Red Hat, CentOS, Ubuntu, and Debian, to name few.
Apache is a free web server distributed by the Apache Software Foundation and is widely used in all Linux flavors. Apache is an HTTP server that is fast and secure and powers more than 50 percent of all web servers around the world. You can deploy Apache to everything from small static websites to large web applications with thousands or even millions of daily users.
MySQL is a free, relational database management system licensed under GNU GPL. It is not fully open source but instead released by Oracle under a dual GPL and commercial license. If you like to use an open source database management system, then choose MariaDB, which was forked by some developers of MySQL due to concerns over its acquisition by Oracle Corporation. The MariaDB foundation distributes MariaDB and guarantees it will remain open source.
PHP, Perl, and Python are object oriented, server-side scripting languages. Any logic you put in these languages run inside the web server and the results can be embedded into an HTML page before sending it to the web browser. PHP is widely popular among developers due to its natural learning curve and availability of ready-made extensions. Another compelling factor to use PHP is the availability of free and open source web development frameworks like Laravel, Symphony, Zend framework, and more, all written in PHP. Moreover, the popular CMS (content management systems) like WordPress, Drupal, and Joomla are all based on PHP.
LEMP stack is a variation of
LAMP stack. The elements that make up the
LEMP stack are same as
LAMP stack, except the web server Apache is replaced by Nginx, which signifies the E in a LEMP stack. Nginx is an open source reverse proxy server for
IMAP protocols, and it can also act as a load balancer. The
LEMP stack is also very popular and has been gaining ground for quite some time.
The only difference between
LEMP stack is just in the web server software used. A LAMP stack uses Apache as the web server, whereas a LEMP stack uses Nginx as the web server. Due to the event-driven system of Nginx, as opposed to the process-driven system of Apache, Nginx leaves a smaller impression in the system, allowing it to handle a higher load of
- A VPS running any of our OS options— CentOS 6/7, Ubuntu 16.04/18.04.
- A non-root,
sudo-enabled user. If you only have a
rootuser, see our SSH tutorial for details on creating new users.
- This tutorial requires the use of IP addresses. Whenever you see the
IP_ADDRESSvariable, replace it with your server’s IP address.
The easiest and fastest way of installing
LAMP stack in Ubuntu 16.04 is to use tasksel, a tool that installs each element of
LAMP stack with a single command. Tasksel is installed by default in Ubuntu 16.04, If tasksel is missing in your system, then install it by issuing following command from the terminal:
$ sudo apt-get install tasksel
To run tasksel from the command line, type:
$ sudo tasksel
LAMP Server and press
Tab key once, followed by the
Enter key. You will be prompted to provide a root password for MySQL, and once you’ve supplied that, the installation will be complete within a few minutes.
Find the versions of Apache, MySQL, and PHP by issuing the following three commands from the terminal.
$ apache2 -v Server version: Apache/2.4.18 (Ubuntu) Server built: 2018-04-18T14:53:04
$ mysql -V mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
$ php -v PHP 7.0.30-0ubuntu0.16.04.1 (cli) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.30-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies
Each element of
LAMP stack needs to be installed separately in CentOS 6/CentOS 7 since these packages are not grouped as they are in Ubuntu.
Before proceeding with the installation of a
LAMP stack in CentOS 6/CentOS 7, update your system using the following
$ sudo yum update && sudo yum upgrade
CentOS 7 enables a firewall by default, and if it is running in your system, open up port number
firewall-cmd. Opening port 80 will allow web traffic to pass through the firewall. You need the following steps only if your VPS is running CentOS 7.
$ sudo firewall-cmd --zone=public --permanent --add-service=http $ sudo firewall-cmd --reload
Now install each element of the
LAMP stack one by one. To start, install Apache by issuing the following command in the terminal:
$ sudo yum install httpd
Next, enable Apache to start automatically during system boot. The steps are different for CentOS 6 and CentOS 7.
In CentOS 6:
$ sudo chkconfig httpd on $ sudo service httpd start
In CentOS 7:
$ sudo systemctl enable httpd.service $ sudo systemctl start httpd.service
Install MySQL server using following
yum command and start it. Optionally, you can run
mysql_secure_installation to make it secure and enable it during system boot.
In CentOS 6:
$ sudo yum install mysql-server $ sudo service mysqld start $ sudo mysql_secure_installation $ sudo chkconfig mysqld on
In CentOS 7:
$ sudo yum install mariadb-server $ sudo systemctl start mariadb $ sudo mysql_secure_installation $ sudo systemctl enable mariadb
The default repository of CentOS contains PHP 5, which is fine for some purposes, but many will want to install the more recent and improved PHP version 7. To install PHP 7, you need to add a new repository to your CentOS installation.
In CentOS 6:
$ sudo yum install epel-release yum-utils $ sudo yum install http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
In CentOS 7:
$ sudo yum install epel-release yum-utils $ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Now enable the
remi repository using
yum-config-manager before moving forward with the installation of PHP 7, along with few other popular PHP modules. Type in the following commands on both CentOS 6 and CentOS 7.
$ sudo yum-config-manager --enable remi-php72 $ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql
Once the installation of PHP 7 is done, restart Apache to apply new settings:
In CentOS 6:
$ sudo service httpd restart
In CentOS 7:
$ sudo systemctl restart httpd.service
You can also list all available PHP modules using the following command and then install the required modules one by one.
$ sudo yum search php-
Find the installed version of each element of the LAMP stack using the following commands:
$ httpd -v $ php -v $ mysql -V
To test the installation of LAMP stack, create a
phpinfo file inside the root directory of Apache, which is
$ cd /var/www/html $ sudo echo "<?php echo phpinfo();?>" > info.php
Point your favorite web browser to
http://IP_ADDRESS/info.php. You can view the PHP version, system information, build date, and more. Seeing this page load confirms that the LAMP stack installation was successful.
Web servers are an essential part of what powers the Internet as we know it today, so I hope this explanation has been useful. Whether you’re just browsing the web or trying to host your website with a virtual private server, it pays to understand web servers on a deeper level.
Now that you know how to install a LAMP stack, it’s time to get to building your own little home on the Internet. If you need an extra boost, or don’t want to spend time in the terminal, you might want to look into some free cPanel alternatives to help you manage everything.
However you get there, post your self-hosted website (especially if you host it via SSD Nodes) in the comments! Not much makes us happier than seeing how people are using our servers to build something amazing on the web.