Monthly Archives: May 2012

28May

Setting up a LEMP Server on CentOS 6

We are going to set up a Linux, Nginx, MySQL and PHP (LEMP) server. Technically, this only covers the EMP part since I am assuming you have root access to a server, which requires an operating system to already have been installed–Centos 6 to be specific. I’m also assuming you already have DNS properly set up for the site you want to host. I will be using vim but you can of course use whatever editor you prefer.

As always before you get started be sure to: yum update

Nginx

To get the lastest stable version and avoid building from source, add the nginx repository to yum: vim /etc/yum.repos.d/nginx.repo

Paste this configuration:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

Now it’s just simply:

yum install nginx
service nginx start

If you visit your server’s ip address (mine’s http://50.116.10.192) you should see a Welcome to Nginx page.

Next we’ll set up your web page. I highly recommend using virtual hosts for ease of management. The idea is setting up separate directories and files to enable managing each site’s configuration individually without affecting the others. To do this, edit your nginx.conf:

cd /etc/nginx vim nginx.conf Add the following include line to the http block in nginx.conf:

http {
  # ...
  include /etc/nginx/sites-enabled/*;
  # ...
}

Now we need to set up the config files for your particular site (assuming you are still in the /etc/nginx directory):

mkdir sites-enabled
mkdir sites-available
cd sites-available
vim jasonchen.me.conf

You can call your configuration files whatever you want but I always name them after site domain name so there’s no ambiguity in which site I am affecting. Paste this into the configuration file:

server {
  listen       80;
  server_name  www.jasonchen.me jasonchen.me;
  access_log   /var/log/jasonchen.me/access.log;
  error_log    /var/log/jasonchen.me/error.log;
  location / {
    root   /opt/jasonchen.me/;
    index  index.html;
  }
}

Note: you should change the server_name, access_log, error_log, and root values to whatever it is for your site. As its name suggests, the access_log and error_log is where nginx will log to. Most programs log to /var/log so I put them there, namespaced by the domain name, but again you can choose whatever name and location pleases you. If those directories have not been created, you should do do that now:

mkdir /var/log/jasonchen.me
mkdir /opt/jasonchen.me

This configuration file is in the sites-available folder and our nginx only includes configurations in the sites-enabled folder so we will have to link the file:

cd /etc/nginx/sites-enabled
ln -s ../sites-available/jasonchen.me.conf jasonchen.me.conf

Now enabling/disabling a site is as easy as creating/destroying a symbolic link. Now let’s set up a dummy html page to test your configuration:

cd /opt/jasonchen.me echo “I just set up nginx with virtual hosts!” >> index.html Now, if you set up DNS properly, you should be able to visit your site and see a page with “I just set up nginx with virtual hosts” (isn’t quirks mode great?).

PHP

Next, we install php. To get the latest version working with nginx, you may need to add the epel and remi repositories. CentOS is a very conservative distribution so it takes a bit longer for them to add new versions of software to their repository. Epel and remi fills in the gaps for those of us that wants the latest and greatest.

rpm -Uvh http://linux.mirrors.es.net/fedora-epel//6/x86_64/epel-release-6-6.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Remi is not enabled by default so you should vim /etc/yum.repos.d/remi.repo. Under [remi] change enabled=0 to enabled=1.

We will be using php-fpm. Alternatives are just plain php or fastcgi but php-pfm offers greater performance and customization, should you need it. I also had the pleasure of working alongside Rasmus Lerdorf for a summer and he uses php-fpm. So there, php-fpm it is.

With remi and epel enabled, installing and starting php for nginx is as easy as:

yum install php php-fpm php-common
service php-fpm start

Now you will need to modify your nginx configuration to use php-fpm. If you followed the nginx section, simply add a location block inside the server block. So the configuration should look like this (vim /etc/nginx/sites-available/jasonchen.me.conf):

server {
  listen       80;
  server_name  www.jasonchen.me jasonchen.me;
  access_log   /var/log/jasonchen.me/access.log;
  error_log    /var/log/jasonchen.me/error.log;
  location / {
    root   /opt/jasonchen.me/;
    index  index.php index.html;
  }
  location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /opt/jasonchen.me$fastcgi_script_name;
  }
}

Again, please substitute your own values for server_name, access_log, error_log, and root. Now remove the index.html file in your site root folder (rm /opt/jasonchen.me/index.html) and add an index.php file (vim /opt/jasonchen.me/index.php) with the following:

<?php
  phpinfo();
?>

Now restart nginx (service nginx restart) and visit your site in a web browser and you should see the familiar php info page.

MySQL

Last, and possibly least, we need to install MySQL.

yum install mysql-server mysql-php
service php-fpm restart

You should secure your installation so run:

mysql_secure_installation

Follow the instructions and once you are done you should be able to run

mysql -u root -p

and start poking around your shiny new database server.

Wrapping Up

Okay so MySQL was not the last thing we needed to do (lest you ignore this sage advice!). You should add nginx, php and mysql to startup so if you need to reboot, your site also restart automatically:

chkconfig --add nginx
chkconfig --add php-fpm
chkconfig --add mysqld
chkconfig --levels 235 nginx on
chkconfig --levels 235 php-fpm on
chkconfig --levels 235 mysqld on

If you want to be really advanced you should install monit or some other monitoring service to make sure none of these components go down, but that is another topic for another day.

That’s it! Enjoy your new LEMP server.

20May

Installing WordPress on Linode

When setting up this blog, I realized that there were quite a few steps to setting up your own hosted WordPress installation. So I wrote down all the steps I took and will share them with you here. This tutorial goes through the whole pipeline, which is quite lengthy, so it will be broken up into four sections.

My only assumptions are that you have picked a domain name and have root access to a server running CentOS 6. The former is a much blogged about topic and the latter is covered here: Getting Started with Linode. This guide is targeted towards Linode users but in theory you should be able to use another host and still follow along.

Contents

  1. Server Setup
  2. WordPress Installation
  3. Optimizing Performance
© Copyright 2014, All Rights Reserved