Setting Up Ubuntu Server: Nginx, PHP, MySQL, SSL Certificates, Composer, and Adminer

Mehedi HasanMehedi Hasan
5 min read

When deploying a web server on Ubuntu, you often need to configure Nginx as the web server, PHP for dynamic content processing, MySQL for database management, and SSL certificates for security. Additionally, tools like Composer for PHP dependency management and Adminer for database administration can significantly improve your development process. This article provides a step-by-step guide on setting up an Ubuntu server with these technologies.


Step 1: Installing the Nginx Web Server

Nginx is a popular web server known for its performance and low resource consumption. The first step in setting up your Ubuntu server is to install Nginx.

  1. Update the package index to ensure your server has the latest package listings:

     sudo apt update
    
  2. Install Nginx:

     sudo apt install nginx
    
  3. After installation, Nginx will automatically start. To verify that it's running, use:

     sudo systemctl status nginx
    

Configure Firewall (Optional)

If your server has UFW (Uncomplicated Firewall) enabled, you need to allow Nginx traffic:

sudo ufw allow 'Nginx Full'

Nginx is now installed and ready for configuration.


Step 2: Installing PHP

PHP is a server-side scripting language used for developing dynamic web applications. In this section, we’ll install PHP 8.1 (or the latest version) along with necessary PHP extensions.

  1. Install PHP and common extensions:

    For PHP 8.1 (latest version):

     sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https curl git -y
     sudo add-apt-repository ppa:ondrej/php
     sudo apt update
     sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-mbstring php8.1-xml -y
    
  2. Check PHP version:

    After installation, verify the PHP version by running:

     php -v
    
  3. Configure PHP with Nginx:

    Open the Nginx configuration file:

     sudo nano /etc/nginx/sites-available/default
    

    Find the PHP section and modify it as follows:

     location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
     }
    

    Save and close the file, then reload Nginx to apply the changes:

     sudo systemctl reload nginx
    

Step 3: Installing MySQL

MySQL is a widely-used database management system. Here’s how you can install and secure MySQL.

  1. Install MySQL along with PHP MySQL extensions:

     sudo apt install mysql-client mysql-server php8.1-mysql
    
  2. Secure MySQL Installation:

    After installation, secure your MySQL server by running the following command:

     sudo mysql_secure_installation
    

    Follow the prompts to set up root password security, remove anonymous users, disallow remote root login, and remove the test database.

  3. Log in to MySQL:

    Once secured, log in as the root user:

     sudo mysql -u root -p
    
  4. Set a Password for MySQL Root User:

    After logging in, switch to the MySQL database and update the root password for additional security:

     USE mysql;
     ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongPassword';
     FLUSH PRIVILEGES;
    

    Replace YourStrongPassword with a secure password of your choice. Then, exit the MySQL console:

     exit;
    

Step 4: Installing and Securing Nginx with Let’s Encrypt (SSL)

To secure your website with SSL (HTTPS), you can use Let's Encrypt, a free and automated certificate authority.

  1. Install Certbot and the Nginx plugin:

     sudo apt install certbot python3-certbot-nginx
    
  2. Obtain and Install SSL Certificate:

    Run the Certbot command to automatically obtain and install an SSL certificate for your domain:

     sudo certbot --nginx -d your_domain -d www.your_domain
    

    Certbot will prompt you to enter an email address and agree to the terms of service. It will automatically configure Nginx to redirect HTTP traffic to HTTPS.

  3. Test SSL Renewal:

    Certbot renews SSL certificates automatically. To test the renewal process, you can run:

     sudo certbot renew --dry-run
    

Step 5: Installing Adminer (Database Management Interface)

Adminer is a lightweight database management tool that can replace phpMyAdmin. It is easy to install and supports MySQL, PostgreSQL, and other databases.

  1. Create a directory for Adminer:

     sudo mkdir /var/www/adminer
    
  2. Download Adminer:

    Download the latest Adminer file using wget:

     sudo wget "https://www.adminer.org/latest.php" -O /var/www/adminer/index.php
    
  3. Configure Nginx for Adminer:

    Create a new Nginx server block for Adminer:

     sudo nano /etc/nginx/sites-available/adminer
    

    Add the following configuration:

     server {
         listen 80;
         server_name adminer.your_domain;
    
         root /var/www/adminer;
         index index.php;
    
         location / {
             try_files $uri $uri/ =404;
         }
    
         location ~ \.php$ {
             include snippets/fastcgi-php.conf;
             fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
         }
     }
    

    Enable the site by creating a symbolic link:

     sudo ln -s /etc/nginx/sites-available/adminer /etc/nginx/sites-enabled/
    
  4. Test and reload Nginx:

     sudo nginx -t
     sudo systemctl reload nginx
    

Now, you can access Adminer via http://adminer.your_domain.


Step 6: Installing Composer (PHP Dependency Manager)

Composer is the most commonly used dependency manager for PHP, making it essential for modern PHP development.

  1. Install Composer:

    First, update your package list and install the required dependencies:

     sudo apt update
     sudo apt install wget php8.1-zip unzip
    

    Then, download and install Composer:

     php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
     HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
     php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
     sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    
  2. Check Composer Installation:

    Verify that Composer was installed successfully by checking its version:

     composer --version
    

Step 7: Managing Nginx Domains

Adding a New Domain

To host multiple websites, you can add additional domains by creating separate server blocks in Nginx.

  1. Create a directory for the new domain:

     sudo mkdir -p /var/www/your_domain/html
     sudo chown -R $USER:$USER /var/www/your_domain/html
     sudo chmod -R 755 /var/www/your_domain
    
  2. Create a new Nginx server block:

     sudo nano /etc/nginx/sites-available/your_domain
    

    Add the following content:

     server {
         listen 80;
         server_name your_domain www.your_domain;
    
         root /var/www/your_domain/html;
         index index.php index.html index.htm;
    
         location / {
             try_files $uri $uri/ =404;
         }
    
         location ~ \.php$ {
             include snippets/fastcgi-php.conf;
             fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
         }
     }
    
  3. Enable the site and reload Nginx:

     sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
     sudo nginx -t
     sudo systemctl reload nginx
    

Removing a Domain

To remove a domain from your Nginx server:

  1. Disable the server block:

     sudo rm /etc/nginx/sites-enabled/your_domain
    
  2. Remove the configuration and website files:

sudo rm /etc/nginx/sites-available/your_domain sudo rm -rf /var/www/your_domain


3. **Reload Nginx**:

```bash
sudo systemctl reload nginx

Conclusion

By following these steps, you will have a fully functional Ubuntu server with Nginx, PHP, MySQL, SSL encryption, Composer for PHP dependency management, and Adminer for database administration. This setup provides a secure, modern, and robust environment for deploying PHP-based web applications.

0
Subscribe to my newsletter

Read articles from Mehedi Hasan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mehedi Hasan
Mehedi Hasan

Passionate and self-taught Software Engineer, known as "Programmer Hasan" with over 5 years of immersive experience in the tech industry. My journey commenced with an insatiable curiosity for technology, evolving into a fulfilling career marked by continuous learning and leadership. Full-Stack Web, Mobile, Desktop Applications Developer & Programmer, Graphic Designer. I have been a very successful web & mobile & windows (.net) developer for over 5 years, working for individuals and companies in Bangladesh mostly. There was a struggle when I started, but that didn't last very long, Alhamdulillah.