Menu

Install LEMP Stack on Debian 9

Today we are going to guide you install LEMP stack to brand new Debian GNU/Linux 9 Stretch. LEMP stands for Linux, Nginx (as in Engine X), MySQL and PHP. From Debian 9, Mariadb is the default mysql server. But you can also choose mysql for installation. It is the counterpart of more popular LAMP stack except instead of Apache httpd we use nginx. Earlier we have tried LEMP stack on Ubuntu and docker container. 

Installing Nginx, Mariadb and PHP

Installation of nginx, MariaDB and PHP by bellow command line from bash terminal 
apt install nginx mysql-server mysql-client php-fpm php-mysql libfcgi0ldbl
Now start all needed services with systemd and enable them to start at boot
# systemctl start php7.0-fpm nginx mysql
# systemctl enable php7.0-fpm nginx mysql
Configuring LEMP Stack

Now Configure the mysql through bellow command line and use "y" for every question and set the mysql root password. 
mysql_secure_installation
Now you need to bind socket for fastcgi through bellow command line 
cgi-fcgi -bind -connect /run/php/php7.0-fpm.sock
Now we are going to configure sites in nginx configuration folder. First we move default config as backup for security purpose
mv /etc/nginx/sites-available/default /etc/nginx/sites-available/df.bk
After that we will make our own configuration file by bellow command line
nano /etc/nginx/sites-available/default
After that paste the bellow lines on the "default" empty file 
 server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }
}

Now make a index.php file in /var/www/html/ by bellow command line
echo "<?php phpinfo(); ?>" > /var/www/html/index.php
Now check the mysql is working properly or not. First create a database user and password also a database on the mysql by follow command line 
mysql -u root -e "CREATE USER 'hackthesec'@'%' IDENTIFIED BY 'test@123';"

mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'hackthesec'@'%' WITH GRANT OPTION"
To check the mysql is working or not use the following command
nano /var/www/html/db.php
<?php
$dbh = mysqli_connect('localhost', 'hackthesec', 'test@123');
if (!$dbh) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($dbh);
?>

Now reboot the system or restart the services ..
systemctl restart php7.0-fpm nginx mysql
Now open your web browser and hit your web server ip you will see the bellow test page.
Allow lamp on firewall through bellow commands 
ufw allow 3306/tcp
ufw allow 80/tcp
ufw allow 443/tcp (allow if you use https)

@Hackthesec

About Author:


I am a Linux Administrator and Security Expert with this site i can help lot's of people about linux knowladge and as per security expert i also intersted about hacking related news.TwitterFacebook

Next
This is the most recent post.
Previous
Older Post

0 comments:

Post a Comment

 
Top