If you have a custom built Laravel Application and would like to add a WordPress Blog on the same domain, you’re in luck because this is pretty easy to do if you follow a few simple guidelines. Of course you have the option of building an entire blog system yourself right in Laravel, but it would be a lot of work to do it right, and why reinvent the wheel? WordPress is trusted and rock solid as a blogging platform, so let’s just install it alongside our Laravel Application. We will assume use of a LEMP stack for this example of laravel and wordpress together.
1. Download A Copy of WordPress
Head on over to https://wordpress.org/download/ and download the latest copy of the software available. When you download the file, it will be in a zipped package. Place it anywhere you like, we will unzip the files and install them in the correct location in the next step.
2. Create a Blog Directory For The WordPress Files
For this Laravel and WordPress integration, we will configure things so that when you visit the Laravel application that currently lives on the root domain, you will be able to add a /blog
to the domain and reach your blog. For example, if your Laravel domain is http://laravelapp.io
, we will configure the ability to visit http://laravelapp.io/blog
with full support for pretty URLs, administration control, and all the other bells and whistles of WordPress without disturbing the existing Laravel application. This is how to complete the laravel wordpress integration.
- Create a
blog
directory within thepublic
directory of your Laravel Install - Place all the WordPress Files in the
blog
directory - Create a new database on your server to support the new WordPress install
- Update your nginx configuration to support WordPress and Laravel Simultaneously
- Restart the nginx service
- Install WordPress
notes: Again, we make a few assumptions for this tutorial. One being that you have your own VPS, and two being that it is a LEMP stack. LEMP is Linux, Nginx, MySQL, and PHP. Your own VPS gives you the ability to create an additional database easily. It’s a good idea to keep the database from your main application and the WordPress database separate. If you we’re looking to combine the two, there are ways to do that, but not in this tutorial.
The index.php
file in the public
directory is the entry into your Laravel application. You will load the WordPress files into the public/blog
directory such that the index.php
file for WordPress exists in blog
. This is the entry into your WordPress blog.
3. Create a database for the WordPress install
If you have a GUI tool such as phpmyadmin installed, this will be a cinch. If not, you can still ssh into your VPS and create the new database from the command line without too much trouble. For our example, we used the name wordpress
as the new database name.
4. Update The Nginx Configuration
This is an example configuration that should work. Replace the example domain name with your own.
server { listen 80; server_name laravelapp.io; root /home/vagrant/Code/laravel; index index.html index.htm index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log off; error_log /var/log/nginx/laravelapp.io-error.log error; sendfile off; location ~ .php$ { fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; } location ~ /.ht { deny all; } location ^/blog/index.php(/.*)?$ { fastcgi_split_path_info ^(/blog/index.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_read_timeout 1000; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; } location /blog/ { if (!-e $request_filename) { rewrite ^.*$ /blog/index.php last; } try_files $uri $uri/ marketreport/index.php?args; rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last; rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last; } }
Once you have updated the nginx configuration file, found in /etc/nginx/sites-available
, be sure to restart the nginx service via sudo service nginx restart
.
5. Configure wp-config.php
Set up your database configuration for the blog by renaming wp-config-sample.php
to wp-config.php
and fill in the credentials to connect to the new database you created in step 3. A snippet of that configuration file might look a bit like this.
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'wordpress'); /** MySQL database username */ define('DB_USER', 'YOUR-USER-NAME'); /** MySQL database password */ define('DB_PASSWORD', 'YOUR-PASSWORD'); /** MySQL hostname */ define('DB_HOST', 'localhost'); /** Database Charset to use in creating database tables. */ define('DB_CHARSET', 'utf8'); /** The Database Collate type. Don't change this if in doubt. */ define('DB_COLLATE', '');
6. Run Through the WordPress Install
With all of the legwork out of the way, you can now point a browser to http://laravelapp.io/blog
and you will be greeted with the familiar install screen of a WordPress installation. Fill out the fields as needed and submit the data, and you should now have your working blog right alongside your Laravel application on the same domain.
How to add a WordPress Blog to your Laravel Application Summary
Well there you have it, you now know how to combine laravel and wordpress together so you can have a custom laravel application running on the same domain as a blog which may be used to support your Laravel service. Have fun!