How do I install nginx, PHP, and MariaDB on my Smart VPS?

A web server, PHP, and a database in one sitting; the boring, dependable kind of stack.

Steps

You need root or sudo access over SSH. Open ports 80 and 443 in your firewall if you run one (see our firewall article). Point your domain's A record at YOUR.IP.ADDRESS. This article uses nginx; Apache works too.

  1. Update the system. Debian or Ubuntu: apt update && apt upgrade -y. AlmaLinux: dnf upgrade -y.
  2. Install the stack.

    • Debian or Ubuntu: apt install nginx mariadb-server php-fpm php-mysql -y
    • AlmaLinux: dnf install nginx mariadb-server php-fpm php-mysqlnd -y
  3. Start everything and enable it at boot. On AlmaLinux: systemctl enable --now nginx mariadb php-fpm. On Debian and Ubuntu the PHP service name includes a version (such as php8.4-fpm), so find it first with systemctl list-units | grep fpm, then run systemctl enable --now nginx mariadb php8.4-fpm with your version in place of 8.4.
  4. Secure the database with mariadb-secure-installation (older systems call it mysql_secure_installation). Answer its prompts: you can accept the default for switching to unix_socket authentication, set a root password if you want one, remove anonymous users, disallow remote root login, and remove the test database.
  5. Tell nginx to pass .php files to PHP-FPM. Edit your site's server block (Debian and Ubuntu: /etc/nginx/sites-available/default; AlmaLinux: /etc/nginx/conf.d/). On Debian the stock file has the PHP block commented out, so uncomment it or add a block like this one. Run ls /run/php/ to see your socket's real name.

    `` location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.4-fpm.sock; } ``

    On AlmaLinux, use include fastcgi_params; and the socket path from your PHP-FPM pool file.

  6. Test the config and reload: nginx -t && systemctl reload nginx.
  7. Create a test page: echo '<?php phpinfo();' > /var/www/html/info.php. Visit http://YOUR.IP.ADDRESS/info.php (http, not https, until you add a certificate), then delete it with rm /var/www/html/info.php. It reveals a lot about your server.
  8. Create a database and user for your app: run mysql, then CREATE DATABASE appdb;, CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'a-strong-password';, and GRANT ALL ON appdb.* TO 'appuser'@'localhost';.

If it didn't work

  • If apt says "Network is unreachable" for a mirror, just run the command again.
  • nginx -t names the file and line of any config error.
  • journalctl -u nginx -n 50 shows why a service refused to start. On AlmaLinux with SELinux enforcing, check ausearch -m avc -ts recent.
  • Smart VPS is self-managed, so application setup is yours, but open a ticket under Technical Support if the VPS itself seems at fault.


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 0