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 printsystemd. - 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
- Check current status. Run
sudo systemctl status nginx mysql php-fpm(ormariadbinstead ofmysql). Note whether each isenabledordisabled. The goal is to seeenabledfor all three. - Enable Nginx. Run
sudo systemctl enable nginx. This creates symlinks in/etc/systemd/system/multi-user.target.wants/so the service starts at boot. - Enable MySQL/MariaDB. Run
sudo systemctl enable mysqlorsudo systemctl enable mariadb, depending on which you installed. If you’re not sure, runsystemctl list-unit-files | grep -E 'mysql|mariadb'to see the exact service name. - Enable PHP-FPM. Run
sudo systemctl enable php-fpm. On some distros the service is namedphp7.4-fpm,php8.1-fpm, etc. Find yours withsystemctl list-unit-files | grep fpmand use that exact name. - Verify all three are enabled. Run
systemctl is-enabled nginx mysql php-fpm(or the correct names). Each should printenabled. If any printsdisabledornot-found, re-check the service name and repeat the enable step. - Reboot to test. Run
sudo reboot. After the server comes back, log in and runsystemctl status nginx mysql php-fpm(ormariadb). All three should showactive (running). You can also check that your web page loads and that PHP scripts work.
Common Mistakes
- Wrong service name. Using
mysqlwhen you installed MariaDB, orphp-fpmwhen your distro names itphp8.2-fpm. Always check withsystemctl 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.dorchkconfigare old. On systemd you must usesystemctl enable.
Tips
- Disable a service with
sudo systemctl disable nginxif 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.serviceto 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 nginxto completely block it (undo withunmask). - 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, thensudo systemctl start nginxto test. - MySQL fails to start because of a missing data directory. Run
sudo mysqld --initialize(ormariadb-install-dbfor 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’sunix:/run/php/php8.1-fpm.sock– adjust Nginx’sfastcgi_passline 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 withsudo systemctl unmask nginx. - After reboot, services start but your site is down. Check if the firewall is blocking ports. Run
sudo ufw status(Ubuntu) orsudo 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
- China’s IPv9 Decimal Network Project Explained
- Fix Wired Earbuds That Only Work in One Ear
- What to Check Before Buying the Dip in Indian Shares
- Cool Raspberry Pi Projects: 15 Ideas for Any Skill Level3
- Terraform vs Kubernetes: Key Differences
- Internet Connectivity in Remote Areas: Options0.80 USD/GB