This howto guide explains more deeply on how to install LEMP (Linux, Nginx, MariaDB, PHP) on pre-installed Linux servers and installation instructions can be used on CentOS 6.8 servers using Remi and Nginx repositories with YUM command.
In this guide we use latest MariaDB, PHP and Nginx versions with PHP-FPM modules with Nginx. So, before moving further for installation let’s discuss about Nginx and PHP-FPM.
What is Nginx?
Nginx (Engine X) is open source robust light and high performance Web server, reverse proxy sever and also mail proxy server for HTTP, SMTP, POP3 and IMAP protocols. To know more about features visit http://wiki.nginx.org/Overview
What is PHP-FPM?
PHP-FPM stands for FastCGI Process Manager is an alternative PHP FastCGI implementation with some additional useful features for heavily loaded websites. For more information visit http://php-fpm.org/
STEP 1: INSTALL EPEL AND REMI REPOSITORY
1 2 |
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm |
Then type this command:
1 |
yum update -y |
STEP 2: INSTALLING NGINX REPOSITORY
Create file /etc/yum.repos.d/nginx.repo
and add following content to repo file:
1 2 3 4 5 |
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 |
STEP 3: INSTALLING NGNIX, MariaDB, PHP & PHP-FPM
Install NGINX
with the following command:
1 |
yum install nginx |
Start the NGINX service with the following command:
1 |
/etc/init.d/nginx start |
Configure NGINX to start when the system is rebooted:
1 2 |
chkconfig --add nginx chkconfig nginx on |
verify if nginx is running or not
1 2 |
# netstat -tanp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 974/nginx |
Or You can confirm that nginx has installed on your VPS by directing your browser to your IP address.
You can run the following command to reveal your server’s IP address.
1 |
ifconfig eth0 | grep inet | awk '{ print $2 }' |
On the page, you will see the words, “Welcome to nginx”
Congratulations! You have now installed nginx.
Install MariaDB
Create a new MariaDB repository file /etc/yum.repos.d/mariadb.repo
with nano:
1 |
nano /etc/yum.repos.d/MariaDB.repo |
And paste the following text
1 2 3 4 5 6 7 |
# MariaDB 10.1 CentOS repository list # http://downloads.mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos6-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 |
Then exit and save the file by pressing Ctrl+X, then Y.
As you see in the above example, it includes a gpgkey line to automatically fetch the GPG key that MariaDB uses to sign the repositories. This key is needed to verify the integrity of the package downloads.
Note: To find which repo you should use with the MariaDB repository generator.
For a standard server installation, you will need to download and install at least the client, shared, and server RPM files.
1 |
yum install MariaDB-server MariaDB-client |
When the installation process is completed, you can start MariaDB. Please note that the MariaDB service is named “mysql”, so the command below is not a typo.
1 |
/etc/init.d/mysql start |
Run this command to start MariaDB on every boot.
1 2 |
chkconfig --add mysql chkconfig mysql on |
You can check the version of the MariaDB installation with the following command:
1 2 |
# mysql -V mysql Ver 15.1 Distrib 10.1.21-MariaDB, for Linux (x86_64) using readline 5.1 |
So far so good!
You can do some configuration of MariaDB with this command:
1 |
mysql_secure_installation |
The prompt will ask you for your current root password.
Since you just installed MariaDB, you most likely won’t have one, so leave it blank by pressing enter.
1 2 3 4 5 6 7 8 9 |
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): |
Then the prompt will ask you if you want to set a root password. Go ahead and choose Y and follow the instructions.
CentOS automates the process of setting up MariaDB, asking you a series of yes or no questions.
It’s easiest just to say Yes to all the options. At the end, MariaDB will reload and implement the changes.
Now, you have to restart MariaDB.
1 |
/etc/init.d/mysql restart |
Install PHP and PHP-FPM
Run the following command:
1 |
yum --enablerepo=remi-php56 install php php-fpm php-devel php-mysql |
PHP Modules
PHP also has a variety of useful libraries and modules that you can add onto your server. You can see the libraries that are available by typing:
1 |
yum --enablerepo=remi-php56 search php- | more |
Terminal then will display the list of possible modules. The beginning looks like this:
1 2 3 4 5 6 7 8 |
============================== N/S Matched: php- =============================== php-EasyRdf-doc.noarch : Documentation for php-EasyRdf php-cloudfiles-devel-doc.noarch : Development documentation for php-cloudfiles php-fedora-autoloader-devel.noarch : php-fedora-autoloader devel php-fpdf-doc.noarch : Documentation for php-fpdf php-libvirt-doc.noarch : Document of php-libvirt php-pear-Auth-radius.noarch : RADIUS support for php-pear-Auth php-pear-Auth-samba.noarch : Samba support for php-pear-Auth |
To see more details about what each module does, type the following command into terminal, replacing the name of the module with whatever library you want to learn about.
1 |
yum --enablerepo=remi-php56 info name_of_the_module |
Once you decide to install the module, type:
1 |
yum --enablerepo=remi-php56 install php-mbstring php-mcrypt php-soap php-apc php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel |
Start the php-fpm service with the following command:
1 |
/etc/init.d/php-fpm start |
Make sure php-fpm starts on boot with the following command:
1 2 |
chkconfig --add php-fpm chkconfig php-fpm on |
Configure php
Using your favorite editor, edit the file /etc/php.ini
and change the default timezone. It should look similar to the block below.
1 2 3 4 |
[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = UTC |
And find the line, cgi.fix_pathinfo=1, and change the 1 to 0.
1 |
cgi.fix_pathinfo=0 |
If this number is kept as a 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit.
Using your favorite editor, edit the file /etc/php-fpm.d/www.conf
and change user and group from apache to nginx. It should look similar to the block below.
1 2 3 4 5 6 7 |
; 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 |
Save and Exit.
Finish by restarting php-fpm.
1 |
/etc/init.d/php-fpm restart |
NOTE: If you want change listen = 127.0.0.1:9000
to /var/run/php5-fpm.sock
. do it this step.
Edit the file /etc/php-fpm.d/www.conf
and Find the line, listen = 127.0.0.1:9000
, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock
.
1 2 3 4 5 6 |
listen = /var/run/php5-fpm.sock . . . listen.owner = nginx listen.group = nginx |
Save and Exit.
Finish by restarting php-fpm.
1 |
/etc/init.d/php-fpm restart |
Now we need to make some changes to the Nginx configuration file so that php-fpm works correctly with Nginx. Using your favorite editor, edit the file /etc/nginx/conf.d/default.conf
and copy the following block below into your conf file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; # fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~ /\. { deny all; access_log off; log_not_found off; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } } |
Create a test PHP file in the following directory with the following command:
1 |
nano /usr/share/nginx/html/info.php |
Add in the following line:
1 2 3 |
<?php phpinfo(); ?> |
Then Save and Exit.
Test your page in your browser with the following hyperlink changed with yourIP address:
http://YOUR.IP.ADD.RESS/info.php
Congratulations! You have just installed LEMP on your CentOS 6.8 Server. Thank you for following along in this How-To! Check back with us for any new updates.
LEMP Configuration for High Traffic WordPress on Small VPS
Source:
- digitalocean.com
- tecmint.com
- atlantic-community
- github.com
[lastupdated]
.