Web server on CentOS 8 with php7, node.js, and redis

Preface

It's been 2 days since the new version of the CentOS operating system was released, specifically CentOS 8. So far, there are quite few articles online about how to do things in it, which is why I decided to fill this gap. Moreover, I will not only talk about how to install this pair of programs but also about how I see the installation of Linux in a virtual environment in today's world for typical tasks, including disk partitioning and more.

But first, I want to briefly explain why you should switch to this version from all previous ones, and there are two reasons for that:

  1. php7! The previous version of CentOS used the 'traditional' php5.4...

    Okay, on a more serious note, many packages have skipped several versions at once. We (fans of Red Hat-based OS) have finally entered, if not the future, at least the present. And supporters of Ubuntu will no longer laugh at us and point fingers, well... at least for a little while ;).

  2. Transitioning from yum to dnf. The main difference is that it now officially supports working with multiple versions of packages simultaneously. I haven't needed this in version eight yet, but it sounds promising.

Creating a virtual machine

There are different hypervisors, and I don’t aim to tailor the reader for a specific one; I will discuss general principles.

Memory

First… To install the CentOS system starting from 7 for sure, and I think it was the same for 6 too ('but that's not certain'), you need at least 2 GB of RAM. So I recommend allocating at least that much to start.

But just so you know, after installation, you can reduce the amount of memory. The bare system works quite well with 1 GB; I've tested it.

Disk

For a proper installation, you should create a virtual disk of 20-30 GB. That should be enough for the system. And a second disk for data. This can be added either when creating the virtual machine or later. I usually add it later.

Processor

The bare system doesn't lag on a single core. And since resources scale freely, I don’t see a point in giving more at installation (unless you perfectly know the requirements and are too lazy to go into the configurator once again).

Everything else can usually be left as default.

Actually, the installation

So... Let's start the installer... Personally, I have been installing such services only as virtual machines for a long time, so I won't describe how to write the distribution to a USB stick — I just mount the ISO as a CD in my favorite hypervisor, boot it up, and off we go.

The basic installation goes pretty typically; I will only focus on a few points.

Source selection

Since the release of the eighth version, the Yandex mirror has been down for several days. I mean, it occasionally comes back up, but then it starts showing errors again. I’m sure it’s due to excessive load on the service. Thus, to specify the source, I had to go and choose a mirror I like instead of entering the usual address. here, select the mirror I prefer there, and manually enter the address in the installer window. It’s important to remember that you need to specify the path to the folder where the repodata. For example, mirror.corbina.net/pub/Linux/centos/8/BaseOS/x86_64/os.

Disk partitioning

This question is rather religious, in my view. Every admin has their own stance on this matter. However, I will share my perspective on the issue.

Yes, in principle, you can allocate all the space for root, and it will work, most often quite well. So, why go to the trouble of creating different partitions? — In my opinion, there are two main reasons: quotas and portability.

For instance, if something goes wrong and there are errors on the main data partition, you want to be able to still boot the system and carry out recovery actions. Therefore, I personally allocate a separate partition for /boot. It holds the kernel and the bootloader. Usually, 500 MB is sufficient, but in rare cases, more may be needed. Considering that we are already used to measuring space in terabytes, I allocate 2 GB for this partition. And importantly, it cannot be set up as LVM.

Next comes the root of the system. For proper installation, I have never needed more than 4 GB specifically for the system, but during planned activities, I often use the /tmp directory for unpacking distributions. I don’t see the point in allocating it as a separate partition — in modern systems, it is cleared automatically, so it doesn’t fill up. So I allocate 8 GB for the root.

Swap… In general, there’s little practical benefit from it. If your server is using swap today, it merely means that the server needs more RAM. Otherwise, performance issues are guaranteed (or some program has a memory leak). Therefore, this section is only needed for diagnostics. Hence, 2 GB is a great number. Yes, regardless of how much memory the server has. Yes, I've read all those articles about the ratio of memory size to swap size... IMHO, they are outdated. In 10 years of practice, I have never needed it. I used them 15 years ago, yes.

Whether to allocate /home to a separate partition, IMHO, is up to each person to decide. If someone will actively use that directory on the server, it’s better to allocate it. If not, there’s no need.

Next, /var. In my opinion, it must be allocated. You can start with a figure of 4 GB, and then see how it goes. And yes, by 'how it goes' I mean that

  1. Firstly, you can always mount another disk in the subdirectory /var (which I will show later with an example).
  2. Secondly, since we have LVM — it can always be expanded. Usually, you need to add more when too many logs start piling up there. But I have never been able to predict that number in advance, so I start with 2 GB and then monitor.

Unallocated space will remain free in the volume group, and it can always be used later.

LVM

All Partitions, except for /boot, should be created in LVM. Yes, including swap. Yes, according to all advice, swap should be at the beginning of the disk, but in the case of LVM, its location cannot be determined at all. But as I mentioned earlier, your system should not use swap at all. Therefore, it doesn’t matter where it is located. We’re not living in 1995, honestly!

Next, in LVM, there are several basic entities that you need to know how to work with:

  • physical volume
  • volume group
  • logical volume

Physical volumes are combined into groups, with each physical volume being in only one group, while a group can be on multiple physical volumes.
And logical volumes are each in one group.

But... We are in the 21st century, after all. And virtual servers exist. It makes no sense to apply the same mechanisms used for physical servers. With virtual servers, it is crucial to keep data separate from the system! This is particularly important for the ability to quickly switch data to another virtual machine (for example, when transitioning to a new OS) and for various useful features (like separate backups sectioned by the hypervisor, for instance). Therefore, one volume group is used for the system and another one is essential for data! This logical separation greatly helps in practice!

If you created only one virtual hard disk when setting up the virtual machine, that’s where the configuration ends. If there are two, just don’t partition the second one yet.

Starting the installation.

Post-installation

So, finally, the freshly installed system has booted. The first thing to check is the internet.

ping ya.ru

Is there a response? — Great, press Ctrl-C.
If not — go configure the network, because life without it is impossible, but my article is not about that.

Now, if we are not logged in as root, let’s switch to root, because typing such a number of commands with sudo is too much for me (and may the paranoid admins forgive me):

sudo -i

Now the first thing we do is type

dnf -y update

And if you are reading this article in 2019, most likely nothing will happen, but it was worth a try.

Now let’s configure the remaining disk.

Let’s say, the partition for the system was xvda, then the data disk will be xvdb. Okay.

Most advice starts with the words 'Run fdisk and create a partition...'

So, that's not correct.!

I’ll say it again because it's important! In this case, for working with LVM, creating partitions on a single whole, albeit virtual, disk is harmful! Every word in this phrase is important. If we are operating without LVM — it's necessary. If we have both the system and data on the disk — it's necessary. If we want to leave half the disk empty for some reason — that's also necessary. But usually, all these assumptions are purely theoretical. Because if we decide to add space to the existing partition, it will be easiest to do so with this configuration. The convenience in management outweighs many other factors, so we consciously aim for this configuration.

The convenience lies in the fact that if you want to expand the data partition, you simply add space to the virtual volume, after which you can extend the group using vgextend, and that's it! In rare cases, something else may be required, but at least you don't have to expand the logical volume first, which is already a plus. For expanding that very volume, it is recommended to remove the existing one first and then create a new one on top… This can be quite unpleasant and can't be done live, whereas the expansion scenario I described can be executed "on the fly" without even unmounting the partition.

So, we create a physical volume, then a volume group that includes it, and finally a partition for our server:

pvcreate /dev/xvdb
vgcreate data /dev/xvdb
lvcreate -n www -L40G data
mke2fs -t ext4 /dev/mapper/data-www

Here you can specify a lowercase "l" (and size in GB) instead of an uppercase "L" to indicate a relative size, for example, to use half of the currently available space in the volume group, you need to specify "-l +50%FREE"

The last command formats the partition in the ext4 filesystem (which, based on my experience, shows the greatest stability in case something goes wrong, so I prefer it).

Now we mount the partition in the desired location. For this, we add the correct line to /etc/fstab:

/dev/mapper/data-www    /var/www                ext4    defaults        1 2

And we type

mount /var/www

If an error pops up — raise the alarm! Because it means we have an error in /etc/fstab. And that on the next reboot, we will have very serious problems. The system may not boot at all, which can be quite disastrous for cloud services. Therefore, we need to either urgently fix the most recently written line or delete it entirely! That's why we didn't write the mount command manually — otherwise, we wouldn't have such a wonderful opportunity to check the config right now.

Now, we install everything we wanted and open the ports for the web:

dnf groupinstall "Development Tools"
dnf -y install httpd @nodejs @redis php
firewall-cmd --add-service http --permanent
firewall-cmd --add-service https --permanent

If desired, you can also install the database here, but personally, I try to keep it separate from the web server. Although having it nearby is faster, yes. The speed of virtual network adapters is usually around a gigabit, and when running on the same machine, operations happen almost instantly. However, it's less secure. It depends on what is more important to you.

Now we add a parameter to the configuration file (creating a new one, as per the modern ideology of CentOS).

echo "vm.overcommit_memory = 1" > /etc/sysctl.d/98-sysctl.conf

Reboot the server.
In the comments, I was criticized for advising to turn off SeLinux, so I will correct myself and write that after this, you should not forget to configure SeLinux.
Actually, profit! 🙂

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster