How to Install PHP and MySQL on Your VPS — A Fast, Step-by-Step Guide

How to Install PHP and MySQL on Your VPS — A Fast, Step-by-Step Guide

Ready to take control of your server? This fast, step-by-step guide shows how to install PHP and MySQL on your VPS, finishing with a hardened, performant setup for WordPress, custom apps, or APIs.

Setting up PHP and MySQL on your VPS is one of the most common tasks for webmasters, developers, and businesses hosting dynamic applications. This guide walks you through a fast, pragmatic, and secure installation on a typical Linux VPS, explains the underlying principles, compares common stack choices, and gives practical selection and optimization advice for production use. The steps are presented so you can follow along on a fresh VPS instance and finish with a hardened, performant environment ready for WordPress, custom PHP apps, or other web services.

Why PHP and MySQL on a VPS?

PHP and MySQL remain a dominant combination for dynamic websites and applications because of their maturity, ecosystem, and wide hosting support. On a VPS you get full control of configuration, resources, and security—advantages shared by developers and businesses needing predictable performance, custom PHP extensions, or isolation from noisy neighbors.

Common use cases

  • Hosting WordPress, Drupal, Joomla or other PHP-based CMS platforms.
  • Custom web applications built with frameworks (Laravel, Symfony, CodeIgniter).
  • APIs and microservices serving JSON payloads from PHP backends.
  • Development and staging environments mirroring production stacks.

Fundamental components and architecture

When people say “install PHP and MySQL” they generally mean deploying a web server and connecting a PHP runtime to a database server. Typical architectures:

  • LAMP: Linux + Apache + MySQL (or MariaDB) + PHP (often mod_php).
  • LEMP: Linux + Nginx + MySQL/MariaDB + PHP-FPM (FastCGI Process Manager).

Both are valid. Apache+mod_php simplifies configuration for some setups (PHP runs inside the web server process), while Nginx+PHP-FPM is preferred for higher concurrency and lower memory footprint because Nginx handles static files and proxies PHP requests to dedicated PHP-FPM workers.

Prerequisites and recommended VPS specs

Before starting, ensure you have:

  • An up-to-date VPS with SSH access (root or sudo user).
  • A supported Linux distribution: Ubuntu LTS (22.04 or 20.04) or Debian 11/12 are common choices. Commands below use Ubuntu/Debian syntax.
  • At least 1 GB RAM for testing, 2+ GB for production with moderate traffic. More RAM and CPU cores for larger projects.

Choosing a VPS provider and plan matters for performance and latency. For U.S.-based audiences consider providers with data centers in the USA, such as the USA VPS product on VPS.DO which offers varied resource tiers and locations.

Step-by-step installation (Ubuntu/Debian)

These commands assume sudo access. Update packages first.

sudo apt update && sudo apt upgrade -y

1. Install the web server (Nginx)

Nginx is the recommended web server for modern deployments:

sudo apt install -y nginx

Enable and start Nginx:

sudo systemctl enable --now nginx

2. Install MySQL (or MariaDB)

MySQL is widely used, but MariaDB is a drop-in alternative maintained by the community. To install MySQL 8:

sudo apt install -y mysql-server

Secure the installation and set the root password:

sudo mysql_secure_installation

Follow prompts to remove anonymous users, disallow root remote login (unless needed), remove test DB, and reload privileges.

3. Install PHP and PHP-FPM

Install PHP 8.x and commonly required modules (adjust version per OS repository):

sudo apt install -y php-fpm php-cli php-mysql php-xml php-mbstring php-curl php-zip php-gd

Check PHP-FPM status:

sudo systemctl enable --now php8.1-fpm

(Replace 8.1 with the version installed.)

4. Configure Nginx to use PHP-FPM

Create or edit a server block in /etc/nginx/sites-available/yourdomain:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/html/example.com;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    location ~ /.ht {
        deny all;
    }
}

Enable the site and reload nginx:

sudo ln -s /etc/nginx/sites-available/yourdomain /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

