Sunday, February 4, 2018

How to install wordpress with apache, mod_proxy_fcgi and php-fpm on Ubuntu 16.04

Hi everyone,

In one of the previous posts we've discussed how to setup basic website to work with apache and php-fpm. And today I'd like to continue that topic and install wordpress to work with apache and php-fpm. Once you've got that basic website to work with php-fpm, there are not many differences from 'non-php-fpm' wordpress setup. Anyway, I am still going to describe it here in details.

Part 1. Apache, mod_proxy_fcgi, php-fpm


1. Install packages:
sudo apt-get install apache2 php-fpm

2. Create apache virtual host config file (/etc/apache2/sites-available/site.conf):
<VirtualHost *:80>
 ServerName site.com
 DocumentRoot /var/www/site
 <FilesMatch \.php>
  SetHandler proxy:fcgi://127.0.0.1:9000
 </FilesMatch>
</VirtualHost>

3. Enable proxy_fcgi module and site.conf:
sudo a2enmod proxy_fcgi
sudo a2ensite site
sudo service apache2 restart

4. Create file structure of your site:
sudo mkdir -p /var/www/site
sudo touch index.php
and add the following code to your index.php file:
<?php
phpinfo();
?>
Using this file we will check that php code works, but you will need to remove that file later, because it's not secure to print that info on live website.

5. Create pool for php-fpm.
Navigate to php-fpm pool directory:
cd /etc/php/7.0/fpm/pool.d/
Use default template as a base for the pool for our new website. So copy it:
sudo cp www.conf site.conf
Then open site.conf and change two parameters:
- pool name: replace [www] => [site]
- listen:
listen = /run/php/php7.0-fpm.sock
change it to this:
;listen = /run/php/php7.0-fpm.sock
listen = 127.0.0.1:9000
Here I use tcp sockets instead of file sockets. This is just for simplicity. You can change it to whatever value later, once the basic setup is working.

6. Restart php-fpm:
sudo service php7.0-fpm restart
At this point you should have basic website working. When you access http://site.com/index.php it should print some information about your server.

Part 2. Wordpress


1. Install required packages:
sudo apt-get install mysql-server php-mysql php-gd

2. Create database and database user
Login to database as root:
mysql -u root -p
Create database and user:
create database sitedb;
grant all privileges on sitedb.* to siteuser@'localhost' identified by 'password';

3. Download wordpress, extract files
sudo chown $USER /var/www/site
cd /var/www/site
wget wordpress.org/latest.zip
unzip latest.zip
cp -R wordpress/* ./
rm -rf wordpress
rm latest.zip

4. Create wp-config.php
cp wp-config-sample.php wp-config.php
In the config file change database name, database user name, database user password and also add this setting:
define('FS_METHOD', 'direct');

5. Fix file permissions
sudo chown -R $USER:www-data /var/www/site
sudo find /var/www/site -type f -exec chmod 660 {} \;
sudo find /var/www/site -type d -exec chmod 2770 {} \;
Here I used group www-data under which php-fpm/apache is running. This will allow php-fpm/apache to read and write files. But ideally it would be good to have different users/permissions for apache and php-fpm, so that apache could only read and php-fpm read/write. I leave this task for you as a homework ;-)

6. Enable rewrite module, fix apache config (This is necessary for permalinks to work)
sudo a2enmod rewrite
Also edit apache virtual host config file (/etc/apache/sites-available/site.conf) and add this setting which makes apache to honor .htaccess files:
<Directory /var/www/site>
    AllowOverride all
</Directory>
Restart apache:
sudo service apache2 restart

This is it. I hope I didn't forget anything. But if I did, please let me know in comments below. Now if you try to access http://site.com your wordpress should be working correctly.

If you're still confused you can also watch this video where I do all the same steps:

No comments:

Post a Comment