Site icon DevBuddy.tech

How To Install “Nginx, PHP and MySQL” on Ubuntu Server?

mysql

If you want to ensure that your websites and applications are dynamic, one of the most important things you need to know is how to set up a robust web server. Nginx, PHP, and MySQL are known as LEMP Stacks and are extremely popular for web hosting. 

While Linux is an operating system, Nginx is a popular web server, and MySQL is a regional database management system for storing all the data. These three components will form an essential foundation for a powerful and reliable web hosting environment. It will ensure not only optimal performance but, at the same time, maximum scalability as well.

Installing Linux Nginx, MySQL, and PHP in Ubuntu 22.04

The only thing that you need to do is follow the steps for maximum efficiency:

Step 1: Update the system dependencies. This will take care of minor errors and offer maximum efficiency. For this, open the terminal and then execute the following command

sudo apt install nginx.

Update the packages to the latest version. After you have updated the setup, you will be able to start the setup.

Step 2: The next thing you will do is install Nginx. Following the same, the Firewall needs to be set up. To install Nginx on the Ubuntu 22.04 system, you must use the following command.

Install NGINX

  1. Install NGINX from the package manager.
  2. sudo apt install nginx
  3. The NGINX service starts running immediately. You can verify its status with the following command:
  4. sudo systemctl status nginx

The NGINX service is also enabled by default, meaning that it begins running automatically at system startup.

  1. Open port 80on your system’s firewall. UFW is the frontend typically used to manage firewall rules on Ubuntu. You can use the example commands to open port 80 with ufw and reload the rules so they take effect.
  2. sudo ufw allow http   sudo ufw reload
  3. Visit the default NGINX page to see your installation in action. You can find it by navigating to your server’s domain name or its IP address.

For example, if your domain name is example.com, navigate to http://example.com; if your IP address is 192.0.2.0, you can instead navigate to http://192.0.2.0.

Manage NGINX

The NGINX service runs on systemd, which means you can manage it using systemctl commands.

1) View the current status of the NGINX service using the command below:

     “sudo systemctl status nginx”.

2) Stop the NGINX service with the following command:

     “sudo systemctl stop nginx”.

     You can then start the NGINX service back up using the following command:

     “sudo systemctl start nginx”.

3) To disable the NGINX service, preventing it from beginning automatically at system         startup, execute the following:

      “sudo systemctl disable nginx”.

    You can enable the NGINX service again using:

     “sudo systemctl enable nginx”.

4) Restart the NGINX service using the command below:

    “sudo systemctl restart nginx”.

5) To reload NGINX’s configuration files, you an use the following command:

      “sudo systemctl reload nginx”.

NGINX Configuration

  1. Disable the default NGINX configuration file.
  2. sudo unlink /etc/nginx/sites-enabled/default
  3. Create an NGINX configuration file for your site.

In this example, replace example.com with your site’s domain, in both the filename, and in the file’s contents. Do the same whenever you see example.com from here on.

The configuration is

server {

    listen 80;

    listen [::]:80;

 

    server_name example.com www.example.com;

    root /var/www/html/example.com/public_html;

    index index.html;

 

    location / {

        try_files $uri $uri/ =404;

    }

 

    location ~ \.php$ {

        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;

        include snippets/fastcgi-php.conf;

    }

 

    location ~ /\.ht {

        deny all;

    }

}

Step 3: After this is done, you will need to install MySQL and then secure the same as well. To install MySQL and configure it on Ubuntu 22.04, you must use the command

Sudo apt install MySQL- server.”

To verify the MySQL server, type out “sudo service MySQL status .”To check the MySQL version, use the command “sudo mysql- V.”

For securing, MySQL installation comes with a script. This script tends to be named “mysql_secure_installation .”This will allow you to secure the MySQL server and improve efficiency. You will need to configure it with the help of the password plugin, to enhance the security. If you want to validate the password plugin, you must press y. Usually, there are three levels of password validation – low, medium, and strong.

If you want a strong password validation, then type 2.

The next prompt will need you to set a password for the MySQL root user. You will be shown the new password’s strength when you validate the password plugin. This helps you understand how much more you need to work to secure the password. 

You will need to type y, and this will allow you to confirm the password. You will also be asked to remove the anonymous user and restrict the root user access. Ensure that you end up removing the database and then reloading the provoked tables as well.

Step 4: Following the same process, you must install and configure PHP. Next, configure Nginx. Finally, you will need to install Let’s Encrypt SSL. To install the PHP, use the prompt ” sudo apt install php8.1-fpm php8.1 php8.1-common php8.1-MySQL php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl php8.1-bc math unzip -y“. 

After the same has been installed, you will need to use the following command, which can allow you to check the version of the installed PHP. The commands you need to bring in are “php v.

To configure PHP, you must change some of the values in the php.ini file. You can open a php.ini file by entering the command prompt –

sudo nano /etc/php/8.1/fpm/php.ini.

After you have modified PHP settings, you must restart PHP -;FPM. This will reflect all the changes. And finally, you will need to configure Nginx. 

The first thing that you need to do in this case is turn off the default Nginx configuration. Then, you will need to create the website directories and set up the accurate permissions as well. Next, you will create a new virtual host configuration. Finally, you will need to paste the configuration into a new file.

Once this has been done, the only thing that you need to do is enable the new configuration. You will need to install Let’s Encrypt SSL to seal all the functionalities and ensure they are working at their optimum level. The HTTPS protocol secures all the communication between a server and the client. This also provides much-needed trust to the viewers.

Configuring PHP

PHP does not require any further configuration. However, an extra security measure should be applied. The following command ensures PHP only accepts requests for files that actually exist on the server. Otherwise, it can be tricked into executing malicious code. In the following command, use the socket for the installed release of PHP. This example demonstrates how to apply the configuration for PHP 8.1. If another release of PHP is installed, replace 8.1 with the actual release number.

sudo sed -i ‘s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g’ /etc/php/8.1/fpm/php.ini

Conclusion

You can set up a powerful web server on your Ubuntu machine by installing Nginx, MySQL, and PHP. This backbone system instantly helps you get the most functional website. All you need to know is how to make the prompts and then execute the same with maximum efficiency.

 

Exit mobile version