Current Location: Home> Latest Articles> How to Create RPM Packages for PHP Applications: Complete Guide

How to Create RPM Packages for PHP Applications: Complete Guide

gitbox 2025-07-30

In modern software development, creating and distributing software packages is an important task. If you're a PHP developer looking to package your application into an RPM (Red Hat Package Manager) package for distribution and installation in a Linux environment, this guide will walk you through the entire process.

What is an RPM Package?

RPM packages are the package format used by Red Hat and its derivatives (such as CentOS, Fedora, etc.). With RPM packages, you can easily distribute, install, update, and remove applications.

Preparation

Before starting, make sure you have the following software installed:

  • PHP: Ensure that your PHP application is working properly.
  • RPM build tools: Install the RPM build tools with the following command:
<span class="fun">sudo yum install rpm-build rpmdevtools</span>

Steps to Create an RPM Package

Set Up the Environment

First, you need to set up the RPM build environment. Run the following command to create the necessary directory structure:

<span class="fun">rpmdev-setuptree</span>

This will create a directory called ~/rpmbuild, with the following subdirectories:

  • BUILD
  • RPMS
  • SRPMS
  • SOURCES

Write the Spec File

Next, you need to create a spec file, which defines the contents and installation process of the RPM package. The spec file is located in the ~/rpmbuild/SPECS directory. You can create a new file using a text editor, like: myapp.spec.

Here is an example of a basic spec file:

Name: myapp
Version: 1.0
Release: 1%{?dist}
Summary: My PHP Application
License: MIT
Source0: %{name}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-root
Description: My PHP application does amazing things.
%prep
%setup -q
%build
%install
mkdir -p %{buildroot}/var/www/html/myapp
cp -a * %{buildroot}/var/www/html/myapp
%post
echo "My PHP application installed."
%clean
rm -rf %{buildroot}
%files
/var/www/html/myapp

Package the Source Code

Once you have prepared the application contents and spec file, you need to package the source code into a .tar.gz file and place it in the ~/rpmbuild/SOURCES directory. Use the following command to create the compressed file:

<span class="fun">tar -czvf myapp-1.0.tar.gz myapp/</span>

Make sure to replace myapp/ with your PHP application's directory.

Build the RPM Package

Now that everything is set up, you can use the rpmbuild command to build the RPM package. Run the following command:

<span class="fun">rpmbuild -ba ~/rpmbuild/SPECS/myapp.spec</span>

If there are no errors, the RPM package will be generated in the ~/rpmbuild/RPMS/noarch/ directory.

Test and Distribute the RPM Package

Once the RPM package is created, you can test it in a Linux environment to ensure it installs and runs correctly. Use the following command to install the RPM package:

<span class="fun">sudo rpm -ivh myapp-1.0-1.noarch.rpm</span>

With these steps, you can easily package your PHP application into an RPM package and distribute and install it on Linux systems.

Conclusion

By following the steps in this guide, you should be able to create RPM packages for your PHP applications with ease. Remember to regularly check and update your package to provide users with the best experience. Creating RPM packages is no longer a complex task when you follow the steps outlined above.