Initial Server Setup with LEMP
The Basics
When you first begin to access your KaOS install for use as a server, there are a few early steps you should take to make it more secure. Some of the first tasks can include configuring SSH.
The LEMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Nginx (say “Engine-X”!), MySQL, and PHP.
Why use Nginx?
Nginx is a powerful app to provide a web server as same as Apache. It can also be used as a reverse proxy for many protocols (http(s) of course but also imap, udp, tcp, smtp, etc.). It has very good performances for serving static files or dynamics files (to serve php files it doesn’t use php directly but php-fpm (php through the FastCGI protocol)).
The architecture and the config’s syntax of Nginx and Apache are a bit different. For Nginx the entire configuration is server-side: it doesn’t use .htaccess files. That means that the settings are completely and statically loaded at Nginx’s startup. Only the server’s owner can modify the configuration and not the website’s owner. Syntax is different, too. For example the URL rewriting instructions for Apache (often wrotten in a .htaccess) could looks like this (Drupal example):
The similar instruction for Nginx is:
to put in a Location block.
Using Nginx or Apache is a personnal choice. If you’re the owner of the server and need very good performances, Nginx is probably a better choice. But if your website is on a shared server, it mostly uses Apache because you haven’t the right to configure the server and you need to use .htaccess files to configure specific rules for your website.
Configure SSH
Read the Configure SSH part of the LAMP documentation. It is exactly the same thing.
Installing LEMP
Install Nginx
As always, make sure the system is fully up to date before installing any:
Once the update is complete, we can install Nginx:
After Nginx is installed, we need to make some changes in the configuration.
Open up the nginx configuration file:
Notice: the original configuration is saved to /etc/nginx/conf/nginx.default
. Never remove or modify this file! If you completely broke your nginx configuration you could have to copy the nginx.conf.default
to restore the initial settings.
Modify the worker_processes
value as this:
It permits to Nginx to use all CPU cores if necessary.
Now, let’s modify the how the activities are registred to the logs. By default only the access.log
is set and it is not very readable. If there’s an error, we would know why. To do that, we’ll activate the logs for the errors. Uncomment:
Replace it by:
Now we’ll make a human-readable access.log. Uncomment:
Replace it by:
Now it’s time to launch nginx:
Directing your browser to your server’s IP address (http://12.34.56.789) will display the default welcome page of Nginx. To check if your server work, we will modify the /usr/share/nginx/html/index.html
file:
Replace all by:
You can visit the placeholder page by going to your server’s IP address in your browser.
How to Find your Server’s IP address
You can run the following command to reveal your server’s IP address.
Install MariaDB
Read the Install MariaDB part of the LAMP documentation. It is exactly the same thing.
Install PHP
PHP is an open source web scripting language that is widely used to build dynamic webpages.
To install PHP, open terminal and type in this command.
To use PHP, Nginx will need to use the php-fpm service. Let’s start it:
The configuration of the php-fpm service is on the file /etc/php/php-fpm.d/www.conf
. We won’t modify it but Nginx needs to know which is the socket address of php-fpm service. It is the value of the listen
variable:
You should see something like:
Here, the address of the socket is /run/php-fpm/php-fpm.sock
that nginx will need to use to provide php files. Time to modify the nginx configuration:
We’ll need to add a specific Location block for php files inside the server block. Find the following line (you can use CTRL+F):
Just after this block (ie. after the next closing bracket }
following this line), add this:
Now you should be done configuring /etc/nginx/conf/nginx.conf
so save this file and restart Nginx:
See your PHP Config and Add Programs to Startup
After LEMP is completely installed, we can take a look and see the PHP details by creating a quick php info page
To set this up, first create a new blank file:
Add in the following line:
Then Save and Exit.
Visit your php info page (make sure you replace the example ip address with your correct one): http://12.34.56.789/info.php
To close out the LEMP installation, add the Nginx server, php-fpm and MySQL daemons to the list of programs that run upon server startup:
Congratulations, you have now installed the LEMP server on KaOS!
More Comprehensive Settings
This guide is here is here to explain how to get started with LEMP and once the system has gone through the “learning” it is time to use the very complete official documentation, mostly for Nginx: