Setting up a robust server stack is crucial for hosting websites or web applications. Nginx, PHP, and MySQL provide a reliable combination for serving dynamic content. This guide explains how to set up Nginx, PHP, and MySQL on a Linux VPS step by step.
Prerequisites
Before you begin, ensure you have:
-
A VPS: Choose a reliable VPS provider like Hosteons.
-
Linux OS: Use Ubuntu 20.04 or Debian 11 for best results (other distributions may require adjustments).
-
Root Access: Ensure administrative privileges on your VPS.
Step 1: Update Your Server
Start by updating your server to ensure all packages and security patches are up-to-date:
sudo apt update && sudo apt upgrade -y
Step 2: Install Nginx
Nginx is a lightweight, high-performance web server. Install it using:
sudo apt install nginx -y
After installation, start and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
You can verify the installation by visiting your VPS’s IP address in a web browser. You should see the Nginx welcome page.
Step 3: Install MySQL
MySQL is a popular database management system. Install it with:
sudo apt install mysql-server -y
Secure the installation by running:
sudo mysql_secure_installation
Follow the prompts to set a root password, remove test databases, and configure other security settings.
Step 4: Install PHP
PHP is a server-side scripting language used to generate dynamic web content. Install PHP along with necessary extensions:
sudo apt install php-fpm php-mysql -y
Step 5: Configure Nginx to Use PHP
By default, Nginx doesn’t process PHP files. Configure it to pass PHP requests to PHP-FPM:
-
Edit the default Nginx server block configuration:
sudo nano /etc/nginx/sites-available/default
-
Modify the file to include:
server {
listen 80;
server_name your_domain_or_IP;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
-
Save the file and test the Nginx configuration:
sudo nginx -t
-
Reload Nginx:
sudo systemctl reload nginx
Step 6: Test PHP
To verify PHP is working:
-
Create a test file:
sudo nano /var/www/html/info.php
-
Add the following content:
<?php
phpinfo();
?>
-
Save the file and visit
http://your_server_ip/info.php
in a web browser. You should see a PHP information page.
Step 7: Secure Your Server
-
Remove the PHP Info File: Once tested, delete the file to prevent unauthorized access:
sudo rm /var/www/html/info.php
-
Set Up a Firewall: Allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx Full'
sudo ufw enable
-
Enable SSL: Secure your server with HTTPS using Let’s Encrypt:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain
Follow the prompts to set up SSL certificates.
Conclusion
You now have a fully configured server stack with Nginx, PHP, and MySQL, ready to host your websites or applications. This setup provides a powerful and efficient foundation for your web hosting needs. Hosteons’ VPS solutions ensure reliable performance and support to keep your applications running smoothly.