The LEMP Stack is a group of software that can be used to serve dynamic web pages and web applications. This tutorial will walk you through the steps to install Nginx, MariaDB and PHP on a Debian 10 machine.
Prerequisites
You will need one Debian 10 (physical or virtual) machine configured with a non-root user having sudo privileges.Installing Nginx
If this is the first time you'll be using apt on Debian 10, you should start off by updating your local package index and then install the nginx server:On Debian 10, Nginx is configured to start running upon installation. If you have the ufw firewall running, you will need to allow connections to Nginx. Since you haven't configured SSL for your server yet, for now you only need to allow HTTP traffic on port 80.
Output
Installing MariaDB
You need to install the database system to be able to store and manage data for your site.We recommend that you should run a security script comes pre-installed with MariaDB. This script will remove some insecure default settings and lock down anonymous access to your database system.
The first prompt will ask you to enter the current database root password. Because you just installed MariaDB and haven’t made any configuration changes yet, this password will be blank, so just press ENTER at the prompt.
The next prompt asks you whether you'd like to set up a database root password. Because MariaDB uses a special authentication method for the root user that is typically safer than using a password, you don't need to set this now. Type N and then press ENTER.
Rest, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove anonymous users and the test database, disable remote root login, and load these new rules so that MariaDB immediately respects the changes you have made.
Now log in to the MariaDB console by typing:
You can test if the new user has the proper permissions by logging in to the MariaDB console again, this time using the custom user credentials:
This will give you the following output:
Installing PHP
We need to install php-fpm, which stands for "PHP fastCGI process manager", and tell Nginx to pass PHP requests to this software for processing. Additionally, you'll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. Core PHP packages will automatically be installed as dependencies.Configuring Nginx
Nginx has one server block enabled by default and is configured to serve documents out of a directory at /var/www/html. While this works well for a single site, it can become difficult to manage if you are hosting multiple sites. Instead of modifying /var/www/html, let's create a directory structure within /var/www for the website, leaving /var/www/html in place as the default directory to be served if a client request doesn't match any other sites.
Create the root web directory as follows:
Save and close.
Activate your configuration by linking to the config file from Nginx's sites-enabled directory:
Test PHP Configuration
You can test Nginx and PHP functionality by creating a test PHP file in your document root. Open a new file called info.php within your document root in your favorite text editor:Save and close the file by typing CTRL+X and then y and ENTER to confirm.
You can now access this info.php in your web browser by visiting the domain name or IP address you've set up in your Nginx configuration file like below:
http://techsupportpk.com/info.php or http://172.20.10.8/info.php
You will see a web page containing detailed information about your Debian 10 server:
Once tested your PHP and Nginx functionality through info.php file, it's best to remove the file as it contains sensitive information about your PHP environment and your Debian server.
Testing Database
Now we will test if PHP is able to connect to MariaDB and execute database queries, you can create a test table with dummy data and query for its contents from a PHP script.You will see the following output:
After confirming that you have valid data in your test table, you can exit the MariaDB console:
Now you can create the PHP script that will connect to MariaDB and query for your content. Create a new PHP file in your custom web root directory using your preferred editor.
Add the following content to your PHP script:
Save and close the file when you're done editing.
You can now access this page in your web browser by visiting the domain name or IP address followed by /list.php like below:
http://techsupportpk.com/list.php or http://172.20.10.8/list.php
You should see a page like this, showing the content you've inserted in your test table:
That means your PHP environment is ready to connect and interact with your MariaDB server.
Securing Nginx with SSL
For this guide, we will generate a self signed certificate using openssl.add or update the following parameters and replace highlighted text to reflect yours:
Save and close.
Now run the following command to generate a self signed certificate:
add the following ssl parameters in your configuration file. Replace highlighted text to reflect yours.
Save and close.
Update firewall settings to allow SSL traffic then verify nginx configuration and restart nginx service to take changes into effect.
You can now access this page with https in your web browser by visiting the domain name or IP address followed by /list.php like below:
https://techsupportpk.com/list.php or https://172.20.10.8/list.php
No comments: