First I will explain what LEMP and CentOS are for anyone who does not know. According to lemp.io:
"LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Because of its modular nature, the components can be easily swapped. With LEMP, Apache is replaced by the lightweight and powerful Nginx."
It is also possible to use Apache and Nginx on the same server, but that is a topic for another day.
To begin, you need access to a VPS so you can run the commands in this tutorial. In the commands below I will show the installation using PHP 7.3.6, which was the current version at the time.
First, install Nginx. Start by installing the repository where it is available.
yum install epel-release -y
Now install Nginx:
yum install nginx -y
Start it and enable it as a service so it starts with the VPS:
systemctl start nginx systemctl enable nginx
Now install MariaDB, a MySQL variant used on CentOS, then start and enable it just like Nginx:
yum install mariadb-server mariadb -y systemctl start mariadb systemctl enable mariadb
To finish MariaDB setup, run the command below to configure the root password and remove default data. This is recommended.
mysql_secure_installation
Now for PHP. This part will likely change over time because new versions are released, so some commands below may need to be adjusted, especially the PHP version number. First install the repository with the latest PHP versions.
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
By default, the PHP 5.4 repository is enabled. To install the newest PHP version, disable that repository. Repositories can be enabled or disabled with yum-config-manager, provided by yum-utils.
yum install -y yum-utils yum-config-manager --disable remi-php54
Now enable the PHP 7.3 repository:
yum-config-manager --enable remi-php73
Finally, install PHP:
yum install php php-fpm php-common
To confirm which version was installed, run:
php -v
To finish the LEMP configuration, configure Nginx to work with PHP 7 by creating a new configuration file.
nano /etc/nginx/conf.d/default.conf
Then add the syntax below. Replace IPDAVPS with your VPS IP.
server { listen 80; server_name IPDAVPS;
# note that these lines are originally from the "location /" block
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$ {
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;
}
}
Press CTRL + X to save, then restart Nginx so it reads the new configuration file.
systemctl restart nginx
Now configure PHP-FPM.
nano /etc/php-fpm.d/www.conf
Find and replace these lines:
Where it says "user = apache", replace it with "user = nginx".
Replace "group = apache" with "group = nginx".
"listen.owner = nobody" should become "listen.owner = nginx".
And "listen.group = nobody" should become "listen.group = nginx".
Below ";listen = 127.0.0.1:9000", add:
listen = /var/run/php-fpm/php-fpm.sock
Finally, press CTRL + X to save and restart PHP-FPM.
systemctl start php-fpm.service systemctl enable php-fpm.service
If you did everything correctly, it is time to test it. On the server, go to Nginx's default directory (/usr/share/nginx/html) and create a test.php file.
nano test.php
Add this content:
Now open this address in the browser: http://IPDAVPS/test.php. If everything worked, a blank page with the word "test" should appear. Otherwise, something went wrong and you should review the tutorial.
Did you like this tutorial and want to configure your own server? Use this Digital Ocean discount link: https://m.do.co/c/1e25c79708c7. When you configure your server through it, you get US$100 to test for 60 days. After that, you will be charged for the servers you configure.
Comments
Join the conversation
Loading comments...