LEMP server on CentOS 7 with FastCGI

Updated by Ryan Arlan

Contribute on GitHub

View Project | View File | Edit File

This document describes how to install a Linux, Nginx (pronounced engine-x), MariaDB and PHP server, also called LEMP stack, on CentOS 7 with php-fastcgi. It includes configuring php-fastcgi as a service in systemd for easier administration.

Make sure that before starting this guide you have read through and completed our Getting Started guide.

Set the hostname

Before you install any packages, ensure that your hostname is correct by completing the Setting Your Hostname section of the Getting Started guide. Issue the following commands to verify:

1
2
hostname
hostname -f

In the first example, the hostname command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

System Setup

Make sure your system is up to date using yum:

1
yum update

This ensures that all software is up to date and running at the latest version.

Install Nginx from the EPEL

The quickest and easiest way to install Nginx is from the Extra Packages for Enterprise Linux (EPEL) repository. You can install this using rpm:

1
2
3
sudo yum install epel-release
yum update
yum install nginx

This installs the EPEL repository, pulls the metadata from the new repository, and then installs Nginx.

Configuring Nginx

Starting Nginx with systemd

After installing Nginx it needs to be enabled and started in systemd. You can do this with the systemctl command:

1
2
systemctl enable nginx.service
systemctl start nginx.service

You can then check the status to make sure it is running at any time:

1
systemctl status nginx.service

Configure Nginx Virtual Hosts

Once Nginx is installed, you need to configure your ‘server’ directives to specify your server blocks. Each server block needs to have a server and location directive. You can do this multiple ways, either through different server block files or all in the /etc/nginx/nginx.conf file. In this example, we will use the multiple file approach. By default, Nginx uses the /etc/nginx/conf.d directory, and will include any files ending in .conf:

/etc/nginx/conf.d/example.com.conf
1
2
3
4
5
6
7
8
9
10
11
server {
listen  80;
server_name www.example.com example.com;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;
    
location / {
    root  /var/www/example.com/public_html;
    index index.html index.htm index.php;
    }
}

Any additional websites you like to host can be added as new files in the /etc/nginx/conf.d/ directory. Once you set the configuration, you need to make the directories for your public html files, and your logs:

1
mkdir -p /var/www/example.com/{public_html,logs}

Once you have configured your virtual hosts, you’ll need to restart nginx for your changes to be implemented:

1
systemctl restart nginx.service

Deploy PHP with FastCGI

If you are using PHP code with your application, you will need to implement “PHP-FastCGI” in order to allow Nginx to properly handle and parse PHP code. You can install this via YUM from the EPEL repository that was previously installed:

1
yum install php-cli php spawn-fcgi

Once PHP-FastCGI is installed, you will need to create a script to start and control the php-cgi process. Create the /usr/bin/php-fastcgi file in your favorite editor and place the following lines into the file:

/usr/bin/php-fastcgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
if [ `grep -c "nginx" /etc/passwd` = "1" ]; then 
    FASTCGI_USER=nginx
elif [ `grep -c "www-data" /etc/passwd` = "1" ]; then 
    FASTCGI_USER=www-data
elif [ `grep -c "http" /etc/passwd` = "1" ]; then 
    FASTCGI_USER=http
else 
# Set the FASTCGI_USER variable below to the user that 
# you want to run the php-fastcgi processes as

FASTCGI_USER=
fi

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u $FASTCGI_USER -f /usr/bin/php-cgi

Once the /usr/bin/php-fastcgi file has been created, you need to make sure the script is executable:

1
chmod +x /usr/bin/php-fastcgi

You can then run the file manually, or for easier administration, you can set up a systemd service.

Configuring PHP-FastCGI as a service

When PHP-FastCGI is installed it does not automatically get set up as a service in systemd. If you want to be able to more easily control PHP-FastCGI with systemd, you can configure PHP-FastCGI as a systemd service. To do this, you need to create a service file that points to the /usr/bin/php-fastcgi file you created:

/etc/systemd/system/php-fastcgi.service
1
2
3
4
5
6
7
8
9
[Unit]
Description=php-fastcgi systemd service script

[Service]
Type=forking
ExecStart=/usr/bin/php-fastcgi start

[Install]
WantedBy=multi-user.target

Once the file has been created, you will need to reload the systemd daemons, enable the service, then start it:

1
2
3
systemctl daemon-reload
systemctl enable php-fastcgi.service
systemctl start php-fastcgi.service

Now PHP-FastCGI is installed as a systemd service!

Installing MariaDB

Last but not least, your LEMP stack needs a database. MySQL is no longer supported in CentOS 7, so you need to use MySQL’s drop in replacement, MariaDB.

  1. You can install this directly from the repositories:

    1
    yum install mariadb-server
    
  2. Once the installation is complete, you can use it the same way you use MySQL. First however, you must enable and start it in systemd:

    1
    2
    systemctl enable mariadb.service
    systemctl start mariadb.service
    
  3. MariaDB installs with default information and no root password, so it is highly recommend to secure your installation using the build in mysql_secure_installation command:

    1
    mysql_secure_installation
    
  4. You can follow the on screen prompts to remove the default information and set the root password for your mysql installation. Once you set the root password you can log in start adding data:

    1
    mysql -u root -p
    
  5. Enter the root password then you can issue the following commands to create the ‘mydomain’ and ‘myuser’ database and user. You then grant full permissions to the ‘mydomain’ database for the ‘myuser’ login:

    1
    2
    3
    4
    CREATE DATABASE mydomain;
    CREATE USER 'myuser' IDENTIFIED BY 'MyPassword';
    GRANT ALL PRIVILEGES ON mydomain.* to 'myuser';
    exit
    

    You can edit the name of the user, database, and password to what you would like it to be. You can then configure your application to use that database, username, and password to insert data.

Congratulations! You now have a fully functioning and working LEMP stack on CentOS 7!

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This guide is published under a CC BY-ND 4.0 license.