CakePHP

How To Install CakePHP On An Ubuntu 24.04 or Newer

Welcome to the Greenhost.cloud blog! If you’re a developer looking to build powerful web applications, CakePHP is an excellent framework to consider. Known for its simplicity and flexibility, CakePHP allows for rapid development while maintaining code quality. In this post, we will guide you through the steps to install CakePHP on Ubuntu 24.04 or newer.

Prerequisites

Before we dive into the installation process, ensure that you have the following:

  1. Ubuntu 24.04 or Newer: Make sure your server is running on an updated version of Ubuntu.
  2. LAMP Stack: You need to have Apache, MySQL (or MariaDB), and PHP installed on your system. If you haven’t set this up yet, follow these instructions to install the LAMP stack:
   sudo apt update
   sudo apt install apache2 mysql-server php php-mysql libapache2-mod-php php-cli php-xml php-mbstring php-zip php-imagick
  1. Composer: CakePHP uses Composer for dependency management. If you haven’t installed Composer, you can do so by running:
   sudo apt install composer

Step 1: Install CakePHP

Now that you have your environment set up, let’s install CakePHP. You can do this either globally or within a specific project directory. Here, we will demonstrate the project directory method.

  1. Navigate to your web root directory:
   cd /var/www/html
  1. Create a new CakePHP project. Replace my_app with your desired application name:
   composer create-project --prefer-dist cakephp/app my_app

Step 2: Configure Permissions

After the installation, you need to set the correct permissions for the CakePHP directory to ensure that it can write to specific folders.

  1. Change the ownership to the web server user:
   sudo chown -R www-data:www-data /var/www/html/my_app
  1. Set the appropriate permissions:
   sudo find /var/www/html/my_app -type d -exec chmod 755 {} \;
   sudo find /var/www/html/my_app -type f -exec chmod 644 {} \;
  1. Set permissions for the tmp and logs directories:
   sudo chmod -R 775 /var/www/html/my_app/tmp
   sudo chmod -R 775 /var/www/html/my_app/logs

Step 3: Configure Apache

Next, you need to configure Apache to serve your CakePHP application.

  1. Create a new Apache configuration file:
   sudo nano /etc/apache2/sites-available/my_app.conf
  1. Add the following configuration:
   <VirtualHost *:80>
       ServerName my_app.local
       DocumentRoot /var/www/html/my_app/webroot

       <Directory /var/www/html/my_app/webroot>
           AllowOverride All
           Require all granted
       </Directory>

       ErrorLog ${APACHE_LOG_DIR}/my_app_error.log
       CustomLog ${APACHE_LOG_DIR}/my_app_access.log combined
   </VirtualHost>
  1. Enable the new site and rewrite module:
   sudo a2ensite my_app.conf
   sudo a2enmod rewrite
  1. Restart Apache:
   sudo systemctl restart apache2

Step 4: Update Your Hosts File

To access your application via a browser, you’ll need to update your hosts file.

  1. Open the hosts file:
   sudo nano /etc/hosts
  1. Add the following line (adjusting for your local IP if necessary):
   127.0.0.1 my_app.local

Step 5: Access Your Application

Now that everything is set up, you can access your CakePHP application. Open your web browser and navigate to http://my_app.local. You should see the CakePHP welcome page, confirming that your installation was successful!

Conclusion

Congratulations! You have successfully installed CakePHP on your Ubuntu 24.04 or newer system. You can now start developing your web applications using one of the most powerful PHP frameworks available.

For more tutorials and tips on web development, don’t forget to check out our other blog posts at Greenhost.cloud!