To install PHP and MySQL on a cloud server, you need to run a well-defined sequence of commands, secure each service when done, and verify everything works before deploying your application. This guide covers Ubuntu 24.04 and AlmaLinux 9 — the two most-used distributions in cloud servers — with exact commands and concise explanations.
Prerequisites
Before you start, confirm that you have:
- SSH access with a user that has
sudoprivileges. - An updated operating system (
apt update && apt upgrade -yon Ubuntu;dnf update -yon AlmaLinux). - A basic firewall active (UFW or firewalld) allowing port 22 for SSH.
- At least 1 GB of RAM (2 GB recommended to run MySQL + PHP without resource pressure).
If you just created your server and haven't secured it yet, check the cloud setup resources on our blog before continuing.
Step 1 — Install the Web Server (Apache or Nginx)
PHP needs a web server to pass requests to it. Apache is the classic choice; Nginx is more efficient under high concurrency. This guide uses Apache as an example, but the PHP and MySQL steps are identical with Nginx.
Ubuntu 24.04
sudo apt install -y apache2
sudo systemctl enable --now apache2
AlmaLinux 9
sudo dnf install -y httpd
sudo systemctl enable --now httpd
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
Verify the server responds by opening http://YOUR_IP in your browser — you should see the Apache welcome page.
Step 2 — Install PHP on Your Cloud Server
PHP powers WordPress, Laravel, Drupal, and the vast majority of CMS platforms. The currently recommended version (with active support) is PHP 8.3.
Ubuntu 24.04 — PHP with Essential Modules
sudo apt install -y php libapache2-mod-php php-mysql php-curl \
php-gd php-mbstring php-xml php-zip php-intl
Reload Apache to activate the module:
sudo systemctl restart apache2
AlmaLinux 9 — PHP from the Remi Repository
AlmaLinux 9 includes PHP 8.1 in its base repos. To get PHP 8.3, use the Remi repository:
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.3 -y
sudo dnf install -y php php-mysqlnd php-curl php-gd \
php-mbstring php-xml php-zip php-intl
sudo systemctl restart httpd
Verify the PHP Installation
Create a test file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Open http://YOUR_IP/info.php in the browser. You should see the PHP configuration table with the installed version.
Important: delete that file as soon as you finish testing — it exposes sensitive server information.
sudo rm /var/www/html/info.php
Step 3 — Install MySQL (MariaDB) on the Cloud Server
MariaDB is the community fork of MySQL — 100 % compatible and preferred on most modern Linux servers. To install it:
Ubuntu 24.04
sudo apt install -y mariadb-server mariadb-client
sudo systemctl enable --now mariadb
AlmaLinux 9
sudo dnf install -y mariadb-server mariadb
sudo systemctl enable --now mariadb
Secure the MariaDB Installation
Run the included security wizard:
sudo mysql_secure_installation
The wizard walks you through these steps:
- Root password: set a strong password and store it in your password manager.
- Anonymous users: answer Y to remove them.
- Remote root login: answer Y to disable it.
- Test database: answer Y to remove it.
- Reload privileges: answer Y.
Verify you can log into the client:
sudo mysql -u root -p
Once inside, exit with \q.
Step 4 — Create a Database and User for Your Application
Never use the MariaDB root user for your web application. Create a dedicated user with permissions only on the database it needs:
sudo mysql -u root -p
Inside the MySQL client, run:
CREATE DATABASE my_app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON my_app.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace my_app, app_user, and the password with your own values. The user only has access to that database — if the application is compromised, the attacker cannot touch other databases on the server.
Quick Comparison: Ubuntu vs AlmaLinux for PHP + MySQL
| Feature | Ubuntu 24.04 LTS | AlmaLinux 9 |
|---|---|---|
| PHP in base repos | PHP 8.3 | PHP 8.1 (8.3 with Remi) |
| Package manager | apt / dpkg | dnf / rpm |
| Default firewall | UFW | firewalld |
| Support until | 2029 | 2032 |
| Learning curve | Lower | Medium (cPanel-friendly) |
Step 5 — Integration Test: PHP + MySQL
Before uploading your application, confirm that PHP can connect to MariaDB. Create a temporary file:
sudo nano /var/www/html/test_db.php
Paste this content:
<?php
$conn = new mysqli('localhost', 'app_user', 'StrongPassword123!', 'my_app');
if ($conn->connect_error) {
die('Connection error: ' . $conn->connect_error);
}
echo 'MySQL connection successful. Version: ' . $conn->server_info;
$conn->close();
Open http://YOUR_IP/test_db.php in the browser. If you see the success message with the MariaDB version, your stack is ready.
Delete the test file immediately:
sudo rm /var/www/html/test_db.php
Key Takeaways
- Install the web server first, then PHP, then MariaDB — in that order you avoid broken dependencies.
- Use PHP 8.3 or higher; 7.x versions no longer receive security updates.
- Always run
mysql_secure_installationright after installing MariaDB. - Create a database user with minimal permissions for each application — never use root.
- Verify PHP-MySQL integration with a temporary script and delete it when done.
- Ubuntu LTS is the easiest entry point; AlmaLinux is preferred in cPanel or enterprise RedHat environments.
Need help with the installation or want someone to do it for you? At elenlace.com we configure your cloud server with PHP and MySQL production-ready, support included.
FAQ
Should I install MySQL or MariaDB on my cloud server?
For most PHP projects, MariaDB is the better choice: it's 100 % compatible with MySQL, performs better in recent benchmarks, and is the default engine on most Linux distributions. Use official MySQL if your application requires exclusive features like MySQL InnoDB Cluster.
Which PHP version should I install?
Install PHP 8.3 or the latest LTS version available. Avoid PHP 7.x: it stopped receiving security patches at the end of 2022 and many modern libraries no longer support it.
Can I install PHP and MySQL on a server with 512 MB of RAM?
Technically yes, but performance will be very tight. MariaDB under load needs at least 256 MB for its buffers; PHP-FPM with a few workers will consume another 128–256 MB. The practical minimum is 1 GB of RAM; with 2 GB you'll have headroom for real traffic.
How do I upgrade PHP to a newer version in the future?
On Ubuntu, add the Ondřej PPA (ppa:ondrej/php) to have all versions in parallel and switch with update-alternatives. On AlmaLinux, change the active Remi module (dnf module reset php && dnf module enable php:remi-X.Y) and reinstall the PHP packages. In both cases, test your application's compatibility before switching in production.
Prefer it done for you? El Enlace handles hosting and professional web development.
Compare providers
Other providers and guides worth comparing: