NEW LOCATION

Olá Brazil Brazil NVMe-powered VPS Plans in Brazil Now Available.

Deploy Instantly
  • +357 2425 0808
  • Login
  • English

Home

Blog

How to Install Nginx, MySQL, PHP on Ubun...

How to Install Nginx, MySQL, PHP on Ubuntu 22.04 (LEMP)

How to Install Nginx, MySQL, PHP on Ubuntu 22.04 (LEMP)

NetShop ISP

NetShop ISP · Blog Author

May 10, 2024 · Technical Tutorials

The LEMP stack is a popular web development stack consisting of four key components: Linux, Nginx, MySQL and PHP, each of which play a crucial role in web application development and hosting. In this article we will provide a step-by-step guide to installing LEMP on your Ubuntu 22.04 Server.

Prerequisites

  • Server with Ubuntu 22.04 OS installed
  • SSH Access with root or sudo-privileged user

Steps to Install LEMP on Ubuntu Server

Step 1. Switch to Root user

First, switch to the root user using the following command. Unless stated otherwise, all subsequent commands must be executed as the root user.

john@localhost:~$ sudo –i

Step 2. Install Nginx

Nginx is a powerful web server software designed to handle a high volume of simultaneous requests. It offers faster loading times and better performance than most of other webservers.

root@localhost:~$ apt update -y

Once updates are installed run the following command:

root@localhost:~$ apt install nginx -y

Step 3. Start Nginx and Enable Autostart

Start nginx using the following command:

root@localhost:~$ systemctl start nginx

Now, lets make Nginx to automatically start on boot.

root@localhost:~$ systemctl enable --now nginx

If everything is okay, Nginx should be running. Check this by executing the following command:

root@localhost:~$ systemctl status nginx

Sample Output:

● nginx.service - A high performance web server and a reverse proxy server
      Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2023-09-10 16:27:18 UTC; 23s ago
       Docs: man:nginx(8)
   Main PID: 2079 (nginx)
      Tasks: 3 (limit: 1588)
     Memory: 5.2M
        CPU: 30ms
     CGroup: /system.slice/nginx.service
             ├─2079 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─2082 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─2083 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Sep 10 16:27:18 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 10 16:27:18 ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.

Step 5. Install MariaDB Server

MariaDB Server is one of the most popular open source relational databases. It’s made by the original developers of MySQL and guaranteed to stay open source. It is part of most cloud offerings and the default in most Linux distributions.

root@localhost:~$ apt install mariadb-server -y

Step 6. Start MariaDB and Enable Autostart

Similar to the procedure we followed with Nginx, we need to ensure it will automatically start on server boot.

root@localhost:~$ systemctl enable –now mariadb

Now, check the status of MariaDB to ensure it’s running properly:

root@localhost:~$ systemctl status mariadb

The previous command should output something that resembles the following.

Sample Output:

● mariadb.service - MariaDB 10.6.12 database server
  Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
  Active: active (running) since Sun 2023-09-10 16:29:46 UTC; 18s ago
  Docs: man:mariadbd(8)
  Main PID: 3405 (mariadbd)
  Status: "Taking your SQL requests now..."
  Tasks: 14 (limit: 1588)
  Memory: 62.6M
     CPU: 268ms
Sep 10 16:29:46 ubuntu mariadbd[3405]: Version: '10.6.12-MariaDB-0ubuntu0.22.04.1'  socket: '/run/mysqld/mysqld.sock'  port: 3306  Ubunt>
Sep 10 16:29:46 ubuntu systemd[1]: Started MariaDB 10.6.12 database server.
Sep 10 16:29:46 ubuntu /etc/mysql/debian-start[3422]: Upgrading MySQL tables if necessary.
Sep 10 16:29:46 ubuntu /etc/mysql/debian-start[3425]: Looking for 'mariadb' as: /usr/bin/mariadb
Sep 10 16:29:46 ubuntu /etc/mysql/debian-start[3425]: Looking for 'mariadb-check' as: /usr/bin/mariadb-check
Sep 10 16:29:46 ubuntu /etc/mysql/debian-start[3425]: This installation of MariaDB is already upgraded to 10.6.12-MariaDB.
Sep 10 16:29:46 ubuntu /etc/mysql/debian-start[3425]: There is no need to run mysql_upgrade again for 10.6.12-MariaDB.
Sep 10 16:29:46 ubuntu /etc/mysql/debian-start[3425]: You can use --force if you still want to run mysql_upgrade
Sep 10 16:29:46 ubuntu /etc/mysql/debian-start[3433]: Checking for insecure root accounts.
Sep 10 16:29:46 ubuntu /etc/mysql/debian-start[3437]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tabl>

Step 8. Finalize & Secure MariaDB Installation

When installing MariaDB, it is recommended that you run the mysql_secure_installation script.

The script can increase the security of the MariaDB server depending on the answers given to a few yes/no questions. The exact answers to the questions will vary depending on your exact needs.

