Yii PHP Framework

How To Install and Setup Yii PHP Framework on Ubuntu 24.04

Welcome to the Greenhost.cloud blog! Today, we will guide you through the process of installing and setting up the Yii PHP framework on Ubuntu 24.04. Yii is a high-performance PHP framework that is perfect for developing modern web applications quickly and efficiently. Whether you’re building a small website or a large enterprise application, Yii’s powerful features and flexibility will serve you well.

Prerequisites

Before we begin, make sure you have the following:

  1. Ubuntu 24.04 installed on your server or local machine.
  2. Root or sudo access to your terminal.
  3. PHP (version 7.4 or higher) installed on your system.
  4. A web server like Apache or Nginx.
  5. Composer installed for managing dependencies.

Step 1: Update Your System

First, open your terminal and ensure your system is up-to-date. Run the following commands:

sudo apt update
sudo apt upgrade -y

Step 2: Install PHP and Required Extensions

If you haven’t installed PHP yet, you can do so using the following command:

sudo apt install php php-cli php-fpm php-mbstring php-xml php-curl php-zip php-mysql -y

You can verify the installation of PHP by checking the version:

php -v

Step 3: Install Composer

Composer is a dependency manager for PHP, and Yii uses it for installation. To install Composer, run the following commands:

cd ~
curl -sS https://getcomposer.org/installer -o composer-installer.php
php composer-installer.php --install-dir=/usr/local/bin --filename=composer

Verify Composer installation:

composer --version

Step 4: Install Yii Framework

Now, let’s install the Yii framework using Composer. You can create a new Yii application by running:

composer create-project --prefer-dist yiisoft/yii2-app-basic myYiiApp

This command creates a new Yii application in a directory called myYiiApp. You can choose any name you prefer.

Step 5: Configure Apache (or Nginx)

If you’re using Apache, you need to enable the rewrite module and configure the virtual host. Run the following commands:

sudo a2enmod rewrite

Next, create a new configuration file for your Yii application:

sudo nano /etc/apache2/sites-available/myYiiApp.conf

Add the following configuration:

<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /path/to/myYiiApp/web

    <Directory /path/to/myYiiApp/web>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/myYiiApp_error.log
    CustomLog ${APACHE_LOG_DIR}/myYiiApp_access.log combined
</VirtualHost>

Replace /path/to/myYiiApp with the actual path to your Yii application and yourdomain.com with your domain name.

Enable the site and restart Apache:

sudo a2ensite myYiiApp.conf
sudo systemctl restart apache2

For Nginx Users

If you’re using Nginx, create a new configuration file:

sudo nano /etc/nginx/sites-available/myYiiApp

Add the following configuration:

server {
    listen 80;
    server_name yourdomain.com;
    root /path/to/myYiiApp/web;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Enable the configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/myYiiApp /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Step 6: Test Your Yii Application

Open your web browser and navigate to http://yourdomain.com. You should see the Yii welcome page, indicating that the framework has been successfully installed and configured.

Conclusion

Congratulations! You have successfully installed and set up the Yii PHP framework on Ubuntu 24.04. With its powerful features and flexibility, you’re now ready to start developing your web applications. If you have any questions or run into issues, feel free to leave a comment below, and we’ll be happy to help!

Stay tuned for more tutorials on web development and server management right here at Greenhost.cloud!