How to Install LEMP on CentOS 7.x (NGINX, PHP 7.x, SQL MariaDB 10.x)
1. Update system
$ sudo yum update
2. Install EPEL repository
$ sudo yum install epel-release -y
Install REMI repository
$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
3. Verify EPEL repository
$ sudo yum repolist
4. Install Nginx webserver
$ sudo yum install nginx -y
5. After installation nginx completes, enable nginx start on boot and run it.
$ sudo systemctl enable nginx
$ sudo systemctl start nginx
6. Configure firewall to allow external access to port 80 (http) and port 443 (https)
$ sudo firewall-cmd --permanent --zone=public --add-service=http
$ sudo firewall-cmd --permanent --zone=public --add-service=https
7. Reload firewall
$ sudo firewall-cmd --reload
8. Check connection
9. Create MariaDB repo
$ sudo vi /etc/yum.repos.d/MariaDB.repo
Before add yum repository make sure 10.1 is the latest version of MariaDB
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
10. Install MariaDB
$ sudo yum --enablerepo=remi install mariadb-server mariadb -y
11. After installation MariaDB completes, enable MariaDB start on boot and run it.
$ sudo systemctl enable mariadb.service
$ sudo systemctl start mariadb.service
12.Set passwords for the MySQL (MariaDB) root account:
$ sudo mysql_secure_installation
13. PHP 7.1 installationEnable php71 repository which is disabled by default:
$ sudo yum install yum-utils -y
$ sudo yum-config-manager --enable remi-php71
14. Install PHP package
$ sudo yum --enablerepo=remi,remi-php71 install php-fpm php-common
and some common module
$ sudo yum --enablerepo=remi,remi-php71 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
enable and start php-fpm
$ sudo systemctl enable php-fpm
$ sudo systemctl start php-fpm
Edit php.ini file
$ sudo vi /etc/php.ini
Open PHP-FPM configuration
$ sudo vi /etc/php-fpm.d/www.conf
Find and replace these lines:
user = apache to user = nginx
group = apache to group = nginx
listen.owner = nobody to listen.owner = nginx
listen.group = nobody to listen.group = nginx
And, lastly, under ;listen = 127.0.0.1:9000 add this line:
listen = /var/run/php-fpm/php-fpm.sock
Restart php-fpm
$ sduo systemctl restart php-fpm
Test php
$ sudo vi /var/www/html/info.php
<?php
phpinfo();
phpinfo(INFO_MODULES);
?>
Configure Nginx
Open /etc/nginx/nginx.conf
$ sudo vi /etc/nginx/nginx.conf
Make sure the following lines is in the Server context
Create a new default file in /etc/nginx/conf.d/default.conf
$ sudo vi /etc/nginx/conf.d/default.conf
Paste the following code in to this file
server {
listen 80;
server_name your_server_ip; # note that these lines are originally from the "location /" block
root /var/www/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 /var/www/html;
}
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;
}
}
Restart nginx
$ sudo systemctl restart nginx
Test on browser
Install PhpMyAdmin
$ sudo yum --enablerepo=remi install phpMyAdmin
Change the authentication in phpMyAdmin config.inc.php from cookie to http:
$ sudo vi /etc/phpMyAdmin/config.inc.php
$ sudo ln -s /usr/share/phpMyAdmin /usr/share/nginx/html
$ sudo systemctl restart php-fpm
Test
http://server_domain_or_IP/phpMyAdmin/index.php
Chmod php session if you getting 403 error:
$ sudo chmod 777 /var/lib/php/session/
15. Create file test Php
- Create file
$ sudo vi /var/www/html/info.php
xx. Edit php.ini file
18. Open PHP-FPM configuration $ sudo vi /etc/php-fpm.d/www.conf
. Find and replace these lines:
xx. Restart php-fpm
`$ sudo systemctl restart php-fpm'
16. Configuring Nginx to work with PHP 7, create a new Nginx configuration file by running vim or nano text editor:
$ sudo vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name your_server_ip; # note that these lines are originally from the "location /" block
root /var/www/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 /var/www/html;
}
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;
}
}
17. Restart Nginx
$ sudo systemctl restart nginx
19. Enable and restart php-fpm
$ sudo systemctl start php-fpm.service
$ sudo systemctl enable php-fpm.service