How to configure a PHP environment in 15 minutes

Gianluca Nativo
devartis
Published in
7 min readOct 21, 2019

--

Introduction

Hello there! In this opportunity, I would like to show you how to create a simple web app with PHP and Yii from scratch, set up your app on your local Apache server and configure XDebug on your local environment for code debugging purposes.

To follow this tutorial, you will need:

Yii and MySQL integration

The following libraries are likely to be required to run Composer, which is a tool for dependency management in PHP that allows you to declare the libraries your project depends on and it will manage (install/update) them for you:

sudo apt-get updatesudo apt-get install php7.2-xmlsudo apt-get install php-mbstring

After you have installed PHP and Composer, run the following command to create a Yii project in the project directory:

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

This command will install the latest stable version of a Yii application template and all the necessary components that requires to be executed. Once the installation is completed, you can run the app server with the following command on your project directory:

./yii serve

Now we can access the server address and check if our app is currently working:

Great! Now there is a Yii app running locally. The next steps are the creation and configuration of the MySQL database. I will take for granted that you already have MySQL installed in your environment, if that is not the case, please take your time to install it since it is necessary to continue with the tutorial.

You may need to install the following library along with MySQL:

sudo apt-get install php7.2-mysql

On MySQL we are going to create our movies database for this project by opening the interpreter from the console, replacing your_mysql_user with your own MySQL username:

sudo mysql -u your_mysql_user -p

This command will trigger an input prompt in which you should enter your MySQL password and the interpreter will be executed.

Then, on the interpreter we create the database with the following sentence:

create database movies_db;

Once the database is created, we write the word exit on the MySQL interpreter to leave it.

In the my_yii_project/config/db.phpfile, we set the variables needed for the connection between our MySQL instance and our recently created Yii app, replacing your_mysql_user and your_mysql_pass with your own MySQL username and password:

<?phpreturn [    'class' => 'yii\db\Connection',    'dsn' => 'mysql:host=localhost;dbname=movies_db',    'username' => 'your_mysql_user',    'password' => 'your_mysql_pass',    'charset' => 'utf8',    // Schema cache options (for production environment)    //'enableSchemaCache' => true,    //'schemaCacheDuration' => 60,    //'schemaCache' => 'cache',];

With this command we create a migration that will generate a movies table on the database, with the title, the director and the year of release as their attributes:

./yii migrate/create create_movie_table --fields="title:string(100):notNull,director:string(100):notNull,year:integer:notNull"

And we will run it with this one:

./yii migrate/up

Now the database has a movies table that will be used in our Yii app. The next step is to create our model controller and their views:

Gii features

Yii contains Gii, a module which can help us to create our models and controllers in a simple way from a web view, abstracting us from the code. By going to this URL (http://hostname/index.php?r=gii) we are able to create our Movie model and MovieController in our project.

Gii main view
Gii Model Generator view
Gii Controller Generator view

To create both the Model and Controller files, go through the Gii views and complete the fields with the names showed on the screens, and review that all the create actions are checked at the end of the view.

After following these steps our app will have an empty controller, which will contain our CRUD methods. Besides from that, a Model file (which provides the PHP representation of a MySQL model instance) is created as well. With these features included, what you should do is to create the CRUD methods with Gii.

Gii CRUD Generator view
Gii CRUD Generator file changes

After we generate our Model CRUD, we should modify our config/web.php file to access our views (As from now the Gii view will not be available anymore, but it will not be used again in this tutorial).

'urlManager' => [    'enablePrettyUrl' => true,    'showScriptName' => false,    'rules' => [        'movies' => 'movie/index',    ],],

If we go to http://hostname/movies, now we can create, update or delete our movies.

Great! Our app is already completed, but the most important step is not completed yet. To detect and analyze future bugs in our app, we should configure XDebug for PHP.

XDebug and Apache settings

Before you continue reading this section, check whether XDebug has been installed in your PHP version or not.

php -v

If XDebug is listed below the PHP version, you can continue with the settings for this feature. Otherwise, please check the installation guide for XDebug at the beginning of the article and follow the steps until you have XDebug listed.

Output of the PHP command with their version and extensions

Afterwards, install the php-xdebug library to connect PHP and XDebug to your editor and remote configurations.

sudo apt install php-xdebug

With this library installed, the next step is to edit the xdebug.ini file to enable debugging for the project.

sudo gedit /etc/php/7.2/mods-available/xdebug.ini

And add these lines on the file:

zend_extension=xdebug.soxdebug.idkey=PHPSTORMxdebug.default_enable=1xdebug.remote_enable=1xdebug.remote_host=127.0.0.1xdebug.remote_port=9000

Now we can take for granted that the XDebug configuration is ready. The final step is the Apache configuration for our app. This quick guide will lead you through the essential features without an extended development.

  1. Open a terminal tab and go to the ‘/etc/apache2/sites-available’ directory
  2. Create the my-yii-project.conf file for the local domain that your app will use and add the following configuration text:
<VirtualHost *:80>ServerAdmin webmaster@example.comDocumentRoot /path/to/my_yii_project/webServerName my-yii-project.com<Directory "/path/to/my_yii_project/web">Require all granted# use mod_rewrite for pretty URL supportRewriteEngine on# If a directory or a file exists, use the request directlyRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# Otherwise forward the request to index.phpRewriteRule . index.php</Directory></VirtualHost>

3. Go to the ‘/etc/apache2/sites-enabled’ directory and add the symbolic link:

ln -s /etc/apache2/sites-available/my-yii-project.conf my-yii-project.conf

4. Add this line ‘127.0.0.1 my-yii-project.com’ at the end of /etc/hosts file, to assign that domain to the IP address.

5. Modify index.php priority on ‘/etc/apache2/mods-enabled/dir.conf’:

<IfModule mod_dir.c>DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm</IfModule>

6. Install the libapache2-mod-php library if it is not already installed:

sudo apt-get install php libapache2-mod-php

7. Restart apache to apply changes:

sudo service apache2 restart

For local development, you may need to modify the permissions on your project files in order to be used by Apache:

On root directory of the project:

sudo chgrp -R www-data . //assigns group ownernship to apache groupsudo chmod 775 . //allows write-read-execute permission to group and owner of the filessudo chgrp -R www-data ./web/assetssudo chmod 775 ./web/assets

We are almost on the verge of greatness. The last step is to set up the Yii server on the project settings and their respective running configuration.

Once these settings are ready, run the server on debug mode and put a breakpoint on any method of the MovieController file. If the breakpoint has a small tick on the inside, it means that this whole process has been a success. Go to the URL which will trigger your breakpoint and see the magic.

Conclusion

Getting started on PHP is not as easy as one would expect, since it requires a lot of background knowledge in many different tools. However, with good research you can find all the information you will need (maybe by reading this article). What is coming next is up to you, be free to give this language a chance and experiment with its unique features.

Visit us!

--

--