Skip to main content

Command Palette

Search for a command to run...

Setting Up a PHP Backend with MySQL, Composer, and Nginx on Ubuntu

Deploy a PHP backend with MySQL,Composer, and Nginx on Ubuntu.

Published
4 min read

This comprehensive guide walks you through building a development environment for PHP applications with a MySQL database, Composer dependency management, and Nginx web server on Ubuntu. We'll cover installing necessary packages, securing the database, creating a user and database, installing Composer, and setting up Nginx.

Prerequisites:

  • An Ubuntu system with administrative privileges (sudo access)

  • Basic familiarity with the terminal

Steps:

  1. Update Package Lists:

    Bash

     sudo apt update
    

    This refreshes the system's package information, ensuring we install the latest versions.

  2. System Upgrade (Optional):

    Bash

     sudo apt upgrade -y
    

    This step is optional, but it's recommended to keep your system and packages up-to-date for security and bug fixes.

  3. Clone Your Project (if applicable):

    If you're working with a version control system like Git, clone your project repository:

    Bash

     git clone https://github.com/akramul140111/qr-app-backend.git
    

    Replace the provided URL with the actual URL of your repository.

  4. Add PHP Repository:

    Bash

     sudo add-apt-repository ppa:ondrej/php
     sudo apt update
    

    These commands add a repository maintained by Ondřej Surý, which provides newer PHP versions often unavailable in the default Ubuntu repositories. After adding the repository, update the package lists again.

  5. Install PHP and Extensions:

    Bash

     sudo apt install php8.2 php8.2-mysql php8.2-intl php8.2-curl php8.2-mbstring php8.2-xml php8.2-zip php8.2-ldap php8.2-gd php8.2-bz2 php8.2-sqlite3 php8.2-redis php8.2-fpm
    

    This installs PHP 8.2, along with essential extensions like php8.2-mysql (for database connectivity), php8.2-intl (for internationalization), php8.2-curl (for making HTTP requests), and more. You can choose specific extensions based on your project's requirements.

  6. Install MySQL Server:

    Bash

     sudo apt install mysql-server -y
    

    This installs the MySQL database server.

  7. Start and Enable MySQL Services:

    Bash

     sudo systemctl start mysql.service
     sudo systemctl enable mysql.service
    

    These commands start the MySQL service immediately and configure it to start automatically at system boot.

  8. Secure MySQL (Important!):

    Bash

     sudo mysql_secure_installation
    

    This script guides you through securing your MySQL installation. Here are the recommended responses (enter y or Y for "yes" and press Enter):

    • Change the root password? (y/n) y

    • Set a root password: Enter your desired secure password and retype it for confirmation.

    • Remove anonymous users? (y/n) y

    • Disallow root login remotely? (y/n) y (unless you specifically need remote access)

    • Remove test database and access to it? (y/n) y

    • Reload privilege tables now? (y/n) y

  9. Create MySQL User and Database:

    Bash

     sudo mysql -u root -p
    

    Enter the MySQL root password you set in step 8 when prompted.

    Inside the MySQL shell:

    SQL

     mysql> CREATE USER 'uy'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mohi@lnx';
     mysql> GRANT ALL PRIVILEGES ON *.* TO 'uy'@'localhost' WITH GRANT OPTION;
     CREATE DATABASE DevOps;
     FLUSH PRIVILEGES;
     exit;
    
    • This creates a user named uy with the password mohi@lnx and grants them full privileges on all databases. You can adjust the username, password, and privileges according to your project's security needs.

    • The CREATE DATABASE DevOps command creates a database named DevOps, which you can replace with your preferred name.

  10. Install Composer (Package Dependency Manager):

Bash

sudo apt update
sudo apt install php8.2-cli unzip

These commands install the PHP command-line

Instructions on installing Nginx on Ubuntu:

Steps:

  1. Update Package Lists:

    Bash

     sudo apt update
    

    This refreshes the system's package information, ensuring you install the latest version of Nginx.

  2. Install Nginx:

    Bash

     sudo apt install nginx
    

    This command uses the apt package manager to install the nginx package.

  3. Verify Nginx Installation (Optional):

    -Check service status:

    Bash

     sudo systemctl status nginx.service
    

    This command displays the status of the Nginx service, indicating whether it's running, stopped, or encountering any errors.

    -Test basic web server functionality:

    Open a web browser and navigate to http://localhost or the IP address of your server. If Nginx is running correctly, you should see the default Nginx welcome page.

Additional Notes:

  • The default installation directory for Nginx on Ubuntu systems is typically /etc/nginx. This is where configuration files and logs are stored.

  • You can manage the Nginx service using systemctl:

    • To start Nginx: sudo systemctl start nginx.service

    • To stop Nginx: sudo systemctl stop nginx.service

    • To restart Nginx (attempting a graceful stop and start): sudo systemctl restart nginx.service