Jump to content

LEMP: Difference between revisions

From Wiki
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Update Software Packages==
==Update Software Packages==
sudo apt update


sudo apt upgrade
<syntaxhighlight lang="bash">
sudo apt update
</syntaxhighlight>
 
<syntaxhighlight lang="bash">
sudo apt upgrade
</syntaxhighlight>


==Install Nginx Web Server==
==Install Nginx Web Server==
sudo apt install nginx
 
<syntaxhighlight lang="bash">
sudo apt install nginx
</syntaxhighlight>


After it’s installed, we can enable Nginx to auto-start at boot time by running the following command.
After it’s installed, we can enable Nginx to auto-start at boot time by running the following command.
sudo systemctl enable nginx
 
<syntaxhighlight lang="bash">
sudo systemctl enable nginx
</syntaxhighlight>


Then start Nginx with this command:
Then start Nginx with this command:
sudo systemctl start nginx
 
<syntaxhighlight lang="bash">
sudo systemctl start nginx
</syntaxhighlight>


Now check out its status.
Now check out its status.
sudo systemctl status nginx
 
<syntaxhighlight lang="bash">
sudo systemctl status nginx
</syntaxhighlight>


Check Nginx version.
Check Nginx version.
nginx -v
 
<syntaxhighlight lang="bash">
nginx -v
</syntaxhighlight>


If you are using UFW firewall, then run this command to open TCP port 80.
If you are using UFW firewall, then run this command to open TCP port 80.
sudo ufw allow http
 
<syntaxhighlight lang="bash">
sudo ufw allow http
</syntaxhighlight>


Finally, we need to make www-data (Nginx user) as the owner of web directory. By default, it’s owned by the root user.
Finally, we need to make www-data (Nginx user) as the owner of web directory. By default, it’s owned by the root user.
sudo chown www-data:www-data /usr/share/nginx/html -R
 
<syntaxhighlight lang="bash">
sudo chown www-data:www-data /usr/share/nginx/html -R
</syntaxhighlight>


==Install MariaDB Database Server==
==Install MariaDB Database Server==
sudo apt install mariadb-server mariadb-client
 
<syntaxhighlight lang="bash">
sudo apt install mariadb-server mariadb-client
</syntaxhighlight>


After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.
After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.
systemctl status mariadb
 
<syntaxhighlight lang="bash">
systemctl status mariadb
</syntaxhighlight>


If it’s not running, start it with this command:
If it’s not running, start it with this command:
sudo systemctl start mariadb
 
<syntaxhighlight lang="bash">
sudo systemctl start mariadb
</syntaxhighlight>


To enable MariaDB to automatically start at boot time, run
To enable MariaDB to automatically start at boot time, run
sudo systemctl enable mariadb
 
<syntaxhighlight lang="bash">
sudo systemctl enable mariadb
</syntaxhighlight>


Now run the post installation security script.
Now run the post installation security script.
sudo mysql_secure_installation
 
<syntaxhighlight lang="bash">
sudo mysql_secure_installation
</syntaxhighlight>


So you can run the following command to login without providing MariaDB root password.
So you can run the following command to login without providing MariaDB root password.
sudo mariadb -u root
 
<syntaxhighlight lang="bash">
sudo mariadb -u root
</syntaxhighlight>


To exit, run
To exit, run
exit;
 
<syntaxhighlight lang="bash">
exit;
</syntaxhighlight>


Check MariaDB server version information.
Check MariaDB server version information.
mariadb --version
 
<syntaxhighlight lang="bash">
mariadb --version
</syntaxhighlight>


==Install PHP7.4==
==Install PHP7.4==
sudo apt install php7.4 php7.4-fpm php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl
<syntaxhighlight lang="bash">
sudo apt install php7.4 php7.4-fpm php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl
</syntaxhighlight>


Installing these PHP extensions ensures that your CMS runs smoothly. Now start php7.4-fpm.
Installing these PHP extensions ensures that your CMS runs smoothly. Now start php7.4-fpm.
sudo systemctl start php7.4-fpm
 
<syntaxhighlight lang="bash">
sudo systemctl start php7.4-fpm
</syntaxhighlight>


Enable auto-start at boot time.
Enable auto-start at boot time.
sudo systemctl enable php7.4-fpm
 
<syntaxhighlight lang="bash">
sudo systemctl enable php7.4-fpm
</syntaxhighlight>


Check status:
Check status:
systemctl status php7.4-fpm
 
<syntaxhighlight lang="bash">
systemctl status php7.4-fpm
</syntaxhighlight>


