How to deploy a website on VDS

Let's imagine that you have finished your project or completed the first tasks on the project, and it is time to publish the work on the network (organize dev/test sites or “roll out to production”).

We'll tell you how we do it.

“Where to turn around?” is one of the first questions you may have.

We will answer that the most universal, simple and correct solution is VDS (virtual dedicated server, which the hosting provider provides you for rent). It is almost like renting an apartment in which your web projects will live, where a loyal tenant allows you to do whatever you want and does not interfere as long as you pay and do not do anything illegal.

Then you ask: OK, why not virtual (shared) hosting? And we will answer: such a solution only adds difficulties and deprives you of flexibility. Although such a method may be familiar to those who have their own small site that does not need special technologies and a simple PHP backend is enough.

Shared hosting provides access to limited resources and often an inconvenient website control panel. The user works with a limited set of programs and does not have installation rights, unlike VPS or VDS, where the user receives root rights by default.

However, when choosing a VPS/VDS, it is important to remember that minimal Linux administration skills are required.

In what cases is VDS preferable?

In high-load projects that require flexible resource management, settings, and the use of a complex technology stack. And in any other case when the capabilities of shared hosting are clearly insufficient.

VPS, VDS – what's the difference?

In fact, there is no difference in appearance. As well as in operating experience.

VPS and VDS differ only in implementation. VPS operation is fully controlled by the operating system, while VDS — is a virtual server at the hardware level of the system.

In the case of VPS, a Linux-based operating system is installed on the server, on which OpenVZ is installed and configured. The process of managing the server (container) resources is performed by the program itself in the OS. VPS assumes dynamic distribution of available resources between all machines.

The advantages include the ability to change allocated resources without rebooting the machine. However, when using this method of virtualization, the container's operation will depend on neighboring containers on the host. This can affect the performance of your server, and if one server is overloaded, another, seemingly independent one, may suffer.

VDS operation occurs at the hardware level (KVM technology). Each virtual dedicated server has its own operating system and resources.

KVM eliminates situations that could happen with VPS and ensures the reliability and fault tolerance of physical machines. However, you lose flexibility.

The issue of changing disk space will be solved by mounting additional storage instead of expanding the current one. Increasing the amount of RAM or the number of processor cores may also be more difficult.

When is VDS needed?

When you need to store a lot of data and host heavy projects.

As mentioned earlier, you can do anything with VDS. VPN server, database, backend and frontend host, remote build machine, CI/CD machine, game server, and so on.

Which configuration to choose?

RAM:

  • 2-4 GB — one or several small sites. Well suited for dev sites that are not expected to have a real, combat load;

  • Up to 16 GB – suitable for almost any type of project: online stores, SPA or Telegram bots, mini apps, etc.;

  • 16-32 GB — heavy projects, or servers where you plan to host many projects. For example, marketplaces or backend for games.

CPU:

We recommend taking machines with processors with a frequency of 3 GHz and a number of cores of two or more.

operating system

Providers often offer ready-made OS images, which greatly simplifies the process of creating a new VDS. The most common and recommended systems are Ubuntu and Debian. We choose the first option.

Which provider?

Whichever you like. We trust FirstVDS and Timeweb and use them, giving preference to the first.

What's next?

You have chosen a tariff, rented a server. Then it is enough to simply connect via SSH to the machine and work.

For SSH connections we use Termius, a modern SSH client that allows you to save and synchronize hosts between clients, support multiple simultaneous connections. Termius also has a built-in SFTP client, if needed.

Once connected, the first thing you need to do is set up a basic environment for your project.

By default, the provider will provide you with a clean server, so you will have to install nginx, docker and other utilities yourself. Up-to-date information on how to configure certain applications can be found on their official resources.

Don't forget to run apt update or similar commands for other systems before working with your OS's package repository.

Project organization

When working with nginx, it is good practice to place your projects in the /var/www/html directory of the server. We prefer the following structure (approximately, for a typical Laravel project):

/var/www/html/:

project-domain.com/
-     builds/
-     -     current -> main/
-     -     main/
-     -     -    storage -> /var/www/html/project-domain.com/storage/
-     -     previous-main/
-     storage/
subdomain.project-domain.com/
another-project-domain.com/

Directories with projects are named according to the domain on which the project will be available. The builds directory is intended for the source code, inside it is the current build, the previous build and a link to the current build.

This structure allows you to quickly navigate projects and roll back the build in case of a screw-up.

The storage directory is intended for storing system data (cache, logs) of the framework and downloads, if external storage is not used (for example, S3). Moving storage out of the project directory allows you to save its contents between updates.

This structure may also be convenient for CI/CD, but its appearance may vary depending on the project and deployment approach.

I have a database dump and I need to deliver it to the server. What should I do?

You can use SFTP or the rsync utility, but we know a more convenient solution – Croc.

Using croc is very simple:

The croc send command on the sending machine opens a secure p2p channel, outputs to the standard output stream the secret code that you need to pass as an argument to croc on the receiving machine. The file will be transferred and you don’t need to bother with complex formatting of the rsync command.

You can also transfer directories, but this will be somewhat slower than transferring, for example, an archive of the same directory.

Using croc is convenient for organizing interserver file transfers, but it is important to remember that the utility must be installed on both machines.

The code is delivered, the containers are up. What's next?

First, you need to link your domain to the server address. In the control panel of your domain provider, create an A-record for the domain or subdomain, in which you specify the IP address of the server.

Now a little about the organization of nginx configs.

Nginx provides us with two directories: sites_available and sites_enabled.

In sites_available we create server configs for domains, we name the files like directories from /var/www/html — by domain. In sites_enabled you can place soft links to configs from sites_available. This will allow you to quickly enable and disable sites at the web server level.

Don't forget to run nginx -t to test configs and nginx -s reload to load changes.

The site is accessible from the network. Certificates are required.

The easiest way to issue a certificate to a site is to use the Let's Encrypt service. The certbot utility will help with this.

Certbot works with nginx and apache web servers and is available from Snapcraft or Python PIP. Either installation should work, we prefer the one with pip.

Once certbot is installed, make sure your sites are accessible and run the certbot –nginx command.

Enter your email address to receive certificate expiration emails and unsubscribe if you are not interested. Certbot will prompt you to select domains to issue a certificate for. You can choose not to select it – then the certificate will be issued/renewed for all domains found in sites_enabled.

Please remember that the certificate is valid for three months, which means that it will need to be renewed periodically.

You can immediately create a cron entry to update once every 2-3 months.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *