Sunday, July 9, 2017

Setting up CakePHP3 with CentOS

Contents

CakePHP
1. Setting up CakePHP3 with CentOS

2. Bake and Migration


4. Add a calendar page

5. Use "contain"

6. Use find('list')

Introduction


I am using CentOS 7.1. If you want to know how to install CentOS 7.1 with vagrant, please refer to this post: Virtual box and Vagrant.
It isn't so difficult. Just run "vagrant init bento/centos-7.1", fix Vagrantfile and "vagrant up".
If you want to use the public network, please see this: Sharing a local website inside a local network

I assume you already have installed them and finished settings for them: apache, mysql, php. (1 to 4 of the contents)

Install packages for PHP


At first, install what you need:
$ sudo yum -y install php-intl php-mbstring php-xml

If you are using php7.1 and webtatic repository:
$ sudo yum -y install php71w-intl php71w-mbstring php71w-xml
If you are using IUS repository, if you change "w" with "u", it may work.

Then restart Apache:
For CentOS 7:
$ sudo systemctl restart httpd.service
For CentOS 6:
$ sudo service httpd restart 

Then move to the vagrant directory:
$ cd /vagrant

Install composer


Install the composer:
$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

Check if you could install it successfully:
$ composer

If this is displayed after running "composer" command, you have installed it successfully.

Then run the following:
$ composer self-update

Install CakePHP3


Install cakephp3:
$ composer create-project --prefer-dist cakephp/app cake
"cake" at the end is the app name. If you want other name for the cakephp application, you can change there.

If you had any problem during the composer's project creation, run it as a super user. (Actually not recommended though. But it should be ok for development environment.) Password should be "vagrant" if you didn't change it:
$ su
# composer create-project --prefer-dist cakephp/app cake

At the end of the install, you will be asked this:
Created `config/app.php` file
Set Folder Permissions ? (Default to Y) [Y,n]?
It is asking if it is ok to give permission to php's folders. We will answer yes, so input y and press enter.

We will start the builtin server now. Move to the cakephp's direcotory:
$ cd /vagrant/cake

Start the server now. When starting the server, you need to designate the host with -H option.
$ bin/cake server -H 0.0.0.0

Then the server starts:
Welcome to CakePHP v3.4.9 Console
---------------------------------------------------------------
App : src
Path: /vagrant/cake/src/
DocumentRoot: /vagrant/cake/webroot
---------------------------------------------------------------
built-in server is running in http://0.0.0.0:8765/
You can exit with `CTRL-C`

If you are using the private network http://192.168.33.10, access to the cakephp's top page through this URL: http://192.168.33.10:8765/

I am using public network, so my URL is: http://192.168.11.100:8765/
(tips: If you are not using vagrant nor virtual machine, simply use http://localhost:8765/.)

Then you can see if the page is working.



If you are using Apache


Now we need to modify setting of Apache. Run this command to open the setting file:
$ sudo vi /etc/httpd/conf/httpd.conf

If you press "a", you can be in insert mode. If you press escape key, you can exit from the insert mode. If you input "?aaa", you can search the word "aaa" on this file. Press "n" to go to the next match.

If you input ":q!" and press enter key, you will close the file without saving it.
If you input ":wq" and press enter key, you will save and close the file.

Make sure that AllowOverride is set to All for the correct directory. "Require" was set to "all granted" by default, so I just let it be.

<Directory "/var/www/html" />
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>


Then save and close the file.

Restart the apache.

For CentOS7:
$ sudo systemctl restart httpd.service
For CentOS6:
$ sudo service httpd restart 

If you are using Nginx


Open your default.conf of Nginx.
$ sudo vi /etc/nginx/conf.d/default.conf

Then change your root:
server {
    listen       80;
    server_name  localhost;
    root   /usr/share/nginx/html/cake/public;

    ...(abbreviated)...

Restart Nginx.
$ sudo systemctl restart nginx

If you want to use subdirectory instead, check this stackoverflow.

Check if CakePHP is working


Access to the top page again from this URL:
Private network: http://192.168.33.10/cake/
Public network: http://(your URL)/cake/
or, if you changed the document root, see: http://192.168.33.10

It works.

--- optional below ---
If you see errors because it can't write in the log file, run the following command to change the folder's owner.
For Apache users:
$ sudo chown -R apache:apache /vagrant/{path to the log folder}
For Nginx (with php-fpm) users:
$ sudo chown -R php-fpm:php-fpm /vagrant/{path to the log folder}

If this doesn't work, change/add the following line in Vagrantfile.
For Apache users:
config.vm.synced_folder "./", "/vagrant", type: "virtualbox", owner: 'vagrant', group: 'apache', mount_options: ['dmode=777', 'fmode=777']
For Nginx (with php-fpm) users:
config.vm.synced_folder ".", "/vagrant", type: "virtualbox", owner: 'vagrant', group: 'php-fpm', mount_options: ['dmode=777', 'fmode=777']
And "vagrant reload".

If even that doesn't work, it might be because of SE linux. Turn off SE linux this way:
$ sudo getenforce
Enforcing
$ sudo setenforce 0
$ sudo getenforce
Permissive
--- optional above ---

Database


Now we make new database for cakephp project. We suppose both of the username and the password for mysql is "root".
$ mysql -u root -proot
mysql> CREATE DATABASE test;
mysql> exit

Now change the database setting file in cakephp:
$ sudo vi /vagrant/cake/config/app.php

Change the username, password, database:
'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'nonstandard_port_number',
        'username' => 'root',
        'password' => 'root',
        'database' => 'test',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,

Now check if it works from the top page.
There was an error regarding database access:

Error is gone now:

Installation of cakephp3 is finished!