Setting Up a PHP Backend with MySQL, Composer, and Nginx on Ubuntu
Deploy a PHP backend with MySQL,Composer, and Nginx on Ubuntu.
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:
Update Package Lists:
Bash
sudo apt updateThis refreshes the system's package information, ensuring we install the latest versions.
System Upgrade (Optional):
Bash
sudo apt upgrade -yThis step is optional, but it's recommended to keep your system and packages up-to-date for security and bug fixes.
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.gitReplace the provided URL with the actual URL of your repository.
Add PHP Repository:
Bash
sudo add-apt-repository ppa:ondrej/php sudo apt updateThese 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.
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-fpmThis 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.Install MySQL Server:
Bash
sudo apt install mysql-server -yThis installs the MySQL database server.
Start and Enable MySQL Services:
Bash
sudo systemctl start mysql.service sudo systemctl enable mysql.serviceThese commands start the MySQL service immediately and configure it to start automatically at system boot.
Secure MySQL (Important!):
Bash
sudo mysql_secure_installationThis script guides you through securing your MySQL installation. Here are the recommended responses (enter
yorYfor "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
Create MySQL User and Database:
Bash
sudo mysql -u root -pEnter 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
uywith the passwordmohi@lnxand 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 DevOpscommand creates a database namedDevOps, which you can replace with your preferred name.
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:
Update Package Lists:
Bash
sudo apt updateThis refreshes the system's package information, ensuring you install the latest version of Nginx.
Install Nginx:
Bash
sudo apt install nginxThis command uses the
aptpackage manager to install thenginxpackage.Verify Nginx Installation (Optional):
-Check service status:
Bash
sudo systemctl status nginx.serviceThis 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://localhostor 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.serviceTo stop Nginx:
sudo systemctl stop nginx.serviceTo restart Nginx (attempting a graceful stop and start):
sudo systemctl restart nginx.service
