In this tutorial, we will show you how to install LEMP on CentOS 6. For those of you who didn’t know, A LEMP software stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym that represents the Linux operating system, with the Nginx webserver (which replaces the Apache component of a LAMP stack). The site data is stored in a MySQL database (using MariaDB), and dynamic content is processed by PHP.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple. I will show you the step-by-step installation of LEMP (Linux Nginx, MariaDB, and PHP) on the CentOS 6 server.
Prerequisites
- A server running one of the following operating systems: CentOS 6.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install LEMP on CentOS 6
Step 1. First, you need to enable the EPEL repository on your system and make sure that all packages are up to date.
## RHEL/CentOS 6 64-Bit ## wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm
## RHEL/CentOS 6 32-Bit ## wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm
Step 2. Installing Nginx.
We will be installing Nginx with yum, with the following command:
yum update yum install nginx
Start Nginx and add it to automatically start on your system start-up using:
service nginx start chkconfig nginx on
You can verify that Nginx is really running by opening your favorite web browser and entering the URL http://your-server’s-address and you need to open port 80 to make your web server accessible:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT /etc/rc.d/init.d/iptables save
Step 3. Configuring Nginx and Default Virtual Host.
Finally, we need to configure our Nginx virtual hosts. This is much simpler to configure than Apache. Take a look at the config below, it is slightly different from our default config, but I’ll explain the changes below:
nano /etc/nginx/conf.d/default.conf
# The default server # server { listen 80; server_name mydomain.com; location / { root /var/www/html; index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
Step 4. Installing MySQL.
Install MySQL with the following command to begin the installation:
yum install mysql mysql-server
After that add it to your system startup and start the MySQL server using the following commands:
chkconfig --levels 235 mysqld on service mysqld start
By default, MySQL is not hardened. You can secure MySQL using the mysql_secure_installation script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MySQL:
mysql_secure_installation
To log in to MySQL, use the following command (note that it’s the same command you would use to log into a MySQL database):
mysql -u root -p
Step 5. Installing PHP.
Finally, run the commands below to install PHP along with other good-to-have modules:
yum install php php-common php-fpm php-mysql
You may want to install some other PHP extensions required by your applications. The following is a list of the available PHP modules:
php-bcmath => A module for PHP applications using the bcmath library php-cli => Command-line interface for PHP php-common => Common files for PHP php-dba => A database abstraction layer module for PHP applications php-devel => Files needed for building PHP extensions php-embedded => PHP library for embedding in applications php-enchant => Human Language and Character Encoding Support php-gd => A module for PHP applications using the gd graphics library php-imap => A module for PHP applications that use IMAP php-intl => Internationalization extension for PHP applications php-ldap => A module for PHP applications that use LDAP php-mbstring => A module for PHP applications which need multi-byte string handling php-mysql => A module for PHP applications that use MySQL databases php-odbc => A module for PHP applications that use ODBC databases php-pdo => A database access abstraction module for PHP applications php-pear.noarch => PHP Extension and Application Repository framework php-pecl-apc => APC cache optimizing PHP intermediate code php-pecl-memcache => Extension to work with the Memcached caching daemon php-pgsql => A PostgreSQL database module for PHP php-process => Modules for PHP scripts using system process interfaces php-pspell => A module for PHP applications using pspell interfaces php-recode => A module for PHP applications using the recode library php-snmp => A module for PHP applications that query SNMP-managed devices php-soap => A module for PHP applications that use the SOAP protocol php-tidy => Standard PHP module provides tidy library support php-xml => A module for PHP applications which use XML php-xmlrpc => A module for PHP applications which use the XML-RPC protocol php-zts => Thread-safe PHP interpreter for use with the Apache HTTP Server
Configure PHP-FPM:
nano /etc/php-fpm.d/www.conf
Replace the values of user and group with Nginx like below:
; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache Choosed to be able to access some dir as httpd user = nginx ; RPM: Keep a group allowed to write in log dir. group = nginx
Restart Nginx so all the changes take effect:
service nginx restart service php-fpm restart
To test PHP, create a test file named info.php with the content below. Save the file, then browse to it to see if PHP is working:
nano /var/www/html/info.php
<?php phpinfo(); ?>
Congratulations! You have successfully installed the LEMP stack. Thanks for using this tutorial for installing LAMP (Linux Nginx, MariaDB, and PHP) on CentOS 6 system. For additional help or useful information, we recommend you to check the official Nginx, MySQL, and PHP websites.