If you’re anything like me server’s piss you off. Not because they aren’t cool, but because you’ll get to a point where you have everything setup the way you want then you run into an issue that nobody on the internet has experienced. Worse still I’ll have to start over completely with a new distro because the one you want to use isn’t supported by someone or whoever. So this is the Ultimate Guide to Setting up a WordPress Server for complete noobs who know almost nothing about linux or servers. This setup is a simple LEMP stack designed not to be too complicated for beginners. For more High Performance servers see my other post here.

First off you need a server. As opposed to getting a physical computer and setting it up to run linux it’s become far easier to just pay someone else to host it. I can personally vouch for Linode however if you’re looking for something a little cheaper DigitalOcean will do just fine. Sign up and create your droplet/linode.

Now comes a big fork in the road. Which distro? Distros come in all shapes and colors and each run differently. You’ll hear strong opinions on both sides for every distro. However I’m using Ubuntu 14.04 LTS. Why? Because since it’s an LTS it will be supported for quite some time to come, and not a whole lot of distros can claim that. Plus most server guide’s and tutorials use Ubuntu 14.04. If you know what the hell you’re doing, get off my blog, but otherwise use a different distro but be warned a lot of the rest of this tutorial won’t apply.

Download and install Putty so you can connect to your server, just put the IP address and your username should be root and whatever your password is.

Initial Server Setup

The first and most important command to run is

sudo apt-get update

because this will update all your software that we’ll be downloading.

Next we want to install MySQL and PHP using the following commands:

sudo apt-get install mysql-server php5-mysql

When it asks for a username and password for root make sure it’s different from your SSH root password, this is for the MySQL database.

Now we’ll securely install MySQL with this command:

sudo mysql_install_db

And we’ll set it up with this:

sudo /usr/bin/mysql_secure_installation

When it asks for your root password enter the one you used to setup MySQL with. You don’t need a new root password, you’ll want to remove anonymous users and disallow root login remotely. Remove the test database and access to it and reload the privilege tables and you should be good!

Now let’s setup the MySQL server WordPress will run on. Run this command and enter your root password you used in the installation

mysql -u root -p

Now run the following commands with YOURPASSWORD replaced with your real password

CREATE DATABASE wordpress; CREATE USER [email protected]; SET PASSWORD FOR [email protected]= PASSWORD("YOURPASSWORD"); GRANT ALL PRIVILEGES ON wordpress.* TO [email protected] IDENTIFIED BY 'YOURPASSWORD'; FLUSH PRIVILEGES; exit;

Fantastic now we have a MySQL database to use!

Webserver Setup

Now we’ll install a webserver. There are two popular webservers, Apache and Nginx. Both have proven that they are very capable, however I’ll be using Nginx because of it’s easy configuration and the way it’s event-based vs process-based like Apache. You can read more about the comparison here.

We’ll install Nginx using this:

sudo apt-get install nginx

And we’ll start it using this

sudo service nginx start

Now we’ll install PHP and a bunch of other useful tools!

sudo apt-get install php5-fpm php-pear php5-common php5-mysql php-apc php5-gd

Next we’ll fix a security issue in PHP by opening this file

sudo nano /etc/php5/fpm/php.ini

Find this segment by pressing Ctrl+W and searching for this:

cgi.fix_pathinfo=0

Save and close the file.

Now we’ll configure PHP to listen to the correct place. Open up the following file:

sudo nano /etc/php5/fpm/pool.d/www.conf

Find the following line

listen = 127.0.0.1:9000

And change it to

listen = /var/run/php5-fpm.sock

Now you can restart PHP to apply the changes:

sudo service php5-fpm restart

Webserver Configuration

Now we’ll do some tweaks so the webserver will serve the correct files and do caching correctly.

First run the the following command to open the Nginx Configuration file

sudo nano /etc/nginx/nginx.conf

And add the following lines directly under the http subsection

port_in_redirect off; gzip_types text/css text/xml text/javascript application/x-javascript; gzip_vary on;

Now we’ll remove the default Nginx website configuration with this command

sudo rm /etc/nginx/sites-available/default

And replace it using this command

sudo nano /etc/nginx/sites-available/default

Enter the following and switch out the bolded sections with your site info

server { ## Your website name goes here. server_name domainname.com www.domainname.com; ## Your only path reference. root /home/master/applications/zachadams/public_html/; listen 80; ## This should be in your http block and if it is, it's not needed here. index index.html index.htm index.php; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } location = /apple-touch-icon.png { access_log off; log_not_found off; } location = /apple-touch-icon-precomposed.png { access_log off; log_not_found off; } location ~ /. { deny all; access_log off; log_not_found off; } location / { # This is cool because no php is touched for static content try_files $uri $uri/ /index.php?q=$uri&$args; } location ~ .php$ { fastcgi_buffers 8 256k; fastcgi_buffer_size 128k; fastcgi_intercept_errors on; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/var/run/php5-fpm.sock; } location ~* .(css|js|png|jpg|jpeg|gif|ico)$ { expires 1d; } }