Ive been using CentOS since CentOS 4 and it is my Linux flavour of choice because a) its what I know, and b) it has compatibility with Red Hat Enterprise.
So when I moved from AWS to Digital Ocean for this blog it was my go to choice.
The following is a guide on installing the latest PHP stack backed by MySQL and Nginx (interfacing with PHP via FastCGI-Process-Manager (FPM)) on currently the latest version of CentOS, CentOS 6. WordPress will run nicely on top of this stack (incase you were wondering).
All commands should be run as the root user. Replace vi in the commands with your editor of choice (vi, nano, emacs, other)
Mysql
First install mysql and mysql server
1 |
yum install mysql mysql-server |
Nginx
Next install Nginx from their official repo.
Create the following file
1 |
vi /etc/yum.repos.d/nginx.repo |
then put the following configuration within that file
1 2 3 4 5 |
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 |
Now install nginx
1 |
yum install nginx |
Setting up a virtual domain in Nginx (replace ‘yourdomain.com’ with your actual domain)
1 |
vi /etc/nginx/conf.d/yourdomain.com.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
server { listen 80; server_name yourdomain.com; #charset koi8-r; access_log /var/log/nginx/yourdomain.com.access.log main; location / { root /usr/share/nginx/yourdomain.com; index index.php index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/share/nginx/yourdomain.com; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } } |
PHP
PHP is in Extra packages so we need to add these repositories
1 2 3 4 5 6 7 8 9 |
//32 bit system rpm -Uvh http://epel.blizoo.mk/epel/6/i386/epel-release-6-8.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm //64 bit system rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm |
Install, PHP 5.5, PHP-FPM and the new PHP 5.5 op-cache by Zend
1 |
yum --enablerepo=remi,remi-php55 install php-fpm php-mysql php-opcache php-common |
Changing the PHP FPM process’s to run under the user ‘nginx’.
Open the following file
1 |
vi /etc/php-fpm.d/www.conf |
Then find the lines
1 2 |
user = apache group = apache |
And change them to
1 2 |
user = nginx group = nginx |
Set up a test php page
1 |
echo"<?php phpinfo();" > /usr/share/nginx/yourdomain.com/index.php |
Restarting the process’s and setting them to spawn at system boot
Finishing up and testing
1 2 3 |
service mysqld restart; service nginx restart; service php-fpm restart; |
Enable the services on system startup
1 |
for s in nginx php-fpm mysqld; do chkconfig $s on; done |
Finally test to see if the setup is working..
Add a line to your local computers hosts file
1 |
162.243.150.25 mindfsck.net |
Replacing the domain with youdomain.com and the IP with your CentOS machines IP address
Next go to yourdomain.com in your browser and check for the following in the PHP config output
Mark Greenall
This is a good article. As a follow up you should look at NGiNX security by compiling from source?
Ankit
Awseome tutorial. Helped me a lot with the installation.
Thanks a ton Jason.
Jason Whatson
Your welcome Ankit 🙂
Pranav
Thanks Jason.
It helped me setup my server.
I read through a lot of tutorials, but nothing worked as great as yours.
I have bookmarked it so I dont loose it.
Have A Great Day.
Performa Digital
Great article for any web developer or server admin. Nginx is such a great platform for fast WordPress sites.
Thanks very much.
Kaden
Is it me, or did you forget to enable opcache? From what I understand it is not enabled by default. There are additional parameters that need to be added to configure and enable it.