keepcalculator

06 Numbers and Powers

Auto-Start Nginx, MySQL & PHP-FPM on Boot

Card No. 06·20241 · Updated · Standard formula, shown below

This guide shows you how to configure Nginx, MySQL (or MariaDB), and PHP-FPM to start automatically whenever your Linux server boots. You’ll edit systemd service files, which is the standard way to manage services on modern distributions like Ubuntu, Debian, CentOS, and Fedora. By the end, your web stack will come up on its own after a reboot or crash, so you don’t have to log in and start each service manually.

What You’ll Need

  • A Linux server with systemd (most distros since 2015). Check with ps -p 1 -o comm= – it should print systemd.
  • Nginx, MySQL/MariaDB, and PHP-FPM already installed and working. If they’re not, install them with your package manager first.
  • Root or sudo access to run systemctl commands.

Step-by-Step

  1. Check current status. Run sudo systemctl status nginx mysql php-fpm (or mariadb instead of mysql). Note whether each is enabled or disabled. The goal is to see enabled for all three.
  2. Enable Nginx. Run sudo systemctl enable nginx. This creates symlinks in /etc/systemd/system/multi-user.target.wants/ so the service starts at boot.
  3. Enable MySQL/MariaDB. Run sudo systemctl enable mysql or sudo systemctl enable mariadb, depending on which you installed. If you’re not sure, run systemctl list-unit-files | grep -E 'mysql|mariadb' to see the exact service name.
  4. Enable PHP-FPM. Run sudo systemctl enable php-fpm. On some distros the service is named php7.4-fpm, php8.1-fpm, etc. Find yours with systemctl list-unit-files | grep fpm and use that exact name.
  5. Verify all three are enabled. Run systemctl is-enabled nginx mysql php-fpm (or the correct names). Each should print enabled. If any prints disabled or not-found, re-check the service name and repeat the enable step.
  6. Reboot to test. Run sudo reboot. After the server comes back, log in and run systemctl status nginx mysql php-fpm (or mariadb). All three should show active (running). You can also check that your web page loads and that PHP scripts work.

Common Mistakes

  • Wrong service name. Using mysql when you installed MariaDB, or php-fpm when your distro names it php8.2-fpm. Always check with systemctl list-unit-files | grep -E 'mysql|mariadb|fpm'.
  • Enabling a service that doesn’t exist. If you get Failed to enable unit: Unit file php-fpm.service does not exist, you have the wrong name. Find the exact one and use it.
  • Forgetting to test after reboot. Enabling looks fine, but a service might fail to start because of a config error. Always reboot and verify.
  • Using SysV init commands. Commands like update-rc.d or chkconfig are old. On systemd you must use systemctl enable.

Tips

  • Disable a service with sudo systemctl disable nginx if you later want to stop it from auto-starting.
  • Start a service immediately without waiting for reboot: sudo systemctl start nginx.
  • Check dependency order. systemd handles ordering automatically, so you don’t need to worry about MySQL starting before PHP-FPM. But if you have custom services that need the database, you can add After=mysql.service to their unit files.
  • Mask vs disable. If a service keeps starting despite being disabled, it may be pulled in by another unit. Use sudo systemctl mask nginx to completely block it (undo with unmask).
  • Use a single command. You can enable multiple services at once: sudo systemctl enable nginx mysql php-fpm.

Troubleshooting

  • Service shows ‘enabled’ but doesn’t start after reboot. Check the logs: journalctl -u nginx -b (or mysql/php-fpm). Look for config errors, port conflicts, or permission issues. Fix the underlying problem, then sudo systemctl start nginx to test.
  • MySQL fails to start because of a missing data directory. Run sudo mysqld --initialize (or mariadb-install-db for MariaDB) to create it, then start the service.
  • PHP-FPM starts but Nginx returns 502 Bad Gateway. Check that the PHP-FPM socket path in your Nginx config matches the one in php-fpm.d/www.conf. Often it’s unix:/run/php/php8.1-fpm.sock – adjust Nginx’s fastcgi_pass line accordingly.
  • Service is enabled but not starting because it’s masked. Run systemctl list-unit-files | grep masked. If you see your service, unmask it with sudo systemctl unmask nginx.
  • After reboot, services start but your site is down. Check if the firewall is blocking ports. Run sudo ufw status (Ubuntu) or sudo firewall-cmd --list-all (CentOS). Allow HTTP/HTTPS if needed.

Frequently Asked Questions

Do I need to enable services in a specific order?

No. systemd resolves dependencies automatically. Nginx will start after the network is up, and PHP-FPM will start before Nginx if your Nginx unit has a dependency on it (which it usually does).

What’s the difference between ‘enable’ and ‘start’?

enable makes the service start at boot. start launches it right now. You usually want both – enable for future boots, start for the current session.

How do I know the exact service name for PHP-FPM?

Run systemctl list-unit-files | grep fpm. You’ll see something like php8.1-fpm.service. Use that full name (without the .service extension) in systemctl commands.

Can I use these steps on a system without systemd?

No. Older systems use SysV init or Upstart. You’d need to edit /etc/rc.local or use update-rc.d instead. This guide is specifically for systemd.

What if I want to start services only after the network is ready?

systemd already handles that for standard services. If you have a custom service, add After=network-online.target and Wants=network-online.target to its unit file.

Is it safe to enable all three at boot?

Yes, that’s the standard setup for a web server. It ensures your site comes back automatically after a reboot, which is what most production environments want.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →