How to install Linux, Nginx, MySQL, PHP (LEMP) stack on CentOS 7

wpspeedster

Well-known member
Registered
Joined
May 6, 2016
Messages
120
Points
18
I have just got a new VPS and I want to install LEMP stack on CentOS 7. Can some of you here share any tips for me to get started?
 

jmlopez

Member
Registered
Joined
Jul 11, 2016
Messages
24
Points
3
When you read this post, the individual steps were tested on a VPS (Virtual Private Server) with CentOS 7 installed on the system. This guide will work on any standard Debian OS installation.

Now can we start?

Nginx is sometimes called poetic as the "unsinkable web server". Thanks to the ThreadPool, it can handle many TCP connections (also very slow as those of smartphones) at the same time with low system load.
MySQL is a powerful RDBMS (Relational Database Management System) and is available free of charge. It is the basis of prominent web applications such as WordPress or Joomla.

1. Preparations

a) Firewall: Centos has the firewall for IPv4 but not enabled by default for IPv6.
For the Centos OS, it has prepared shell scripts for you in /root. It has created and stored the firewall tables for IPV4 and IPV6. You can check these by the following commands:

Code:
# Check  IPV4
iptables -n -L -v
# Check IPV6 
ip6tables -n -L -v
b) Update your system:

Code:
yum update
c) Apache is already on port 80. You can uninstall Apache as follows if its existed:

Code:
yum remove httpd
Or Apache is reconfigured to a different port so that nginx can listen its services on port 80. Edit the file /etc/httpd/conf/httpd.conf and change the line with "listen" as follows:

Code:
listen 8000;
Restart Apache:

Code:
service httpd restart
2. Install MySQL Server

Centos has the MariaDB in the Repos. This is a replacement and more faster than the MySQL server. The installation is quite simple with:

Code:
yum install mariadb-server mariadb
Enable and restart mariadb
Code:
systemctl enable mariadb
systemctl start mariadb
Back up your SQL instance. The DB root password has not been set yet, enter the blank password with "Enter" and set a new SQL root password. All other questions can be answered with "Yes".

Code:
/usr/bin/mysql_secure_installation
3. Install NGINX

Option 1: Download the yum configuration file using the PGP public key. You will get a warning that this rpm is not signed. However, the signature of the Nginx packages is checked during the installation.

Code:
wget nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -i nginx-release-centos-7-0.el7.ngx.noarch.rpm
Option 2: You edit the new file /etc/yum.repos.d/nginx.repo with the following contents as of the nginx indicated (the signature of Nginx packages will not be checked during installation):

Code:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
Then install Nginx with:

Code:
yum install nginx
Now Port 80 is no longer occupied and we can start the Nginx server:

Code:
systemctl enable nginx
service nginx restart
Test: You can view the address of your server on a browser, you can visit http: //Your_IP_address (or make sure your DNS settings are correct and use your-domain.com instead of the IP address) and you are welcomed with the following message:

Code:
Welcome to nginx!
for cooperating with ngninx php5-fpm to configure:

Edit the file /etc/nginx/conf.d/default.conf
Code:
# You will find the line 
server_name localhost;

# And change to:
server_name www.example.com;


# Change the block:
location / {
root   /usr/share/nginx/html;
index  index.html index.htm;
}

# as follows:
index  index.html index.htm index.php;
root   /usr/share/nginx/html;
location / {
try_files $uri $uri/ =404;
}


# Edit the block for php processing as follows:
location ~ .php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Nginx must be restarted:

Code:
service nginx restart
4. PHP

For php5-fpm very little packets must be specified, the rest is controlled by dependencies:

Code:
yum install php php-mysql php-fpm
Configuration: cgi.fix_pathinfo default set is 1. This may cause a security breach. For help, see the comments in the configuration file php.ini . Edit the file /etc/php.ini and find the line with cgi.fix_pathinfo and change them as follows:

Code:
cgi.fix_pathinfo=0
php5-fpm should already running with the fast UNIXSocket. Edit the file /etc/php-fpm.d/www.conf as follows:

Code:
# listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
php5-fpm only needs to be started:

Code:
systemctl enable php-fpm.service
systemctl start php-fpm
Apache-Server needs to be started in order for the new configuration file of phpMyAdmin is activated.

Code:
systemctl restart httpd
PHP Info Page
Create and edit a new file: nano /usr/share/nginx/html/info.php with the following content:

Code:
<?php phpinfo(); ?>
Check with a browser

Code:
http://your-domain.com/info.php
The output of info.php should look as follows (The PHP version depends on the installation time):

lemp-stack-centos-7.png

5. Install and configure PHPMyAdmin

It is easy to manage your database server with a graphical interface. However, one must not neglect the security. In the following, PHPMyAdmin is installed so that you can only access localhost and your database is protected against attacks from the Internet. For an occasional access to your database, building an SSH tunnel is much more comfortable than handling SQL commands.
First we perform the installation with a standard configuration for apache2 so that PHPMyAdmin can create the necessary MySQL tables for itself.

Code:
yum install epel-release
yum install phpmyadmin
The installation is now completed. The installation contains an Apache configuration file, which is already arranged correctly. PHPMyAdmin is now only accessible locally, which we also intend. Configuration details you can check in /etc/httpd/conf.d/phpMyAdmin.conf.
If you have decided to uninstall Apache, you will need an nginx configuration file. Edit the new file /etc/nginx/conf.d/phpmyadmin.conf with the following contents (thereafter must nginx restart, see above):

Code:
server {
listen       localhost:8000;
server_name  localhost;

index  index.html index.htm index.php;
root   /usr/share/phpMyAdmin;
location / {
   try_files $uri $uri/ =404;
}


location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}
}
For access, we still need the SSH tunnel:
Code:
# Create a new user
useradd -m test
passwd test

# If you are running Windows, you can use putty for tunnel setup, for example.

# SSH tunnel (replace www.example.com with the correct hostname or IP address)
ssh -fCN [email protected]  -L 8000:localhost:8000


# The option of the above command:
# -f: ssh is started as a background process
# -N: no remote command is executed 
# -C: Data compression is turned on
#  Localhost: 8000: the remote TCP port 8000 is connected to localhost: 8000.
URL of your phpMyAdmin with Apache server:

Code:
http://www.example.com:8000/phpmyadmin/
URL of your PHPMyAdmin with nginx server:
Code:
http://www.example.com:8000/
Your server is now ready for running and you can start upload websites to it to test.

Good luck!
 

VirtuBox

Well-known member
Registered
Joined
May 3, 2016
Messages
1,622
Points
83
If you want to install a LEMP stack easily, without having to configure everything yourself, I recommend you to begin with easyengine.
It's only compatible with Debian/Ubuntu, but you can setup a perfect LEMP stack with Nginx / php5.6 + php7 / Mariadb / Memcached / Redis / phpMyAdmin just by running two command :

Code:
[B]## install easyengine[/B]
wget -qO ee rt.cx/ee && bash ee
[B]
## install your LEMP Stack [/B]
ee stack install --web

[B]## create your first website[/B]
ee site create yourdomain.com --**options-you-want** 

options available :
--html : simple html website
--php : simple php site
--mysql : mysql + php website
--php7 : to add with mysql to use php7
--letsencrypt : get a free ssl certificate with letsencrypt
--wp : wordpress website
--wpredis : wordpress + redis-server cache
--wpsc : wordpress + wp super cache
--w3tc : wordpress + w3 total cache
 

wpspeedster

Well-known member
Registered
Joined
May 6, 2016
Messages
120
Points
18
Great tutorials, I will try to install on my new VPS.

I like Virtubox's way because it's short and easy to apply and will give it a go.
 
Older Threads
Newer Threads
Replies
8
Views
4,791
Replies
4
Views
2,204
Replies
36
Views
13,067
Recommended Threads
Replies
2
Views
1,983
Replies
9
Views
3,728
Replies
6
Views
893
Replies
3
Views
2,739

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top