5. Test PHP and Database connectivity

Create a PHP info file in your web root:

echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/example.com/info.php

Visit http://example.com/info.php to confirm PHP is running. Remove this file after testing for security.

To test MySQL connectivity from PHP, create a small script that connects using mysqli or PDO and reports success. Alternatively, use mysql client:

sudo mysql -u root -p

then in MySQL: SHOW DATABASES;

Security and hardening

Security is critical on a VPS. Apply these steps:

  • Firewall: Use UFW to allow only needed ports (80, 443, SSH). Example:
sudo apt install ufw
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
  • SSH: Disable root login, use key-based auth, and consider changing the default port or using fail2ban.
  • MySQL: Keep remote access disabled unless necessary; use strong passwords and least-privilege DB users.
  • Updates: Apply security updates promptly. Consider unattended-upgrades for automated patching.
  • HTTPS: Always serve production sites over TLS. Use Let’s Encrypt certbot for free certificates:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Performance tuning and optimization

After functional setup, tune for performance:

  • PHP-FPM tuning: Configure pm.max_children, pm.start_servers, and pm.max_requests in /etc/php/8.1/fpm/pool.d/www.conf according to available RAM and average memory per PHP process.
  • OPcache: Enable and configure PHP OPcache for bytecode caching (improves response times).
  • MySQL tuning: Use tools like mysqltuner.pl and customize innodb_buffer_pool_size (typically 60–80% of available RAM on a dedicated DB server).
  • Static file handling: Let Nginx serve static assets and apply proper cache headers and gzip compression.
  • Use a CDN: For globally distributed audiences, combine your VPS origin with a CDN for assets and caching.

MySQL vs MariaDB, PHP-FPM vs mod_php — quick comparison

Choosing components affects maintenance and performance:

  • MySQL: Official Oracle-backed distribution, strong support and ecosystem compatibility. Good for managed hosting and enterprise features.
  • MariaDB: Community-driven fork with performance tweaks and extensions. Mostly compatible; some edge-case differences in versions/features exist.
  • PHP-FPM: Recommended with Nginx; isolates PHP processes, better for high concurrency and lower memory per connection.
  • mod_php: Simpler for Apache setups and some legacy apps. However, it consumes more memory because each Apache worker embeds PHP.

Backup, monitoring, and maintenance

Operational reliability requires:

  • Automated backups (database dumps and file system snapshots). Schedule mysqldump or use Percona XtraBackup for larger datasets.
  • Monitoring: Use tools like Prometheus/Grafana, Netdata, or hosted monitoring to watch CPU, memory, disk I/O, and DB performance.
  • Log rotation for Nginx and PHP logs, and secure retention policies to prevent disk exhaustion.
  • Disaster recovery testing: Periodically restore backups to a staging server to validate the recovery process.

Choosing the right VPS for PHP + MySQL hosting

When selecting a VPS plan consider:

  • Memory: PHP-FPM and MySQL both benefit from RAM—prioritize memory over CPU for database-heavy workloads.
  • Disk type: Use SSD or NVMe for faster random I/O. Databases are sensitive to disk latency.
  • Network: Low-latency network and bandwidth matter if you serve many external API calls or large assets.
  • Scalability: Look for easy vertical scaling or snapshot/backup features. Consider separate DB instances for larger deployments.

Conclusion

Installing PHP and MySQL on a VPS is straightforward with a clear set of steps: install and configure a web server, set up a database, deploy PHP-FPM, secure the instance, and then tune for performance. For production systems, prioritize proper hardening, backups, and monitoring, and choose a VPS with enough memory, fast disk I/O, and a host location close to your users.

If you need reliable U.S.-based VPS hosting with flexible resource tiers and SSD storage, consider exploring the USA VPS options on VPS.DO to find a plan that matches your PHP and MySQL workload needs.

Fast • Reliable • Affordable VPS - DO It Now!

Get top VPS hosting with VPS.DO’s fast, low-cost plans. Try risk-free with our 7-day no-questions-asked refund and start today!