How to Install Zend Framework / Laminas on Linux

Install Zend Framework Linux Ubuntu RHEL Debian Centos

Introduction

Zend Framework / Laminas is a popular open-source PHP framework designed to make building robust, high-performance web applications simpler and more efficient. With its modular architecture and use of object-oriented programming principles, Zend Framework provides developers with the tools they need to create scalable, secure, and high-quality applications. In this comprehensive guide, we will cover how to install Zend Framework on a Linux system, ensuring you have everything you need to get started with your PHP development.

Table of Contents

  1. Prerequisites
  2. Installing Required Software
    • PHP
    • Apache
    • MySQL
    • Composer
  3. Setting Up the Environment
  4. Installing Zend Framework
  5. Configuring Apache
  6. Creating a New Zend Framework Project
  7. Running Your First Zend Framework Application
  8. Common Issues and Troubleshooting
  9. Additional Resources

1. Prerequisites

Before diving into the installation process, ensure that you have a Linux system with a user account that has sudo privileges. You will also need an active internet connection to download the necessary packages and dependencies.

  • Linux distribution: Ubuntu, Debian, CentOS, Fedora, or any other major distribution.
  • PHP version 7.4 or higher.
  • Apache HTTP Server 2.4 or higher.
  • MySQL version 5.7 or higher (optional, for database-driven applications).
  • Composer (dependency manager for PHP).

2. Installing Required Software

Installing PHP

To install PHP, you need to update your package index and then install PHP along with some common modules.

On Ubuntu/Debian

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

On CentOS/Fedora/RHEL

$ sudo yum install epel-release -y
$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
$ sudo yum install yum-utils -y
$ sudo yum-config-manager --enable remi-php74
$ sudo yum install php php-cli php-fpm php-mysql php-json php-curl php-mbstring php-xml php-zip -y

Verify the installation by checking the PHP version:

$ php -v

Installing Apache

Apache is a widely used web server, and it can be easily installed using the package manager of your Linux distribution.

On Ubuntu/Debian

$ sudo apt update
$ sudo apt install apache2 -y

On CentOS/Fedora

$ sudo yum install httpd -y

Start and enable Apache to run on boot:

On Ubuntu/Debian

$ sudo systemctl start apache2
$ sudo systemctl enable apache2

On CentOS/Fedora

$ sudo systemctl start httpd
$ sudo systemctl enable httpd

Verify the installation by opening your web browser and navigating to http://localhost. You should see the Apache default welcome page.

Installing MySQL

If your application requires a database, you can install MySQL or MariaDB. Here, we will use MySQL.

On Ubuntu/Debian

$ sudo apt update
$ sudo apt install mysql-server -y

On CentOS/Fedora

$ sudo yum install mysql-server -y

Start and enable MySQL to run on boot:

On Ubuntu/Debian

$ sudo systemctl start mysql
$ sudo systemctl enable mysql

On CentOS/Fedora

$ sudo systemctl start mysqld
$ sudo systemctl enable mysqld

Secure your MySQL installation:

$ sudo mysql_secure_installation

Follow the prompts to set up a root password and secure the installation.

Installing Composer

Composer is a dependency manager for PHP that will help you manage your project’s libraries and dependencies.

