How to setup nginx, PHP-FPM, and MariaDB on CentOS 6.x

In this tutorial, we will walk you through installing nginx, PHP-FPM, and MariaDB on a fresh VPS. Note that this tutorial assumes that you are installing nginx, PHP-FPM, and MariaDB on a fresh CentOS 6.x 64-bit machine. If you are installing this on any other distribution, your miles will vary. Mean Servers offers VPS setup services, contact the sales department today for a quote, available for both on-net and off-net services.

Install the Required Repositories

Our first step is installing the required repos that our software packs will be using. We will need the epel repo, the MariaDB repo, and the remi repo.

  1. Install the epel repo by running yum install epel-release
  2. Install the MariaDB 10.1 repo, use the repo generator here and put the contents in the repo file at /etc/yum.repos.d/MariaDB.repo
  3. Install the remi repo by downloading and installing the remi RPM with this command: rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Install and Setup MariaDB 10.1

Next, we will install and setup MariaDB 10.1.

  1. Install the required software for running MariaDB with the following command: yum install MariaDB-server MariaDB-client
  2. Start MariaDB by running: service mysql start
  3. Ensure MariaDB starts with the machine by running the following command: chkconfig mysql on
  4. (Optional) You may wish to secure your MariaDB install by running the following command: /usr/bin/mysql_secure_installation
MariaDB 10.1 is now installed, we are ready to move to the next step!

Install and Setup nginx

Next we will install nginx which is a powerful replacement for the most commonly used web server, Apache.

  1. Install nginx by running the following command: yum install nginx
  2. Start nginx by running the following command: service nginx start
  3. Ensure nginx is set to start with the server by running the following command: chkconfig nginx on


Next we need to make some changes to the nginx configuration files, the first is changing how many worker processes are started:

  1. Edit the nginx.conf file with vi /etc/nginx/nginx.conf
  2. Locate the worker_processes variable and change it from 1 to 4
  3. Save and exit the file by pressing the ESC key and typing :wq
Secondly, we need to enable PHP-FPM, without this step, the PHP files will be downloaded to the visitors machine instead of being executed properly

 

  1. Edit the default.conf file with vi /etc/nginx/conf.d/default.conf
  2. Scroll down to the commented out PHP scripts section and add the following replacing the commented out PHP sections:
    1.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
          #
          location ~ \.php$ {
              root           /usr/share/nginx/html;
              fastcgi_pass   127.0.0.1:9000;
              fastcgi_index  index.php;
              fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
              include        fastcgi_params;
          }
  3. Save and exit the file by pressing the ESC key and typing :wq
  4. Restart the nginx process with service nginx restart

nginx is all setup and ready to go, now we just need to install PHP-FPM and your fast web server will be ready for content!


Install and Setup PHP-FPM

The following will vary depending on what version of PHP-FPM you want to install. Mean Servers always recommends a version below the most recent, non-beta release but your needs may vary. It is highly recommend to avoid any PHP versions that are EOL (end of life) since they no longer receive any updates, there WILL BE SECURITY RISKS that CANNNOT be avoided. Do not run a EOL PHP version on a production machine, you will be sorry!

Once you decide on a PHP version you wish to run, edit the remi repo and enable that version. If you would like to run PHP 5.5 or PHP 5.6 (as of January 14, 2016), you would run the following (If you want to run PHP 7, skip this section):

  1. Edit the remi.repo file with vi /etc/yum.repos.d/remi.repo
  2. Locate either the remi-php55 section if you want to run PHP 5.5 or the remi-php56 section if you want to run PHP 5.6 and change enabled=0 to enabled=1
  3. Under the remi section, ensure you change enabled=0 to enabled=1 too.
  4. Save and exit the editor by pressing the ESC key and typing :wq

If you wish to run PHP 7, run these commands instead:
  1. Edit the remi.repo file with vi /etc/yum.repos.d/remi.repo
  2. Under the remi section, ensure you change enabled=0 to enabled=1
  3. Save and exit the remi.repo file by pressing the ESC key and typing :wq
  4. Edit the remi-php70.repo file with vi /etc/yum.repos.d/remi-php70.repo
  5. Under the remi-php70 section and change enabled=0 to enabled=1
  6. Save and exit the remi-php70.repo file by pressing the ESC key and typing :wq
Once you have enabled the proper repos, you are ready to install and setup PHP-FPM

  1. Install PHP-FPM and the most common libraries needed by running yum install php-fpm php-mysql php-common php-json php-pdo Note that depending on how you edited the repo files above, that version will be installed.
  2. Edit /etc/php.ini and ensure cgi.fix_pathinfo=0 is set to 0 and uncommented as this is a security risk. Save and exit the file.
  3. Start PHP-FPM with service php-fpm start
  4. Ensure PHP-FPM is set to start with the machine by running: chkconfig php-fpm on
Next we need to make some changes to the PHP-FPM config files as the defaults are designed for apache.

  1. Edit the www.conf file with vi /etc/php-fpm.d/www.conf
  2. Locate user = apache and change to user = nginx
  3. Locate group = apache and change to group = nginx
  4. Save and exit the file by pressing the ESC key and typing :wq
  5. Restart the PHP-FPM service with service php-fpm restart
Your server is now all set and ready to go. Remember, the default directory for placing web files is located at /usr/share/nginx/html. Feel free to change this to something more suiting along with creating a nginx server file specific to your domain. We will cover how to do that in a later article, for now this is just a quick start guide for those needing to install a very basic nginx, PHP-FPM, and MariaDB server. 
  • 24983 Users Found This Useful
這篇文章有幫助嗎?

相關文章

Setup Your Own Private VPN with PPTP

PPTP VPN IntroductionTired of being geo blocked and not being able to watch your favorite TV...

Protect DirectAdmin from Brute Force Attacks

A common method of gaining access over a server is to use a technique called a brute force...

Changing Your Server's Time Zone

Changing the time zone that your server is set to is relatively simple and straight forward. In...

Synchronizing Your Server's Clock

Synchronizing your server's clock is not very difficult as there is a program that can be ran by...

Installing CSF (ConfigServer Security & Firewall)

The CSF, ConfigServer Security & Firewall, is a powerful firewall that is made for Linux...