Setting up a web server on Ubuntu 22.04 using Nginx, PHP 8.2, and MySQL Server requires several steps. Follow this guide to install and configure everything correctly.
Step 1: Update Your System
Before installing any software, update your system packages:
sudo apt update && sudo apt upgrade -y
Step 2: Install Nginx
Nginx is a lightweight and high-performance web server.
- Install Nginx:
sudo apt install nginx -y
- Enable and start the Nginx service:
sudo systemctl enable nginx
sudo systemctl start nginx
- Allow Nginx through the firewall:
sudo ufw allow 'Nginx Full'
sudo ufw enable
- Verify Nginx installation by opening your browser and visiting:
http://your-server-ip
You should see the default Nginx welcome page.
Step 3: Install MySQL Server
MySQL is a popular database management system.
- Install MySQL:
sudo apt install mysql-server -y
- Secure MySQL installation:
sudo mysql_secure_installation
Follow the prompts:- Set a strong root password.
- Remove anonymous users.
- Disable remote root login (unless needed).
- Remove test database.
- Reload privilege tables.
- Verify MySQL is running:
sudo systemctl status mysql
Step 4: Install PHP 8.2
PHP is required to process dynamic content.
- Add the Sury PPA repository for PHP 8.2:
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
- Install PHP 8.2 with necessary extensions:
sudo apt install php8.2 php8.2-fpm php8.2-mysql php8.2-cli php8.2-curl php8.2-gd php8.2-xml php8.2-mbstring php8.2-zip unzip -y
- Verify PHP installation:
php -v
- Enable and start PHP-FPM:
sudo systemctl enable php8.2-fpm
sudo systemctl start php8.2-fpm
Step 5: Configure Nginx to Use PHP
Now, configure Nginx to serve PHP files.
- Remove the default configuration:
sudo rm /etc/nginx/sites-enabled/default
- Create a new Nginx server block:
sudo nano /etc/nginx/sites-available/example.com
Replaceexample.com
with your domain or IP and add the following configuration:server {
listen 80; server_name example.com www.example.com;
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/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
}
location ~ /\.ht
{
deny all;
}
}
- Save the file (
CTRL + X
, thenY
, thenENTER
). - Enable the new site:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
- Test Nginx configuration:
sudo nginx -t
- Reload Nginx to apply changes:
sudo systemctl reload nginx
Step 6: Test PHP
- Create a PHP info page:
sudo nano /var/www/html/info.php
- Add the following content:
<?php phpinfo(); ?>
- Save the file and visit:
http://your-server-ip/info.php
I
f PHP is working, you’ll see the PHP info page.
Step 7: Secure Your Server
- Remove the test PHP file (for security reasons):
sudo rm /var/www/html/info.php
- Enable the UFW firewall:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
- Set up an SSL Certificate (Optional, Recommended)
If you have a domain, install Let’s Encrypt SSL:sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
Auto-renew SSL:sudo systemctl enable certbot.timer
Step 8: Install phpMyAdmin (Optional)
If you need a web-based database management tool, install phpMyAdmin:
sudo apt install phpmyadmin -y
During installation:
- Select Apache (even though you’re using Nginx) and press TAB to continue.
- Select No when asked to configure the database automatically.
Now, configure Nginx to serve phpMyAdmin:
sudo ln -s /usr/share/phpmyadmin /var/www/html
Visit:
http://your-server-ip/phpmyadmin
Final Step: Reboot the Server
sudo reboot
Your Nginx, PHP 8.2, and MySQL web server is now fully set up on Ubuntu 22.04! 🚀