This is a complete guide on how to install nginx, php-fpm, APC, MariaDB, and phpMyAdmin on CentOS 6. If you are looking for a similar guide on how to do this on CentOS 7, then this article is the one you’re looking for instead.
First you need to make sure you have the EPEL repo installed. If you don’t, simply Google “Install EPEL CentOS” and you’ll find what you need to proceed. If you need to check if you have EPEL installed then just type this:
1 |
yum repolist | grep epel |
If you see any output from the command above, then you already have EPEL installed and you’re ready to move on.
Now let’s install nginx and php-fpm:
1 |
yum install nginx php-fpm |
Usually at this point I like to start the nginx service and accept ports 80 and 443 in iptables so we can access our server to ensure nginx is running and delivering pages properly:
1 |
service nginx start |
Now lets add port 80 to iptables so people can connect to your webserver on port 80:
1 |
iptables -A INPUT -p tcp --dport http -j ACCEPT |
(optional): You can also add port 443 if you want to accept SSL via https:// protocol:
1 |
iptables -A INPUT -p tcp --dport https -j ACCEPT |
Now let’s flush the iptables rules:
1 |
iptables -F |
And at this point, you should be able to browse to the ip address of your server with something like http://1.2.3.4 where 1.2.3.4 is your server’s ip address. If everything is in order, let’s install MariaDB!
If you’re not familiar with MariaDB, it’s a drop-in replacement for MySQL and anything that runs on MySQL will run on MariaDB. It has some enhancements and things run better overall. For more information read up on MariaDB here.
To get started, we’ll install the remi repo:
1 2 |
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm yum --enablerepo=remi-test --disablerepo=remi install compat-mysql55 |
Now we’ll add a repo for MariaDB:
1 |
vim /etc/yum.repos.d/maria.repo |
Paste the following into that file:
1 2 3 4 5 |
[mariadb] name=MariaDB baseurl=http://yum.mariadb.org/5.5/centos6-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 |
Then upgrade (to make sure any previous MySQL stuff gets overwritten), install MariaDB, and start the service:
1 2 3 |
yum upgrade yum install MariaDB-devel MariaDB-client MariaDB-server perl-DBD-MySQL service mysql start |
Note: if you get a huge list of compatibility errors when you run yum upgrade during the transaction test, just do “yum remove mysql” and try the commands above.
At this point I’d suggest securing MySQL:
1 |
mysql_secure_installation |
Note: if you don’t know about correct horse battery staple passwords, check them out.
Next step is to configure php-fpm to run through the local unix:// socket. This is faster than running things through port 9000 (the default) as it doesn’t take any network overhead to pass php files to php-fpm.
1 |
vim /etc/php-fpm.d/www.conf |
Look for the line that says “listen” towards the top and change it to this:
1 |
listen = /var/run/php-fpm/www.sock |
Before we start php-fpm, let’s create the www.sock file and change the owner to nginx:
1 |
touch /var/run/php-fpm/www.sock && chown nginx:nginx /var/run/php-fpm/www.sock |
Now we can start php-fpm…
1 |
service php-fpm start |
Okay, now that we have MariaDB, nginx, and php-fpm all setup and ready to go, we can now setup phpMyAdmin and nginx vhosts.
1 |
yum install phpmyadmin --enablerepo remi |
Now let’s configure phpMyAdmin:
1 2 |
cp /usr/share/phpMyAdmin/config.sample.inc.php /usr/share/phpMyAdmin/config.inc.php vim /usr/share/phpMyAdmin/config.inc.php |
Change your blowfish_secret to a random secure string and save the file. Now let’s setup an alias for /sqladmin/ on any of your sites to go to phpMyAdmin:
1 |
vim /etc/nginx/sqladmin.conf |
Paste the following into that file:
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 |
location /sqladmin/ { alias /usr/share/phpMyAdmin/; index index.php; } location ~ ^/sqladmin/(.+\.php)$ { alias /usr/share/phpMyAdmin/$1; fastcgi_pass unix:/var/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; # From fastcgi_params fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT /usr/share/phpMyAdmin; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_param REDIRECT_STATUS 200; } |
And finally, it’s time to setup a vhost in nginx for your site. I’m going to use the vhost that I’ve used for this site. You can change any instance of “robido” and “robido.com” to your site’s URL for the following steps:
1 |
vim /etc/nginx/conf.d/robido.conf |
Paste the following into that file (replacing robido.com as needed). Note: lines 19-24 are specifically for this WordPress site on a subdirectory install of http://robido.com and adding the rewrites for WordPress SEO sitemap.xml files. These lines can be excluded if they don’t apply to your install.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 |
server { listen 80; server_name .robido.com; root /www/robido.com/public; client_max_body_size 512M; # Include phpmyadmin include /etc/nginx/sqladmin.conf; # Logs access_log /www/robido.com/logs/access.log main; error_log /www/robido.com/logs/error.log; # Default location settings location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?$args; } # 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; } #error_page 404 /404.html; # Pass the PHP scripts to FastCGI server (locally with unix: param to avoid network overhead) location ~ \.php$ { # Prevent Zero-day exploit try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini fastcgi_pass unix:/var/run/php-fpm/www.sock; 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 location ~ /\.ht { deny all; } # Exclude favicon from the logs to avoid bloating when it's not available location /favicon.ico { log_not_found off; access_log off; } } |
You’ll notice there’s a note in that config that suggest to modify your php.ini file to change your cgi.fix_pathinfo to 0. Edit your /etc/php.ini file and search for “fix_pathinfo” then set it to 0:
1 |
cgi.fix_pathinfo=0 |
Now that we have a vhost setup, we can create directories for our site, add a user for SFTP/FTP/SSH and restart nginx:
1 2 3 4 5 6 |
mkdir /www && cd /www mkdir robido.com && cd robido.com mkdir public logs useradd -d `pwd` robido chown -R robido:robido . passwd robido |
Once you’ve set your password we can reload nginx to pull in the new configs. Note: it’s always good to use “nginx -t” to check to see that your configs don’t have any problems before restarting:
1 |
nginx -s reload |
And if you want nginx, php-fpm, and mysql to start on boot:
1 2 3 |
chkconfig mysql on chkconfig nginx on chkconfig php-fpm on |
That’s it folks! You should now be able to create an index.html, index.htm, or index.php file (in /www/robido.com/public/index.php for example) and the contents of that file will be displayed when you point your domain (robido.com in this case) to your server and access http://robido.com. You can also browse to phpMyAdmin with a URL like http://robido.com/sqladmin/ (the trailing / is necessary here with our current config).
If any of the steps above throw errors for you, or you have questions or issues, feel free to post a comment and I’ll help debug. The only issue that I had during my setup process was some conflicts of php-mcrypt, php-mbstring, and the other libraries that phpMyAdmin needed (which were a different version than my install from the remi repository). I added in –enablerepo remi into the phpMyAdmin install step to hopefully prevent anyone else from having the same issue.
If you want to make your WordPress site SCREAMING fast with this setup, check out my other post about installing batcache: Add batcache to your WordPress site (nginx + APC + php-fpm + batcache). Any comments, questions, or concerns are encouraged!
Hello,
Nice tutorial, however i thought creating of the site directory should come first instead of last?
Hey Kingsley,
So long as the
nginx -t
is ran shy of reloading the config withnginx -s reload
it’s fine either way since nginx will bark at you if a directory you need doesn’t exist. Feel free to create directories first if that suits you. Thanks for the feedback 🙂How do i add phpmyadmin in a subfolder under one of my sites ? so mydomain.com/phpmyadmin??
Hey Kingsley,
This tutorial actually goes through adding an alias of /sqladmin/ to all of your sites that you want it enabled on (the trailing / is required at the end). In your nginx virtual hosts you can add the
include /etc/nginx/sqladmin.conf;
line like I did in my example vhost and it should make the http://mysite.com/sqladmin/ URL pull up phpMyAdmin. You can of course change this to /phpmyadmin/ or whatever you’d like.alright,
trying to use this tutorial for multiple blogs on one server