June 01, 2015

LEMP on RedHat/CentOS

Let's install Nginx, PHP and MySQL (MariaDB) on a RedHat or CentOS 7 server!

Previous: LEMP on Debian

Let's install Nginx, PHP and MySQL (MariaDB) on a RedHat or CentOS 7 server!### Version

Let's see what version of CentOS we're running:

$ cat /etc/redhat-release 
CentOS Linux release 7.0.1406 (Core) 

SELinux

We should also check SELinux to see if it's enabled/enforcing. (It is, we'll deal withit if it presents itself as an issue).

sudo sestatus

# If we need to turn SELinux off temporarily
sudo setenforce 0

EPEL Repository

To install Nginx, we need Fedora's EPEL repository. There are two ways we can do that.

The long way:

wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
sudo rpm -ivh epel-release-7-5.noarch.rpm

A quicker way:

sudo yum install epel-release

Nginx

Let's search for nginx, which is a newly available package:

$ yum search nginx
# other stuff, and:
nginx.x86_64 : A high performance web server and reverse proxy server

Let's install it, enable it, and run it. CentOS 7 uses Systemd - we'll need to enable the Nginx service using systemctl and then we can use start/status with the service command or continue using systemctl:

sudo yum install nginx
sudo systemctl enable nginx.service
sudo systemctl start nginx.service
sudo systemctl status nginx.service

See Nginx run in browser, and/or curl it:

curl localhost

PHP

We'll get php 5.4, or you can use https://webtatic.com/packages/php56/ to update to php 5.5 or 5.6 via the webtatic-release.rpm:

sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

Or without the extra repositories, let's get PHP 5.4 and start the PHP-FPM process:

sudo yum install php php-mysqlnd php-pdo \
                 php-mbstring php-mbstring \
                 php-mcrypt php-fpm
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
sudo systemctl status php-fpm

Let's edit /etc/php.ini:

cgi.fix_pathinfo=0

And then edit php-fpm conf:

sudo vi /etc/php-fpm.d/www.conf

We're listening over the network at 127.0.0.1:9000. Let's make a server and find out:

Edit the main configuration and make the server there no longer default_server.

sudo vi /etc/nginx/nginx.conf

Change listen 80 default_server; to just listen 80;

Create web root sudo mkdir -p /var/www/phpsite.

Configure the Nginx site:

sudo vi /etc/nginx/conf.d/phpsite.conf

Make it this:

server {
    listen       80 default_server;
    server_name  localhost;
    root         /var/www/phpsite;

    access_log /var/log/nginx/phpsite.com-access.log;
    error_log  /var/log/nginx/phpsite.com-error.log error;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    charset utf-8;
    index index.html index.htm index.php;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
         fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        # fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

Reload nginx:

sudo service nginx reload

If you get an error, check it's status using systemctl, which will show some log output sudo systemctl status nginx.

MySQL (Mariadb)

sudo yum install mariadb-server
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo systemctl status mariadb

sudo mysql_secure_installation

Then you can connect to it using PHP as usual!

All Topics