Scripts for managing virtual hosts on the Debian web server

I want to share a little groundwork that simplifies the administration of a web server running a Debian-like operating system. Immediately make a reservation, I'm far from a guru in this area, just at some point I needed to raise vsftpd, nginx, PHP-FPM and PostgreSQL. It's no secret that when you add virtual hosts, configure PHP-FPM pools and create databases, you have to perform the same actions. Removing virtual hosts with everything connected with them, as well as creating backups, are also very uniform. Therefore, it would be nice to get scripts that automate all these things.

To be honest, at the time of writing the scripts, I did not bother looking for existing solutions, it was just interesting for me to do everything myself. And although the repository is on Bitbucketappeared along with the first version of scripts, thoughts about their possible usefulness for someone else came to mind a little later.

And yet, for starters, it’s worth considering existing solutions. One of them is described in the article . It provides an example script for FreeBSD that creates a virtual host. Another solution is given in the article , and a similar script is considered there, but for Debian.

After reviewing the materials of the articles, we can conclude that none of them presents a complete solution to the problem. Also upset is the lack of a flexible parameter system, that is, you can not affect the behavior of scripts without changing their code. In the meantime, we may need to create a virtual host without PHP support or, for example, not create a database. In addition, all configs are located right inside the scripts, which is also not very good.

To business


So, there are several scripts:
  • siteadd.sh - to create a virtual host;
  • passwd.sh - to change the password;
  • backup.sh - to create a backup copy;
  • sitedel.sh - to delete a virtual host.

How to work with them is described on the wiki , but the general idea is that, for example, when creating a test.ru virtual host, the script asks which password to assign to the user test.ru, creates this user in the system, then creates the / var / www folder /data/test.ru/public_html with the stub file index.html and gives all the necessary rights to the user test.ru.

Next, the script creates the nginx configuration file according to the template with the necessary parameters and similarly sets up the PHP-FPM pool, in PostgreSQL creates the test_ru database and the user test_ru, which it appoints as its owner.

If --no-php is specified when invoking the script, then the PHP-FPM pool is not configured, and when creating the nginx configuration file, a template without PHP support is used. With the --no-pgsql parameter , PostgreSQL does not create the database and user.

When changing the password for the test.ru virtual host, the script asks for a new password and assigns it both to the user test.ru in the system and to the user test_ru in PostgreSQL.

When creating a backup copy for the test.ru virtual host, the script creates:
  • file archive /var/www/backup/test.ru/YY-mm-dd_HH:ii:ss.tar.gz;
  • base dump /var/www/backup/test.ru/YY-mm-dd_HH:ii:ss.sql.

When deleting the test.ru virtual host, the script deletes the database and user test_ru from PostgreSQL, deletes the /var/www/data/test.ru folder and the user test.ru from the system. Backups are not deleted.

It should be noted that there are two versions of scripts:
  • regular version, which lies in the default branch;
  • version with support for passing parameters in JSON format, which lies in the json branch.

The regular version does not require any special tools. The JSON-enabled version requires a JSON processor on the system called jq, which is available through standard Debian repositories. And although someone will not like this condition, this version is now more flexible, but not for long.

What's next?


From time to time, I return to my server to play around. As a result, scripts develop, and here, what I would like to finalize in the first place:
  • increase the number of parameters of the regular version, which will make it more flexible, including adding a parameter for deploying a backup;
  • add configuration templates, as there are some settings missing;
  • write a script for cleaning obsolete backups;
  • provide support for Apache and MySQL.

That's all for now. I remind you that scripts can be downloaded here . I will be glad to comments and suggestions. Thanks for attention!

Also popular now: