Running Yii2 and WordPress on the Same Server

A lot of people around the web seem rightfully confused about running both WordPress and Yii2 together. In other words, you’re running a Yii server with your application on yourdomain.com but wish to run WordPress at yourdomain.com/blog! How do you do it?

After I launched Meeting Planner, I decided to add a WordPress blog. At first, I ran it in a subdomain. Recently, I realized that Google treated these as two different sites and that this was limiting my traffic.

As I browsed around the web, I found a lot of people trying to run Yii or another application framework such as Laravel or Symfony alongside WordPress. There are a lot of partial solutions out there. I took the concepts from a number of posts and explored form there.

An Example Apache Configuration File

Here’s what I figured out that integrated my WordPress blog at https://meetingplanner.io/blog alongside my meeting scheduler Yii app.

You need to have enabled mod_alias on Apache2:

$sudo a2enmod alias

Check it with:
$sudo cat /etc/apache2/mods-enabled/alias.load

This is my new Apache configuration file for both Yii2 and WordPress sites:
<VirtualHost *:443>
ServerName meetingplanner.io
ServerAlias www.meetingplanner.io
DocumentRoot "/var/www/yii2/frontend/web"
Alias /blog /var/www/wordpress
AliasMatch /blog/(.*)$ /var/www/wordpress
<Directory "/var/www/wordpress">
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
<Directory "/var/www/yii2/frontend/web/">
Options -Indexes
AllowOverride All
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</Directory>
...
RewriteEngine On
RewriteCond %{SERVER_NAME} =www.meetingplanner.io
RewriteRule ^ https://meetingplanner.io%{REQUEST_URI} [END,QSA,R=permanent]
...
</VirtualHost>

I ran into the same problem most people did … at first everything seems to work, e.g. WordPress home page and WordPress dashboard but then you realize that blog posts fail. Most people don’t have this line:

AliasMatch /blog/(.*)$ /var/www/wordpress

Configuring .htaccess

You may notice that I integrated the .htaccess functionality for Yii2 into my Apache2 configuration file, which is meant to improve performance.

Managing WordPress works better to maintain the .htaccess file, here are the changes I used to integrate with the above Apache configuration. These were required to make everything work together:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

WordPress Settings

You also have to configure your site url to the path you are using e.g. https://meetingplanner.io/blog:

WordPress running with Yii2

WordPress General Settings Running on Same Server

Explore the Sites

You can check out each site here:

Integrating the Sitemaps

The Yii2 Meeting Planner site has an extensive sitemap with 21 languages. I extended the code to create my own layout for a sitemap index and then wrote code to generate a sitemap index that includes each WordPress sitemap index file.

I didn’t submit the WordPress sitemap index to Google, instead I submitted my Yii-generated sitemap index.

Synchronizing the timestamps from the WordPress sitemaps isn’t done yet but can probably be done by pulling the file modified times. The Yii app should be able to read the blog directory as they are in the same Apache configuration.

By doing this, Google sees both sites as one website and expands the SEO footprint of Meeting Planner.

Additional Challenges and Opportunities

Running WordPress on my Yii server opens up performance and security challenges that need to be managed. As traffic grows, it may make sense to set of a reverse proxy and direct blog traffic to a different server.

With the WordPress blog running within the same domain, design issues become more obvious. Unifying the toolbars makes sense and ideally, you could also need to deal with session status.

In Conclusion

Theoretically, you can run WordPress with any application framework e.g. Rails, Symfony, Laravel, CraftCMS and of course Yii2. As a consultant, I’ve always advised clients to run blogs in different domains, but for a startup trying to grow their SEO footprint, I’m trying this new approach.

Please try scheduling your next meeting with Meeting Planner.

 

Support this site, share this page:Share on twitter

Twitter

Share on reddit

Reddit

Share on linkedin

Linkedin

Share on facebook

Facebook

Share on email

Email