How to set up a web server and host a web on GNU / Linux

Web URL

If you've ever wondered how some hosting services that exist in the network can host a web page or what is a web server and how it works, including what are the bars that appear in the URL of some web pages, how a client can connect to a web page remotely, etc., in This article is going to make it clear to you. I will teach you not only the concepts of what a server is, you will also know how to create your own web server step by step thanks to our simple tutorial.

Today we all use remote services of all kinds, also the burgeoning cloud computing, but if there is a service that stands out above the rest, perhaps it is the one they provide web servers, since there are tons of websites that we visit on a daily basis to read our favorite news, check emails from web interfaces that provide some services such as GMail, make transactions, work, make purchases online, etc. Nobody escapes these services, right? However, for many of its users they remain unknown as to what lies behind them ...

What is a server?

Server farm

Some users think that a server is something special, something very different from what it really is. But said in simple language, a server is nothing more than a computer like the one we can have in our house, only that instead of acting as a client, it is doing it as a server, that is, it is providing a service. You may think that, in that case, why those images that we see on TV or in other media when servers come out are so rare ...

Well, those images like the one I have included here are images of server farms. This is the name given to a series of clusters of computers that work together as a single server. Keep in mind that the services that these servers usually offer are intended for hundreds, thousands or millions of users who act as clients on their computers, smartphones, tablets, smart TVs, etc. Therefore, the capacities they must handle are much higher than those of a home computer.

You just have to think about services like Twitter, how many millions of users this social network has, how many files and messages are transferred every second. If you think about it, it's a large amount of dataTherefore, it is not valid with a connection like the one we have at home and a normal computer. Very fast connections are needed so that there are no delays in the accesses of all those users, and give it the necessary capacity so that they can host all that information.

With this what I mean is that for this service dozens or hundreds of "computers" are used like the ones we can use at home that are housed in cabinets with racks. But in essence, each of them is not far from a desktop computer like the one we have in our home. Maybe some have special microprocessors such as AMD EPYC, Intel Xeon, etc., maybe they also have several hard drives configured as RAID to avoid that if any of them fails, the information is lost, but as I say, keep that they are computers like the that you manage right now, and I will tell you this because now I will explain to you how to turn your PC into a modest server ...

Of course these servers are of many types, there are those that provide cloud services, such as storage, there are those that provide email services, web servers, also some that simply services such as DNS, NTP, DHCP, LDAP, etc., that is, all the latter are very necessary and surely use them daily without even realizing it, since they are services that some ISP (Internet Service Provider) or Internet service provider provide us.

What is a web page?

Website on different devices

We have previously mentioned that some web servers, they provide hosting or hosting for Web pages. A web page is a set of electronic or digital information (HTML, PHP, CSS, ...) that can contain only text, or also other content such as web apps written in certain specific programming languages ​​or scripts (Perl, JavaScript, Ruby with the RoR or Ruby on Rails framework, PHP, etc.), multimedia content (images, videos, sounds, etc.), and also links that usually direct you to another place on this same web page or to a different one.

And for this to be possible we have the web servers that host them, that is, it stores all this data on its hard drive, and also a series of network protocols such as HTTP (HyperText Transfer Protocol) and HTTPS (HTTP with SSL / TLS certificate security). A software will take care of this as we will teach you later, that is, to implement a bidirectional connection for the client and that he can navigate through the hypertext content, that is, the means to share, link and interact with the information adapted to the WWW (World Wide Web).

How Does It Work?

Client-Server connection

Well, we already know what a web and a web server is, explained in my own way and with simple language so that more or less everyone can understand it, even those who do not have knowledge about this technology. And now I continue with this section in which I will try to make clear the operation of this client-server system. But for this, first I will differentiate between the two:

  • Client: the client is the user who accesses the website from their device, be it a laptop, desktop, tablet, smartphone, etc. For access, you only need an Internet connection and a web browser, a very important software on the client side that is in charge of displaying all that web content in a user-friendly way and allowing the user to interact with it. And for this we will only need the address of the web page or an IP ..., although you may be thinking that this is not always needed to access, since there are search engines (eg: Google) that, through keywords, allow us to show these websites that have indexed, and you're right.
  • employee: As we have explained, it will contain all the data and a software that acts as a server, that is, it allows the client to connect to do whatever they need to do. In the case of a web server it will be, for example, Apache, Lighttpd, etc.

