Configure CakePHP 3.6 in a Subfolder of WordPress
What do I actually want to do?
I want to access my WordPress site at example.com, while installing a CakePHP app on example.com/cakephp.
To achieve this, we need to install wordpress along with cakephp and manipulate the .htaccess files with effective rules.
First of all we’ll complete the wordpress and cakephp installations. I’m assuming that you are connected to your EC2 instance. I divided this whole scenario into three sections, where in the first part we’ll configure wordpress and in the second part we’ll do the installation of cakePHP3.6 and in the third part we’ll modify the .htaccess file under wordpress folder and cakePHP app subfolder.
Note: If you have already installed the wordpress and cakephp then you can directly jump to the third section where I mentioned the .htaccess manipulation.
Section 1: WordPress Installation
In the first part we’ll download the latest wordpress and unzip the tar file. Check the following commands for it.
[ec2-user@webnersolutions html]$ cd /var/www/html/ [ec2-user@webnersolutions html]$ wget https://wordpress.org/latest.tar.gz [ec2-user@webnersolutions html]$ tar -xzf latest.tar.gz
After unzip the folder we’ll get the folder with name wordpress, Now we’ll login to mysql server with root user and database root password.
Create the user and password for WordPress, check following commands.
mysql> CREATE USER 'wp-user'@'localhost' IDENTIFIED BY 'any-strong-password'; mysql> CREATE DATABASE `wordpress-db`; mysql> GRANT ALL PRIVILEGES ON `wordpress-db`.* TO "wp-user"@"localhost"; mysql> FLUSH PRIVILEGES; mysql> exit
Now, Create and edit the wp-config.php file, The path is shown below
[ec2-user@webnersolutions ~]$ cd /var/www/html/wordpress/ [ec2-user@webnersolutions wordpress]$ cp wp-config-sample.php wp-config.php [ec2-user@webnersolutions wordpress]$ sudo nano wp-config.php
Now modify the following directives with your information, here I’m using my wordpress information, it helps the wordpress to communicate with mysql databases.
define('DB_NAME', wordpress-db); define('DB_USER', 'wp-user'); define('DB_PASSWORD', 'your_password');
Save the wp-config.php file.
Move all files and directories to root folder, the path is /var/www/html, and remove the wordpress folder.
[ec2-user@webnersolutions wordpress]$ mv * ../
Now modify the “httpd.conf” file to allow all overrides in the Apache document root, by changing the directives from
“AllowOverride None”
to
“ AllowOverride All ”
Under section
<Directory “/var/www/html”>
[ec2-user@webnersolutions wordpress]$ sudo nano /etc/httpd/conf/httpd.conf
Set the permissions:
Follow the following commands in order to set the files and directories permissions.
[ec2-user@webnersolutions ~]$ sudo chown ec2-user:apache -R /var/www/ [ec2-user@webnersolutions ~]$ sudo usermod -G apache -a ec2-user
In above commands, I have given the user ownership to ec2-user on /var/www directory because I need ec2-user for all manipulation in document root. On second command I added the ec2-user in apache group.
Now, I need to add group write permissions and to set the group ID on future subdirectories, change the directory and files permissions of /var/www and its subdirectories.
[ec2-user@webnersolutions ~]$ sudo chmod 2775 /var/www [ec2-user@webnersolutions ~]$ find /var/www -type d -exec sudo chmod 2775 {} \; [ec2-user@webnersolutions ~]$ find /var/www -type f -exec sudo chmod 0664 {} \;
Now, open a web browser, and enter the URL of your WordPress. You should see the WordPress installation screen.
http://my.public.dns.amazonaws.com or http://yourPublicIpAddress.
CakePHP 3.6 in a Subfolder of WordPressSection 2: CakePHP Installation
We’ll follow some simple steps in order to install cakePHP in EC2 Instance
Download the composer binary file and move this file to /usr/local/composer.
[ec2-user@webnersolutions ]$ curl -sS https://getcomposer.org/installer | php [ec2-user@webnersolutions ]$ sudo mv composer.phar /usr/local/bin/composer [ec2-user@webnersolution ]$ sudo chmod +x /usr/local/bin/composer
Now change the directory and create the project with composer command. I have mentioned the commands below.
[ec2-user@webnersolution ]$ cd /var/www/html [ec2-user@webnersolutions html]$composer create-project --prefer-dist cakephp/app cake
Set the permission
[ec2-user@webnersolutions html]$ chown -R ec2-user:apache cake [ec2-user@webnersolutions html]$ chmod -R 2755 cake
For the database connectivity, goto config/app.php file and add the database details in this file under “’Datasources’ => ” section.
Now open the CakePHP home page with your public IP address followed by “cake”(As I created the cakePHP app with the name “cake”).
http://my.public.dns.amazonaws.com/cake OR http://yourPublicIpAddress/cake
Section 3: Modify the .htaccess rules
We have completed the installation for wordpress and cakephp3.6, Now we need to modify the .htaccess rules under three different locations.
Location 1: .htaccess file under WordPress
Change the directory to the location where WordPress is installed, in the same folder where the wp-config.php file is placed. Now modify the .htaccess file with the following line, assuming that your CakePHP subdirectory will be called ‘cake’
RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA]
Your .htaccess file should look like this:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA] RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
Location 2:. .htaccess file under CakePHP project.
Now we’ll add the following RewriteBase statement in the .htaccess file under cakePHP project. This statement will help the cakePHP app to load the CSS and JS in /app/webroot.
RewriteBase /cake
Your .htaccess file should look like this:
<IfModule mod_rewrite.c> RewriteEngine on RewriteBase /cake RewriteRule ^(.well-known/.*)$ $1 [L] RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] </IfModule>
Location 3:. .htaccess file under webroot.
Edit the .htaccess file under /cake/webroot folder with following rule.
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /cake RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
Now you would be able to load the example.com as a wordpress site and exmaple.com/cake as as cakePHP app.