To set up a LEMP stack (Linux, Nginx, MySQL, PHP) on your server, you'll install and configure Nginx as your web server, PHP-FPM for processing PHP scripts, and Percona MySQL as your database. This powerful combination provides excellent performance for web applications and can be configured to run via TCP or Unix sockets for optimal efficiency.
This guide walks you through installing each component, configuring them to work together, and optimizing settings for production use on CentOS/RHEL systems.
Prerequisites and Repository Setup
Before installing the LEMP stack components, you need to enable the necessary repositories. The EPEL repository provides Nginx and PHP-FPM packages, while the Percona repository contains the MySQL-compatible Percona Server.
Enable both repositories with these commands:
rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
Step 1: Install Percona MySQL Server
Percona Server is a drop-in replacement for MySQL that offers enhanced performance and additional features. Install Percona Server 5.6 with both server and client components:
yum install Percona-Server-server-56 Percona-Server-client-56
Configure InnoDB for Better Performance
To improve InnoDB table performance, enable the file-per-table setting. This creates separate tablespace files for each table rather than storing everything in one shared file.
Edit the MySQL configuration file:
vim /etc/my.cnf
Add this line under the [mysqld] section:
[mysqld]
innodb_file_per_table
Enable and Start MySQL
Configure Percona to start automatically on boot and then start the service:
chkconfig mysql on
/etc/init.d/mysql restart
Step 2: Install and Configure Nginx
Nginx is a high-performance web server that handles HTTP requests efficiently. Install it from the EPEL repository:
yum install nginx -y
Enable Nginx to start on boot:
chkconfig nginx on
Start the Nginx service:
/etc/init.d/nginx start
Configure Firewall for Web Traffic
Open port 80 in your firewall to allow HTTP traffic to reach your web server:
/sbin/iptables -I INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
Save the firewall rules so they persist after reboot:
/etc/init.d/iptables save
Step 3: Install PHP-FPM and PHP Extensions
PHP-FPM (FastCGI Process Manager) provides a robust way to process PHP scripts with better performance than traditional CGI. Install PHP-FPM along with essential extensions:
yum install php-fpm php-mysql php-mssql
Enable PHP-FPM to start on boot:
chkconfig php-fpm on
Start the PHP-FPM service:
/etc/init.d/php-fpm start
Verify PHP-FPM Installation
Check that PHP-FPM is running correctly:
php-fpm -v
You should see output showing the PHP version and FPM-FCGI information. If you want to use the standard php command, create a symbolic link:
ln -s /usr/sbin/php-fpm /usr/sbin/php
Step 4: Configure PHP Security Settings
Before connecting Nginx to PHP-FPM, configure an important security setting. The cgi.fix_pathinfo directive should be set to 0 to prevent PHP from processing files it shouldn't.
Modify the PHP configuration:
sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini
When this value is 1, PHP attempts to process the closest matching file, which creates security risks. Setting it to 0 ensures PHP only processes exact file matches.
Restart PHP-FPM to apply the change:
/etc/init.d/php-fpm restart
Step 5: Connect Nginx to PHP-FPM (TCP Method)
PHP-FPM can communicate with Nginx via TCP sockets or Unix domain sockets. The TCP method uses network connections on port 9000.
Verify PHP-FPM TCP Configuration
Check the PHP-FPM pool configuration:
vim /etc/php-fpm.d/www.conf
Look for the listen directive. It should show:
listen = 127.0.0.1:9000
Configure Nginx Virtual Host
Edit your Nginx configuration file:
vim /etc/nginx/conf.d/default.conf
Locate the PHP location block (or add it) and configure it as follows:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Restart Nginx to apply the configuration:
/etc/init.d/nginx restart
Test PHP Processing
Create a test PHP file:
vim /usr/share/nginx/html/myphpinfo.php
Add this content:
<?php phpinfo(); ?>
Access the file through your browser at http://your-server-ip/myphpinfo.php. You should see the PHP information page displaying your configuration.
Alternative: Connect Nginx to PHP-FPM (Unix Socket Method)
Unix domain sockets typically offer better performance than TCP for local communication. To use sockets instead of TCP, follow these steps.
Configure PHP-FPM for Unix Sockets
Edit the PHP-FPM pool configuration:
vim /etc/php-fpm.d/www.conf
Change the listen directive to:
listen = /dev/shm/php-fpm.sock
Using /dev/shm places the socket in shared memory for faster access.
Update Nginx Configuration
Edit your Nginx configuration:
vim /etc/nginx/conf.d/default.conf
Modify the PHP location block to use the Unix socket:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Restart both services:
/etc/init.d/php-fpm restart
/etc/init.d/nginx restart
Test PHP processing using the same phpinfo.php file created earlier. The page should load successfully, confirming socket communication is working.
Frequently Asked Questions
Which is better for PHP-FPM: TCP sockets or Unix sockets?
Unix domain sockets typically provide better performance for local communication between Nginx and PHP-FPM because they eliminate TCP/IP overhead. Use Unix sockets unless you need PHP-FPM running on a separate server, which requires TCP connections.
Why use Percona Server instead of standard MySQL?
Percona Server offers performance improvements, additional diagnostic tools, and enhanced scalability while maintaining complete compatibility with MySQL. The innodb_file_per_table setting provides better space management and easier table maintenance.
How do I secure my new LEMP stack installation?
After installation, run mysql_secure_installation to set a root password and remove test databases. Configure proper file permissions, disable directory listing in Nginx, remove the phpinfo.php test file, and keep all components updated with security patches.
Can I install additional PHP extensions later?
Yes, you can install additional PHP extensions anytime using yum. Common extensions include php-gd for image processing, php-xml for XML parsing, and php-mbstring for multibyte string handling. Restart PHP-FPM after installing new extensions.
OBHost provides 24/7 technical support for server configuration and optimization questions. If you need assistance with your LEMP stack setup or encounter any issues, contact our support team at https://www.obhost.net/contact or email support@obhost.org.