How to Setup Webserver On Ubuntu 22.04 with NGINX PHP MYSQL

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.

  1. Install Nginx: sudo apt install nginx -y
  2. Enable and start the Nginx service:
    sudo systemctl enable nginx
    sudo systemctl start nginx
  3. Allow Nginx through the firewall:
    sudo ufw allow 'Nginx Full'
    sudo ufw enable
  4. 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.

  1. Install MySQL:
    sudo apt install mysql-server -y
  2. 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.
  3. Verify MySQL is running:
    sudo systemctl status mysql

Step 4: Install PHP 8.2

PHP is required to process dynamic content.

  1. 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
  2. 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
  3. Verify PHP installation:
    php -v
  4. 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.

  1. Remove the default configuration:
    sudo rm /etc/nginx/sites-enabled/default
  2. Create a new Nginx server block:
    sudo nano /etc/nginx/sites-available/example.com
    Replace example.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;
    }
    }
  3. Save the file (CTRL + X, then Y, then ENTER).
  4. Enable the new site:
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
  5. Test Nginx configuration:
    sudo nginx -t
  6. Reload Nginx to apply changes:
    sudo systemctl reload nginx

Step 6: Test PHP

  1. Create a PHP info page:
    sudo nano /var/www/html/info.php
  2. Add the following content:
    <?php phpinfo(); ?>
  3. 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

  1. Remove the test PHP file (for security reasons):
    sudo rm /var/www/html/info.php
  2. Enable the UFW firewall:
    sudo ufw allow OpenSSH
    sudo ufw allow 'Nginx Full'
    sudo ufw enable
  3. 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! 🚀

Leave a Comment