- Update the package index and upgrade the existing packages on the server:
sudo apt update
sudo apt upgrade
- Install Nginx:
sudo apt install nginx
- Allow Nginx through the firewall:
sudo ufw allow 'Nginx HTTP'
- Check if Nginx is running by visiting your server’s IP address in a web browser:
http://your_server_ip_address
- Install MySQL:
sudo apt install mysql-server
- Run a secure installation of MySQL and set the root password:
sudo mysql_secure_installation
- Install PHP and necessary extensions:
sudo apt install php-fpm php-mysql
- Configure Nginx to use PHP:
sudo nano /etc/nginx/sites-available/default
Replace the contents of the file with the following:
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
server_name your_domain;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
Save and close the file. - Restart Nginx:
sudo systemctl restart nginx
- Create a test PHP file:
sudo nano /var/www/html/info.php
Add the following code and save the file:
<?php
phpinfo();
?> - Visit the test PHP file in a web browser by entering the following URL:
http://your_server_ip_address/info.php
- Delete the test PHP file after verifying the installation:
sudo rm /var/www/html/info.php