For the purposes of this tutorial, we show you the script’s output alongside our answers to the questions. Please note that if this is your first time running the script, for the first question (current password for root), you just need to press Enter. The rest of the answers are shown in bold.

root@localhost:~$ mysql_secure_installation

Sample Output (letters in bold are user inputs upon prompt):

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Step 9. Next Install PHP FastCGI Process Manager (PHP-FPM)

root@localhost:~$ apt install php-fpm

Step 10. Enable PHP-FPM to Autostart

Ensure that PHP FastCGI Process Manager is automatically started on server boot by executing the following command:

root@localhost:~$ systemctl enable --now php8.1-fpm

Now, check the status of PHP-FPM to ensure it’s running properly:

root@localhost:~$ systemctl status php8.1-fpm

The above command should output something that resembles the following.

Sample Output:

● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2023-09-10 16:33:51 UTC; 47s ago
       Docs: man:php-fpm8.1(8)
   Main PID: 10452 (php-fpm8.1)
      Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 1588)
     Memory: 7.0M
        CPU: 27ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─10452 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
             ├─10453 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
             └─10454 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">

Sep 10 16:33:51 ubuntu systemd[1]: Starting The PHP 8.1 FastCGI Process Manager...
Sep 10 16:33:51 ubuntu systemd[1]: Started The PHP 8.1 FastCGI Process Manager.

Step 12. Enable FastCGI within Nginx Config

Now that PHP-FPM is installed along with Nginx, we need to modify Nginx’s configuration file and do some adjustments.

Edit the following file using your favourite editor. In this example we are using the ‘vi’ editor.

root@localhost:~$ vi /etc/nginx/sites-available/default

Scroll down until you find the following:

# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
#       include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
#       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
#       # With php-cgi (or other tcp sockets):
#       fastcgi_pass 127.0.0.1:9000;
#}

That section is inactive in its current state as the directives are commented out.

Delete it and replace with the following block of code:

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

Save the file and exit.

Step 13. Verify Nginx Syntax and Apply Changes

Before restarting the Nginx server after making configuration changes, it is a good idea to test the Nginx configuration for syntax errors. To do that, run nginx -t which should give you output similar to the following:

root@localhost:~$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If any errors are reported, you need to go back to the previous step and correct them by editing the Nginx configuration file.

You are now ready to restart the nginx server, using the following command:

root@localhost:~$ systemctl restart nginx

All set! You can verify a successful Nginx installation by browsing to your server’s IP Address. You should be seeing Nginx’s default welcome page as per the following screenshot.

Nginx Default Welcome Page

Deploy an Ubuntu 22.04 VPS Server in any of our global locations. Deployment in 60 seconds from €4.90 per month!

Press Releases
78

Free VPS Trial

No Credit Card Required.

Recent Posts

How To Extract Certificates and Private Key from .PFX file

How To Extract Certificates and Private Key from .PFX file

19 November, 2024

NetShop ISP CEO Stefano Sordini to Moderate Key Panel on Forex AI Automation at Cyprus Fintech Summit 2024

NetShop ISP CEO Stefano Sordini to Moderate Key Panel on Forex AI Automation at Cyprus Fintech Summit 2024

15 November, 2024

Preparing for the Daylight Saving Time (DST) Change: Essential Updates for MetaTrader 4/5 Platforms

Preparing for the Daylight Saving Time (DST) Change: Essential Updates for MetaTrader 4/5 Platforms

25 October, 2024

NetShop ISP Celebrates 20 Years of Innovation and Excellence in Hosting and Data Center Solutions

NetShop ISP Celebrates 20 Years of Innovation and Excellence in Hosting and Data Center Solutions

22 October, 2024

NetShop ISP Makes Waves at Forex Expo Dubai 2024: Showcasing Cutting-Edge Hosting for the Financial Services Industry

NetShop ISP Makes Waves at Forex Expo Dubai 2024: Showcasing Cutting-Edge Hosting for the Financial Services Industry

09 October, 2024

#letushostyou

Award Winning Hosting Provider established in 2004.

120 Faneromenis Avenue, Imperial Tower, 2nd Floor, Larnaca 6031, Cyprus

Products

Bare Metal Servers

Customized Servers

Virtual / Cloud Servers

Forex VPS

Storage VPS

cPanel Web Hosting

Reseller Web Hosting

Colocation

Addons

Premium DNS

Email Hosting

Cloud Backup

DDoS Protection

Licenses

SSL Certificates

Domain Names

Premium SLAs

About Us

Data Center Locations

Looking Glass

Our Company

Contact Us

Careers in Cyprus

Become a Partner

Awards

Certifications

© 2024 S.S. NetShop Internet Services Ltd. All rights reserved.  Terms & Conditions  |  Privacy Policy
CY Reg. Number: HE 217340 | EU VAT Number: CY10217340J

Visa
Mastercard
PayPal
Bitcoin
Tether
Ethereum
Litecoin
Wise
Revolut
Wire Transfer