I would like to point out something else, and it is that as you well know, the IP address It is the one that identifies a machine connected to a network, in this case it will be the IP of the web server. There are some services like this that show you the IP of your favorite page, for example, if you search google.es it will show you the IP corresponding to the server where this service is hosted. If you try to enter this number in the address bar of your web browser, you will see that both by putting www.google.es and said IP, in both cases it will show Google.

Why do I say this? Well because it helps me to link with DNS servers. These servers are other services that contain tables with the names of the websites and their corresponding IP, so that when someone searches for an address by name and not using the IP, the server makes the browser show the content of said website. This is done to make it more intuitive for humans. We can't remember all those numbers easily, but we can remember the names of our favorite website, right?

And I end by specifying what is the URLs (Uniform Resource Locators) or uniform resource locator, which we see at the top of the bar of our browser when we access a website. For example, imagine that you register the domain myweb.es. In this case, that domain will be yours and you can use it to display your web page. Imagine that someone accesses the address http://www.miweb.es/info/inicio.html#web:

  • http://: Indicates that we are accessing using the HTTP protocol, although it could also be HTTPS, FTP, etc. But in this case it is the first, therefore it is a web content.
  • WWW: you know it's from the World Wide Web.
  • myweb.es: this is the domain you have registered, that is, the name that replaces the IP of the server or host that contains your website. Therefore, it will be a name that identifies a server or machine, after all ... In addition, it contains a TLD (Top Level Domain) which in this case is .es, to identify that it is a website from Spain, although it could be .se from Sweden, .com from company, .org Organization, etc.
  • /info/home.html#web: this simply defines that this content has been accessed, that is, an info directory and within it is a home.html file with the hypertext and specifically the section website. It could also have been an image, PDF, video, etc. As it happens in your file manager when you go to a path on your local hard drive, right?

I think that with this there is enough clear operation explained in a simple way.

Tutorial: build your own web server step by step

Apache test web

If you have a GNU / Linux distribution anyYou should know that once you have configured your network properly, since you cannot have a dynamic IP, it must be static or otherwise it will change its value and it would be more difficult to access the web. In addition, you should also pay attention to if you have a firewall configured with iptables or other software that no rule is restricting transfers through port 80 or 8080, etc., as if you have AppArmor or SELinux, they should be allowing the user to act. web server daemon, in this case Apache.

The next step is to install the software to implement our web server, in this case Apache and other additional packages to complete the LAMP, but it could have been another. In my case, from Debian:

sudo apt-get update

sudo apt-get install apache2
sudo service apache2 restart
sudo apt-get install mysql-server php5-mysql
mysql -u root
mysql -u root -p (sin no introdujiste el password durante la instalación)
sudo apt-get install php libapache2-mod-php5 php5-mycrypt
sudo apt-get install php5-sqlite

Then you could configure some parameters from the server if you need it, or perhaps if it doesn't work and you get the page that I show you in the previous image, look at the logs because something has gone wrong ... By the way, you can see that page by accessing your web browser and setting the localhost 127.0.0.1 .2 in the address bar or the static IP that you have configured for your server. The default ports will be in /etc/apacheXNUMX/ports.conf in case you want to modify them.

If you want you can also install other additional packages, if you also plan to have a mail server, or some configuration panels like phpAdmin, etc.

Host your website on the server

Website construction

Once we have our server ready, remember that the PC that you have dedicated to the server must always be on and connected to the network so that the web is accessible from any other device, otherwise the server will be "down". Now we only have host our website, that we can have created it ourselves using HTML or other code, or even make use of a CMS like WordPress that makes things much easier for us and that we could host in the same place ...

And for this we will do it in the / var / www / html / directory that unless we have changed the Apache configuration, it will be there where the webs are hosted. For example, you could do a little test using PHP by creating a file with your favorite text editor with the content:

<?php phpinfo() ?>

Call it test.php and now, after restarting the apache2 daemon, you will be able to see if it can be accessed from the browser: 127.0.0.1/test.php.

I hope this tutorial has helped you and that you at least understand a little better how servers work, so that now every time you access our blog to read a news item, you know everything behind it. Do not forget to leave your comments, doubts, or suggestions, ...