Part of Datorkirurgen AB – IT experts across Sweden

Deployment on Linux with scripts, dry runs and troubleshooting. This is how we publish websites on AWS servers safely. Linux Service helps all of Sweden.

An ongoing project: several websites on one server

Right now, we are building a new website for Datorkirurgen AB, and it is the one you are reading. It shares a Linux server in AWS with six other websites. Each site has its own folder and its own database, but they use the same Apache, PHP and MariaDB. Therefore, every change must hit the right website and nothing else. The images in the article are real screenshots from the work, and as a result you see exactly how a deployment on Linux works with us.

The work covers 29 articles, just over 130 images, menus, SEO settings and a new home page. Doing all this by hand in the admin panel would take days. In addition, it would be hard to repeat if something went wrong. Instead, we write scripts that do the job in a few minutes. The scripts can run again, and they always show what they intend to do before they do it.

Deployment on Linux: signing in to an Amazon Linux 2023 server before the work on the websites
Deployment on Linux: signing in to an Amazon Linux 2023 server before the work on the websites. A real screenshot from our server. Addresses and host names are masked.

Deployment on Linux starts with the right folder

The first step is straightforward, but this is where many faults arise. The screenshot above shows that cd var/www/html does not work from the home directory. In fact, the path lacks the leading slash, and then the shell looks in the wrong directory. The correct command is cd /var/www/html. It is a small detail, but a script with the wrong path can write to the wrong website. Therefore, we always give the full path in every command.

In addition, we run all commands as the web server’s user. On Amazon Linux it is called apache, while it is called www-data on Debian and Ubuntu. If you run as root, new files get the wrong owner, and then the web server can no longer change them. We put the variables after env, for example env WP_CLI_CACHE_DIR=/tmp/wpcli DRY=1, since sudo can otherwise remove them. Read more about file permissions in Linux.

Deployment on Linux in five steps: package, backup, dry run, real run and verification
Deployment on Linux in five steps: package, backup, dry run, real run and verification. Do you need help with Linux? Linux Service helps you in Stockholm, on site or remotely.

Dry runs during deployment on Linux

Every script we deliver has a dry run mode. This means that the script goes through all the work but changes nothing. Instead, it prints what it would have done. Only when the output looks right do we run it for real. In the image below, the dry run ends with 28 posts and 120 image links, and after that the real run starts with the status publish.

The method has prevented incorrect changes many times. For example, a dry run reveals wrong paths, missing files and incorrect conditions. At the same time, the client sees exactly what will happen. We always deliver the scripts as zip files with instructions and never paste long blocks directly into the terminal. In this way, every deployment on Linux becomes both traceable and reproducible.

Dry run finished with 28 posts and 120 image links, followed by the real run
Dry run finished with 28 posts and 120 image links, followed by the real run. A real screenshot from our server. The host name is masked.

Troubleshooting: all the images are missing

During the first run, a long series of warnings appeared about missing images. The files were nevertheless in the right place on the server. The fault was therefore in the script and not in the files. The cause turned out to be a detail in WP-CLI. In fact, the eval-file command runs the script inside a function, and therefore the variables at the start of the script do not become global. The function that uploaded the images thus saw an empty path.

The solution was to make the variables global explicitly with $GLOBALS. After that, we tested the fix with PHP on a copy before we delivered a new package. The warning now prints the full path, which makes the next troubleshooting faster. In short, it is such details that separate a quick fix from a long troubleshooting session. Feel free to read about WP-CLI on Linux.

Troubleshooting scripts: the warning Image missing for every image during a run with WP-CLI
Troubleshooting scripts: the warning Image missing for every image during a run with WP-CLI. The warnings that led us to the fault in the script.

Menus, SEO and the home page with the same method

When the articles were in place, we ran the next script. It switches off author and date archives in Yoast, creates the Articles page and builds the menu with services. Here too, we first ran a dry run. The output then showed every menu item to add, and only after that did we run it for real. After that, a separate script built the home page, which also connects the contact form.

Afterwards, we always clear the Elementor cache with wp elementor flush-css and save the permalinks again. We open the pages in a private window and check images, links and forms. In addition, we submit the sitemap to Google Search Console and request indexing of the most important pages. It is the last step in a deployment on Linux, and as a result the website is ready for visitors.

Deployment on Linux: dry run of the menu and the SEO settings in Yoast
Deployment on Linux: dry run of the menu and the SEO settings in Yoast. The dry run lists every menu item before anything changes.

Security and recovery

We always take a backup of the database before every run. The file gets permission 600, so that only the owner can read it, since it contains the whole website. If something goes wrong, we can therefore restore it in a few minutes. In addition, we mask all screenshots before we publish them, so that addresses, host names and keys are not visible.

A restore requires careful thought. If you load an old backup, everything you did afterwards disappears, and in addition files on the disk can remain without a link. Therefore, we have ready-made scripts that clean up such files, and these also have a dry run mode.

The script creates services and articles in the right order on the website
The script creates services and articles in the right order on the website. A real screenshot from the import of services and articles.

Contact Linux Service about deployment

Linux Service is part of Datorkirurgen AB and has more than 30 years of experience of Linux, scripts and databases. We deploy websites and applications on Amazon Linux, Red Hat, AlmaLinux, Debian and Ubuntu for businesses across Sweden. Prices start from €1,300 per hour excluding VAT. The tool we use is described at wp-cli.org.

Therefore, contact us if you are going to move, build or update websites on a Linux server. First, we carry out a review, and then we propose a plan. We work remotely or on site in Stockholm.

Frequently asked questions about deployment on Linux

Questions about deployment on Linux

Dry runs, backups and troubleshooting

More about publishing and support

Website developed by Datorkirurgen AB · Privacy policy