DEV Community

Mehedi Hasan
Mehedi Hasan

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode
  1. Install Nginx:
   sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode
  1. After installation, Nginx will automatically start. To verify that it's running, use:
   sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Configure Firewall (Optional)

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

sudo ufw allow 'Nginx Full'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Check PHP version:

After installation, verify the PHP version by running:

   php -v
Enter fullscreen mode Exit fullscreen mode
  1. Configure PHP with Nginx:

Open the Nginx configuration file:

   sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

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;
   }
Enter fullscreen mode Exit fullscreen mode

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

   sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Secure MySQL Installation:

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

   sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

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

  1. Log in to MySQL:

Once secured, log in as the root user:

   sudo mysql -u root -p
Enter fullscreen mode Exit fullscreen mode
  1. 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;
Enter fullscreen mode Exit fullscreen mode

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

   exit;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

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.

  1. Test SSL Renewal:

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

   sudo certbot renew --dry-run
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Download Adminer:

Download the latest Adminer file using wget:

   sudo wget "https://www.adminer.org/latest.php" -O /var/www/adminer/index.php
Enter fullscreen mode Exit fullscreen mode
  1. Configure Nginx for Adminer:

Create a new Nginx server block for Adminer:

   sudo nano /etc/nginx/sites-available/adminer
Enter fullscreen mode Exit fullscreen mode

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;
       }
   }
Enter fullscreen mode Exit fullscreen mode

Enable the site by creating a symbolic link:

   sudo ln -s /etc/nginx/sites-available/adminer /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
  1. Test and reload Nginx:
   sudo nginx -t
   sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Check Composer Installation:

Verify that Composer was installed successfully by checking its version:

   composer --version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Create a new Nginx server block:
   sudo nano /etc/nginx/sites-available/your_domain
Enter fullscreen mode Exit fullscreen mode

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;
       }
   }
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

Removing a Domain

To remove a domain from your Nginx server:

  1. Disable the server block:
   sudo rm /etc/nginx/sites-enabled/your_domain
Enter fullscreen mode Exit fullscreen mode
  1. Remove the configuration and website files:


   sudo rm /etc/nginx/sites-available/your_domain
   sudo rm -rf /var/www/your_domain
Enter fullscreen mode Exit fullscreen mode
  1. Reload Nginx:
   sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)