==Create an Nginx Server Block==
==Create an Nginx Server Block==
An Nginx server block is like a virtual host in Apache. We will not use the default server block because it’s inadequate to run PHP code and if we modify it, it becomes a mess. So remove the default symlink in sites-enabled directory by running the following command. (It’s still available as <code>/etc/nginx/sites-available/default</code>.)
An Nginx server block is like a virtual host in Apache. We will not use the default server block because it’s inadequate to run PHP code and if we modify it, it becomes a mess. So remove the default symlink in sites-enabled directory by running the following command. (It’s still available as <code>/etc/nginx/sites-available/default</code>.)
sudo rm /etc/nginx/sites-enabled/default
 
<syntaxhighlight lang="bash">
sudo rm /etc/nginx/sites-enabled/default
</syntaxhighlight>


Then use a command-line text editor like Nano to create a brand new server block file under /etc/nginx/conf.d/ directory.  
Then use a command-line text editor like Nano to create a brand new server block file under /etc/nginx/conf.d/ directory.  
sudo nano /etc/nginx/conf.d/default.conf
 
<syntaxhighlight lang="bash">
sudo nano /etc/nginx/conf.d/default.conf
</syntaxhighlight>


Paste the following text into the file. The following snippet will make Nginx listen on IPv4 port 80 and IPv6 port 80 with a catch-all server name.
Paste the following text into the file. The following snippet will make Nginx listen on IPv4 port 80 and IPv6 port 80 with a catch-all server name.


server {
<syntaxhighlight lang="bash">
server {
   listen 80;
   listen 80;
   listen [::]:80;
   listen [::]:80;
Line 102: Line 170:
   }
   }
  }
  }
</syntaxhighlight>


Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.)
Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.)


Then test Nginx configurations.
Then test Nginx configurations.
sudo nginx -t
 
<syntaxhighlight lang="bash">
sudo nginx -t
</syntaxhighlight>


If the test is successful, reload Nginx.
If the test is successful, reload Nginx.
sudo systemctl reload nginx
 
<syntaxhighlight lang="bash">
sudo systemctl reload nginx
</syntaxhighlight>


==Source==
==Source==
Line 117: Line 192:
[[Category:Server]]
[[Category:Server]]
[[Category:Web Server]]
[[Category:Web Server]]
[[Category:Nginx]]

Latest revision as of 17:11, 13 November 2022

Update Software Packages

sudo apt update
sudo apt upgrade

Install Nginx Web Server

sudo apt install nginx

After it’s installed, we can enable Nginx to auto-start at boot time by running the following command.

sudo systemctl enable nginx

Then start Nginx with this command:

sudo systemctl start nginx

Now check out its status.

sudo systemctl status nginx

Check Nginx version.

nginx -v

If you are using UFW firewall, then run this command to open TCP port 80.

sudo ufw allow http

Finally, we need to make www-data (Nginx user) as the owner of web directory. By default, it’s owned by the root user.

sudo chown www-data:www-data /usr/share/nginx/html -R

Install MariaDB Database Server

sudo apt install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.

systemctl status mariadb

If it’s not running, start it with this command:

sudo systemctl start mariadb

To enable MariaDB to automatically start at boot time, run

sudo systemctl enable mariadb

Now run the post installation security script.

sudo mysql_secure_installation

So you can run the following command to login without providing MariaDB root password.

sudo mariadb -u root

To exit, run

exit;

Check MariaDB server version information.

mariadb --version

Install PHP7.4

sudo apt install php7.4 php7.4-fpm php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl

Installing these PHP extensions ensures that your CMS runs smoothly. Now start php7.4-fpm.

sudo systemctl start php7.4-fpm

Enable auto-start at boot time.

sudo systemctl enable php7.4-fpm

Check status:

systemctl status php7.4-fpm

Create an Nginx Server Block

An Nginx server block is like a virtual host in Apache. We will not use the default server block because it’s inadequate to run PHP code and if we modify it, it becomes a mess. So remove the default symlink in sites-enabled directory by running the following command. (It’s still available as /etc/nginx/sites-available/default.)

sudo rm /etc/nginx/sites-enabled/default

Then use a command-line text editor like Nano to create a brand new server block file under /etc/nginx/conf.d/ directory.

sudo nano /etc/nginx/conf.d/default.conf

Paste the following text into the file. The following snippet will make Nginx listen on IPv4 port 80 and IPv6 port 80 with a catch-all server name.

server {
   listen 80;
   listen [::]:80;
   server_name _;
   root /usr/share/nginx/html/;
   index index.php index.html index.htm index.nginx-debian.html;
 
   location / {
     try_files $uri $uri/ /index.php;
   }
 
   location ~ \.php$ {
     fastcgi_pass unix:/run/php/php7.4-fpm.sock;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
     include snippets/fastcgi-php.conf;
   }
 
  # A long browser cache lifetime can speed up repeat visits to your page
   location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
        access_log        off;
        log_not_found     off;
        expires           360d;
   }
 
   # disable access to hidden files
   location ~ /\.ht {
       access_log off;
       log_not_found off;
       deny all;
   }
 }

Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.)

Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx.

sudo systemctl reload nginx

Source