Sunday, July 30, 2017

Bake and migration

Contents

CakePHP
1. Setting up CakePHP3 with CentOS

2. Bake and Migration


4. Add a calendar page

5. Use "contain"

6. Use find('list')

What is migration?


Simply speaking, migration is used to create new tables or to change tables from php files with simple operations. Or to share such changes with other members through migration files. For example, run these commands:
$ cd /vagrant/cake
$ bin/cake bake migration CreateProducts name:string description:text created modified
(Related cakephp 3 cookbook page.)

You will see a new migration file is added in cake/config/Migrations.

From this migration file, new table will be created. Now run the following command:
(If you can't run this command, maybe your permission in the shared folder is not properly configured. See this post to fix the issue.)
$ bin/cake migrations migrate

Then new table is created automatically.

You can check if the new table is really created:
$ mysql -uroot -proot
mysql> show databases;
mysql> use test;
mysql> show tables;


You can see the new table "products" is added. As you see, if you can use migration command, you can create a new table easily from the migration command and migration file. Migration files can be shared in a team, so you can share how tables should be created and changed (or deleted) through migration files in a team.

Bake


Now from this table, we will create a website template. Use the following bake command.
$ bin/cake bake all products

This means "Create all of the three: controller, model, template (view) files." If you want to create each of them separately, run these:
$ bin/cake bake controller products
$ bin/cake bake model products
$ bin/cake bake template products

Then template files are automatically created.

Now access your cakephp 3 website from this URL:
http://192.168.33.10/cake/products/

You will see the basic files & functions were automatically generated:


From the "New Products" page, you can add new product to your database. If you change the design a little, it could be used as a website to store & maintain product information. If there is a customer who wants a simple website that stores & maintains products' information, you can create such website very quickly with cakephp 3 (without bugs as long as you don't touch it...Although I'm sure we must touch it to customize something).

Now you can see, from a single and simple migration file, you can create basic website with simple commands. You can add other pages to your cakephp 3 by using other migration files.

Conclusion


What is migration used for? --- To create or change or delete tables from migration files and to share the changes with other members though the migrations files.
What is bake used for? --- To create basic features (Controller, Model, Template) from a table.

Download and configure for PHPCrawl

Contents:

Preparation for coding:
1. Virtual box and Vagrant

2. Install Apache

3. Install MySQL

4. Install PHP

Edit crawler
1. Download and configure for PHPcrawl

2. Editing code of the crawler

3. Save fetched information in a database.

4. Sharing a local website inside a local network (optional)

5. User interface

PHPcrawl


Download PHPcrawl from here: https://sourceforge.net/projects/phpcrawl/
Then unzip it inside the share folder of virtual machine:


...so that we can access to it inside the virtual machine.

Netbeans


If you don't have netbeans yet, download and install Netbeans from here:
https://netbeans.org/features/php/

I downloaded Netbeans version 8.2.

Run the Netbeans as administrator then choose File --> New Project.


PHP --> PHP Application with existing sources
Then Next.



 Choose your source folder (the folder which contains source code of PHPCrawl_083) from "Browse" and choose PHP 7.0 from "PHP version". Then click Next.


Add your vagrant's URL for PHPCrawl to "Project URL". Then choose "example.php" for index file.
Then finish.


You have your open source project now.... :)


Now we will enable xdebug in Netbeans.
Right click on the project name and choose "Properties":


Choose "Run Configuration" and "Advanced".


Write the path for the project that is in the virtual machine as "Server Path". Write path for the project that is in the local PC as "Project Path" like below:


From the menu bar, choose "Tools" then "Options":



From "PHP", choose "Debugging", then check "Stop at First Line" then click "OK":


Add this to the code:
// Set delay time
$crawler->setRequestDelay(5.0);
This is because accessing a web server too many times for a too short time can be regarded as a DDOS attack. For example, google crawler is said to send access every 15 seconds.


Then save.


Add some breakpoints and click the debug button from the menu bar. Then your PHPCrawl project is supposed to start.
If the lower right keeps saying "waiting for connection (netbeans-xdebug)" and never be changed, maybe some of your setting is wrong. Check if your php.ini is written correctly (especially your IP address and if the firewall is blocking the connection.)

Then your program is executed step by step (press F7 to step forward). In this way, you can see how this program is executed and how code is used in the execution. We will check how PHPCrawl is executed and where we can modify.

Supplemental advice


By the way, if you want to use only the breakpoints, un-check the "Stop at First line":



Then the execution will not stop every time you change the page in the browser.

If your xdebug is not working correctly, check your server log:
# less /var/log/httpd/error_log

To check connections, use this command on Teraterm:
# netstat -an

After starting debug on netbeans, if you use this netstat command on Teraterm, you can see the port of xdebug is 80:
tcp6       0      0 192.168.33.10:80       192.168.33.2:49901      ESTABLISHED

The port of netbeans is 9000:
tcp        0      0 192.168.33.10:52024    192.168.33.2:9000       ESTABLISHED

The debugging port of netbeans can be changed here:

Diagram from stackoverflow:
Citation from stackoverflow (an answer from Linus Kleen) https://stackoverflow.com/questions/8049776/xdebug-for-remote-server-not-connecting

If the connection is not established, it is highly possible that your connection is blocked by your anti-virus program or firewall. You can check it in your anti-virus program or firewall and remove the block as it is a safe connection.

When virtual machine on virtualbox can't connect to internet

I am using CentOS 6.9 and Oracle Virtualbox in this article. I will write what you can do when virtual machine on virtualbox can't connect to internet.

Point to be checked 1
"Whether you selected Connect automatically during the set up of the virtual machine"


When you configured your virtual machine at the beginning, you should have seen "Configure Network".


Make sure "Connect automatically" is checked and click "Apply".


Point to be checked 2
"Whether the cable is connected to the virtual machine"


Open the VM Virtualbox manager and right-click on your virtual machine, and click "Setting".


Make sure your cable is connected. And also make sure your physical cable is connected to the host machine.


Point to be checked 3
"Whether your adapter is attached to Bridged adapter or NAT"


Open the VM Virtualbox manager and right-click on your virtual machine, and click "Setting".


Make sure your adapter is attached to "Bridged adapter" or "NAT".


If 1, 2 and 3 are checked to be ok, now check the following.


Point to be checked 4


Start your virtual machine and try this command:
$ sudo service network restart

If your configuration is properly set up, the following should appear:
Shutting down interface eth0:                            [OK]
Shutting down loopback interface:                    [OK]
Bringing up loopback interface:                        [OK]
Bringing up interface eth0:                                [OK]
Determining IP information for eth0... done.    [OK]

Also try this command:
$ sudo ifconfig -a

If this is displayed, you should be able to connect to internet.

If it fails determining IP and "eth0" is not displayed by ifconfig command, maybe wrong MAC address is registered in your virtual machine.

This is the MAC address used for eth0.

Correct MAC address can be checked from the following place. You can see the same MAC address is shown on this setting and the result of ifconfig command.

If MAC address is wrong (or probably wrong), change it from here:
$ sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
and
$ sudo vi  /etc/udev/rules.d/70-persistent-net.rules

For 70-persistent-net.rules, ATTR{address} is the MAC address. Change it to correct value.
If you are using eth0 and never used eth1 but mysteriously eth1 is added in this file for some reason, change the name of eth1 to eth0 (eth1 is the new information and we want to use this as eth0) and delete the old eth0 one. (This often occurs after cloning your virtual machine. When new MAC address is detected to be being used, udev automatically change the information as eth1, which is actually unnecessary.)

By the way, 70-persistent-net.rules is automatically created even if you delete the file. If you mess up the file and have no idea how to fix the file, you can delete it and restart the virtual machine to re-create the file. Then change the setting written in the file again (if wrong information is written there).

(To delete the file and restart the virtual machine, run the following commands. These are unnecessary if you don't want to re-create 70-persistent-net.rules file.)
$ sudo rm  /etc/udev/rules.d/70-persistent-net.rules
$ sudo shutdown -r now



Firewall 


Check your firewall if it is unnecessarily blocking your virtual machine's network connection.
(Maybe disable your firewall for a while and try to connect to the internet again on the virtual machine. If you can connect, then it is because of the firewall. Turn on the firewall again and properly configure the firewall to allow your virtual machine's network connection.)





Sunday, July 23, 2017

VMbox and CentOS6

We will use:
Windows 8.1 for the host machine.
CentOS 6 for the guest machine.

At first, you need to install oracle virtualbox. Install it from here:
https://www.virtualbox.org/

And you need .iso file to make a virtual machine. We will use CentOS 6.x this time, so install CentOS 6 iso file from here:
http://isoredirect.centos.org/centos/6/isos/x86_64/

Tips: Or, if you want older version of CentOS, install the .iso file from here:
http://vault.centos.org/ 


CentOS 6.4 iso file can be installed from here for example:
(64 bit) http://vault.centos.org/6.4/isos/x86_64/
(32 bit) http://vault.centos.org/6.4/isos/i386/


Also, you would need to check this post to use yum:  Use "yum update" for old CentOS5 that is already EOL

Choose CentOS 6.9 (x86_64) 64bit minimal iso and download it.


After installing Virtualbox, you should have a shortcut for Virtualbox manager on desktop. Click the shortcut on desktop and start the Virtualbox manager.


And you will see the manager will be opened. Click "New" at the upper left:



You should now see the following. Input "CentOS69" for the name,  "Linux" for the type, and "RedHat (64bit)" for the version. Then click next:

You will see the following. Click "Expert Mode":

Check if each item is ok and if you confirmed all of them are ok, click "Create".

You will see your CentOS69 virtual machine is added. Right-click on it and click "Setting".


You will see the following. Choose "Storage" and click the DVD icon that is next to "Controller: IDE".


Click "Choose disk".

Choose the .iso file which you downloaded at the beginning. Then click "Open".


Confirm that the .iso file was added under "Controller: IDE". Then click "OK".

Now change the network setting also. If you choose "Bridged Adapter", you can share the virtual machine within a team. If you don't want to share the virtual machine with anyone, "Host-only Adapter" is enough. We will choose "Bridged Adapter" for this time.


Choose the virtual machine we have made and click "Start" at the upper left.


You will see the CentOS 6 will be started. Choose "Install or upgrade an existing system" and hit the Enter key on your keyboard.
(You would realize, once you click on the virtualbox's window, your mouse cursor is trapped inside the window. To exit from the window, press right ctrl key on your keyboard. If your keyboard doesn't have right ctrl key, press ctrl + alt + del)

Choose "Skip" and hit the Enter key.

Choose "English" and click "Next". Then almost same window will appear, so choose "U.S. English" and click "Next".


Choose "Basic Storage Devices" and click "Next".


Choose "Yes, discard any data" and click "Next".


You will see the following at the lower left after clicking the button. Click "Configure Network".


Make sure to check "Connect automatically" and click "Apply". Then click "Next".


You will see the following. You can choose which time zone you will use for the system. If you want to use the local time for the system clock, un-check "System clock uses UTC". Or else, leave it and then click "Next".


You can decide what password will be used for the root account. This is a virtual machine that exists and works inside your PC, so if you don't allow it connect to external network, I think the password doesn't need to be very complicated. Write whatever you want for the password. (It needs to be at least 6 letters). Then click "Next".


Choose "Use All Space" and click "Next".

Click "Write changes to disk".


Your system will start being configured.


When the configuration is finished, click "Reboot".


The virtual machine will be rebooted like this:


The login user is "root". The password is the password that you made previously.

You made the password in this step


Once you could login to the virtual machine, run this command:
# yum -y update
"#" is not needed to input. It just indicates you are a root user.



If you have everything done successfully, the yum update should get started like the following image.
If you could update it successfully, hooray! :)

If not get started, it is likely that you are using older version of CentOS.
Or your network setting is wrong (did you choose "connect automatically"?)
For other tips, check here: When virtual machine on virtualbox can't connect to internet.
You should have seen this while configuring the virtual CentOS.
You need to check "Connect automatically".


Shared folder setting


Now we will configure for a shared folder between the host machine and guest machine.
At first, install necessary packages for sharing folders.

# yum -y install bzip2 dkms gcc gcc-c++ kernel-devel kernel-headers make

Then reboot the machine.

# shutdown -r now

Set path:
# export KERN_DIR=/usr/src/kernels/$(uname -r)
# export MAKE='/usr/bin/gmake -i'

Now insert GuestAddition CD image:
Maybe you need to insert it twice.

Then run the following commands:
# mount /dev/cdrom /mnt
# ls /mnt

If the following is displayed, you have done it successfully.

Now run the following commands:
# cd /mnt
# sh VBoxLinuxAdditions.run

If the following is shown, you have done it successfully.

Shutdown the virtual machine now.
# shutdown -h now

Create a folder to share in host machine.


Then, on the VM virtual box manager, open the setting.


Open the "Shared folders".

Select the shared folder that we made in the host machine. Then click OK.

Maybe there should be an error. To remove the error, open "Storage" and if you see two "VBoxGuestAdditions.iso" files under "Controller:IDE", remove one of them. Then the error should be gone. Now click "OK".

Start the virtual machine now.


Run this command:
# mount -t vboxsf -o dmode=777,fmode=777 CentOS64 /mnt
(tips: CentOS64 is a name of the shared folder in the host machine. If you gave it an other name, change the command to reflect the shared folder's name.)

Check if the folder is successfully shared. Put some random file inside the shared folder in the host machine.

Then check if this file can be accessible from the guest machine.
Run this command on the virtual machine.
# ls /mnt

If you can see the file from the guest machine like the following, you have done everything successfully. Now you can use the shared folder. :)



Disable SELinux


Security-Enhanced Linux (SELinux) is a Linux kernel security module. But it causes a lot of troubles and is not necessary most of time during development. So we will disable SELinux.

To disable SELinux, open the following file.
# vi /etc/selinux/config

Then change Selinux to be disabled.
SELINUX=disabled

Then restart the virtual machine.
# shutdown -r now