On Ubuntu/Debian and CentOS/Fedora

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('https://composer.$ github.io/installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ sudo mv composer.phar /usr/local/bin/composer

Verify the installation by checking the Composer version:

$ composer --version

3. Setting Up the Environment

Before installing Zend Framework, it is crucial to ensure that your PHP environment is configured correctly. This includes setting up the appropriate PHP modules and configuring your PHP settings.

PHP Configuration

Edit the PHP configuration file (php.ini):

On Ubuntu/Debian

$ sudo nano /etc/php/7.4/apache2/php.ini

On CentOS/Fedora

$ sudo nano /etc/php.ini

Ensure the following settings are configured correctly:

memory_limit = 256M
post_max_size = 50M
upload_max_filesize = 50M
max_execution_time = 300

Save and exit the editor, then restart Apache to apply the changes:

On Ubuntu/Debian

$ sudo systemctl restart apache2

On CentOS/Fedora

$ sudo systemctl restart httpd

4. Installing Zend Framework

With Composer installed, you can now install Zend Framework. Zend Framework is now part of the Laminas Project, so you’ll install Laminas instead.

Installing Laminas via Composer

First, navigate to the directory where you want to create your Zend Framework (Laminas) project:

$ cd /var/www/html

Use Composer to create a new Laminas project:

$ composer create-project laminas/laminas-mvc-skeleton my-zend-app

This command will download the Laminas MVC Skeleton application and install all the necessary dependencies. Once the installation is complete, you will have a basic Laminas application setup in the my-zend-app directory.

5. Configuring Apache

To serve your Laminas application using Apache, you need to configure a virtual host.

Creating a Virtual Host

Create a new Apache configuration file for your application:

On Ubuntu/Debian

$ sudo nano /etc/apache2/sites-available/my-zend-app.conf

On CentOS/Fedora

$ sudo nano /etc/httpd/conf.d/my-zend-app.conf

Add the following configuration to the file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/my-zend-app/public
    ServerName my-zend-app.local
    <Directory /var/www/html/my-zend-app/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/my-zend-app_error.log
    CustomLog ${APACHE_LOG_DIR}/my-zend-app_access.log combined
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [QSA,L]
    </IfModule>
</VirtualHost>

Save and close the file.

Enabling the Virtual Host

On Ubuntu/Debian

$ sudo a2ensite my-zend-app
$ sudo systemctl reload apache2

On CentOS/Fedora

$ sudo systemctl restart httpd

Modifying the Hosts File

To access your application via my-zend-app.local, you need to add an entry to your /etc/hosts file:

$ sudo nano /etc/hosts

Add the following line:

127.0.0.1   my-zend-app.local

Save and close the file.

6. Creating a New Zend Framework Project

Now that your environment is set up and Apache is configured, you can start creating your Zend Framework project. The Laminas MVC Skeleton application comes with a basic setup that you can extend and customize.

Creating Controllers

Controllers are responsible for handling user input and returning responses. To create a new controller, use the Laminas CLI tool:

$ cd /var/www/html/my-zend-app
$ composer require laminas/laminas-cli
$ ./vendor/bin/laminas mvc:controller MyController

This command creates a new controller named MyController. You can find it in the module/Application/src/Controller directory.

Creating Views

Views are used to generate the output sent to the user’s browser. Create a new view script for your controller action by creating a new file:

$ nano module/Application/view/application/my/index.phtml

Add the following content to this file:

<h1>Hello, Zend Framework!</h1>
<p>Welcome to your first Laminas application.</p>

Configuring Routes

Routes define the URLs that your application can respond to. Open the module/Application/config/module.config.php file and add a new route for your controller:

'router' => [
    'routes' => [
        'my' => [
            'type'    => Segment::class,
            'options' => [
                'route'    => '/my[/:action]',
                'defaults' => [
                    'controller' => Controller\MyController::class,
                    'action'     => 'index',
                ],
            ],
        ],
    ],
],

Save and close the file.

7. Running Your First Zend Framework Application

Now that you have created a controller, a view, and configured a route, you can run your application and verify that everything is working correctly.

Starting Apache

Ensure that Apache is running:

On Ubuntu/Debian

$ sudo systemctl start apache2

On CentOS/Fedora

$ sudo systemctl start httpd

Accessing Your Application

Open your web browser and navigate to http://my-zend-app.local/my/index. You should see the welcome message you added in your view.

8. Common Issues and Troubleshooting

Database Connection Issues

If your application requires a database connection and you are experiencing issues, verify your database connection settings in the configuration file. Ensure that the username, password, and database name are correct.

Controller or View Errors

If you are encountering errors with your controller or view, check the Apache error log for more details. You can find the error logs at:

  • Ubuntu/Debian: /var/log/apache2/error.log
  • CentOS/Fedora: /var/log/httpd/error_log

Dependency Loading Issues

If you face issues with loading dependencies using Composer, ensure that you have an active internet connection and that Composer is correctly set up. You can try updating your dependencies with the following command:

$ composer update

Permission Issues

If you encounter permission issues when accessing your application files or directories, ensure that the Apache user has the necessary permissions. For most distributions, the Apache user is www-data on Ubuntu/Debian and apache on CentOS/Fedora. You can adjust the permissions with the following commands:

On Ubuntu/Debian

$ sudo chown -R www-data:www-data /var/www/html/my-zend-app
$ sudo chmod -R 755 /var/www/html/my-zend-app

On CentOS/Fedora

$ sudo chown -R apache:apache /var/www/html/my-zend-app
$ sudo chmod -R 755 /var/www/html/my-zend-app

9. Additional Resources

If you need more information or assistance with using Zend Framework (Laminas), you can refer to the following resources:

Conclusion

This comprehensive guide should provide you with a solid foundation for installing and configuring Zend Framework (Laminas) on a Linux system. By following these steps, you should be able to set up a working environment, create your first Laminas application, and address common issues that may arise.

Remember, developing with a robust framework like Zend Framework requires practice and continuous learning. Explore the extensive documentation and community resources available to deepen your understanding and enhance your skills. Happy coding!

LEAVE A COMMENT