How to install a Linux server for web hosting

Let’s install a web server on Linux. It’s not just a simple NGINX with unencrypted Http traffic; it’s a fully functional containerized web server with a database server and trusted https certificates that are also auto-renewed.

Setting up a web server can be complicated because a web server contains many different components. Like you want to have a database server, you want to have intrusion prevention like Fail to ban, you want to have trusted https certificates, and so forth. But, you can always use a simple approach, a solution that can be easily deployed in just a few minutes, but still has all the necessary components, has trusted https certificates, and so on. And that’s why it is recommended to go for a containerized approach with Docker and Docker Compose so that can be simply installed.

The perquisites before the installation

You need to have Docker and Docker Compose installed and also have a public domain that will point to the IP address of your web server. And your web server needs to be accessible from the public Internet on port 443 and port 80. So don’t forget to check all these prerequisites before starting the installation. 

There are just a few commands you need to execute. You can visit this link for easy installation- https://github.com/xcad2k/videos/tree… . But you could also use a cloud provider like Digital Ocean because they have a droplet in their marketplace where you can easily deploy an Ubuntu server that already has Docker and Docker Compose pre-installed and that will be just created in a few minutes.

If you have installed your Linux server and you have installed Docker and Docker Compose on your server, You can start configuring your web server. You can start with creating a configuration file that is called the Docker Compose file. And this is used to configure all services that are used for the web server stack. It is suggested to use an image from Linux servers IO

Some enthusiasts managed container images for the community and they have some awesome stuff on their homepage. Here you’ll find an interesting image that is called swag. So this contains an nginx image with a fail to ban. It also automatically obtains trusted Http certificates and it can also run a database server and it can also act as a reverse proxy, which is also pretty amazing!

Checking the proper functioning of Docker and Docker compose

 You are supposed to mainly focus just on the web server configuration with a database server. So if you check out the GitHub homepage you can find amazing documentation. There are many different examples with different approaches.

And here you can find some interesting examples for Docker Compose configuration files.  Use a very simple approach by installing a simple web server with a database server and use that to deploy a WordPress homepage. You have to install a new server droplet on your digital Ocean cloud with Ubuntu and automatically Docker and Docker Compose are installed. Let’s check if Docker was installed correctly.

And also check if Docker Compose is there. So with these two commands, you can check if Docker and Docker Compose are running correctly. If you see the version number, everything is fine.

Adding a Username

If you don’t want to do everything with the root user, add a new user name.  For  example- let’s add the user name- “Christian”

You can automatically create a personal home folder with the m power meter, add a comment so this is an administrative user and add this user to the two groups- docker and Sudo. Change the default shell to bin bash. By using the following commands, a new folder in the home “Christian” has been created. VPS with SSD Storage

Now, you have to check if you can log in with the user now.   Exit from here and let’s check if you can now log in with the user “Christian”.

Adding a password

 You can add your password also by using the following commands. 

Now, you need to do some updates. No need to remember or write down these commands. This is the link, https://github.com/bobbyiliev/101-linux-commands-ebook#file-commands from where you can copy and paste all the commands used here. 

The server is now updated and ready to be installed with the web server.

Create a new folder in the opt folder that is called web server swag and also change the owner of this folder to your administrative Linux VPS. Now you need to check which user and group ID your user has and that will be important later. So execute the ID and copy this user ID and this group ID (marked below). So now you can add your darker compose static files.

This is in the research folder web server stack and there you can see there are two files, the simple and the complete YAML. Start with the simple.yaml file. 

Maintaining the swag image and image file

So you just need to start with a simple docker composed configuration file and open this in visual studio code. So you first start with version number “2” services and add a new service with the name swag and that will use the image from Linux server IO.  That is called the swag image and to make it simple, set the container name also to swag.

One more important thing to do 

 Now you need to add this parameter here and you can look this up in the official docker documentation. So this will allow the docker container to access the network stack on the host operating system.

Environment variables 

Then you need to add some environment variables and the PUID and the p group ID should be the user and the group id of your administrative Linux user. So note that the docker container will set the permissions to this user ID. So remember, you don’t have the user ID 1000, you have the user and the group ID 1001. So you need to change that. Now you need to add the time zone. Add the time zone according to your country. And then you need to add the URL of our website.

So don’t change the last part but in the first part, you can choose whatever you like to store the configuration on your host operating system. Then you need to forward the ports for free and to the docker container. So this is important otherwise the docker container would not be able to accept any network connections from the public internet and we will also set the restart to unless stopped. So that means whenever you restart your host operating server, the docker container will also be started. If it was started before or if it was stopped, it won’t start the container automatically.

So now, you have to copy this file here to your Linux server so that you can just use the SCP command. You can use a simple YAML file and simply copy this to Christian (for eg. , you can choose any name) and then the public IP address of your server or you can just use the DNS name that points to the public IP address. (In this example, thedigitallife.com) and you just place this in the opt webs server swag folder and call this docker compose YML. 

Let’s go back to your web server here and see if the file is there. Now, you need to CD into this folder here and do NLS command and there you can see here is your docker-compose file. Let’s check if everything is working fine.

The final check

You can check the entire configuration using the commands for which a link has been given above. Here is the summary of all commands which you want to check. 

Now, you’re all ready for web hosting through a Linux Server!!

(For further commands & explanations, please visit https://github.com/bobbyiliev/101-linux-commands-ebook#file-commands)


Interesting Related Article: “7 Programming Languages That Every Computer Engineering Aspirant Should Learn