How install Ubuntu Server 20.04 + Nginx + PHP 7.2 or PHP 8.1 and MySQL

 

How install Ubuntu Server 20.04 and Nginx 

1 Download Ubuntu Server

Download ubuntu 18 server fron this url: download You should download Server Installation image. 


2 Install ubuntu Server

Install ubuntu server like you usually install using a booting usb or CD.


3 Installing Nginx

Execute command to update your local apt repo. 

 
sudo apt update



Execute this command for installa Nginx from apt repository.


 
sudo apt install nginx


Validate correct installation, go head to browser and put http:// and the public IP server.

 4 Installing PHP 7.1 or 8.1 with FPM

You will want to avoid relying on the default version of PHP because that default version could change depending on where you are running your code. You may also wish to install a different version to match an application you are using or to upgrade to a newer version, such as PHP 8.1.

Next, install software-properties-common, which adds management for additional software sources:

 
sudo apt -y install software-properties-common


The -y flag will automatically agree to the installation. Without that, you would receive a prompt in your terminal window for each installation.

Next, install the repository ppa:ondrej/php, which will give you all your versions of PHP:

 
    sudo add-apt-repository ppa:ondrej/php


You update apt-get again so your package manager can see the newly listed packages:


 
sudo apt-get update


Installing PHP 7.4 FPM

You can install version that you wish, in this case is PHP version 7.4


sudo apt install php7.4-fpm php7.4-mysql php7.4-mbstring php7.4-xml php7.4-bcmath

or 

Installing PHP 8.1 FPM

PHP 8.1


sudo apt install php8.1-fpm php8.1-mysql php8.1-mbstring php8.1-xml php8.1-bcmath


7 If you have installed more than one PHP version of the same pack

Check php version using 

php -v

Type this command and select your cli version


 
sudo update-alternatives --config php


Setting Ngnix


5.1 Create new site on Nginx


If we have installed web application on a local folder of your remote user’s home directory, and while this works well for local development environments, it’s not a recommended practice for web servers that are open to the public internet. We’ll move the application folder to /var/www, which is the usual location for web applications running on Nginx.

 
sudo mv ~/basic_app /var/www/basic_app

Or you can crear a new folder using de command

<sudo mkdir basic_app>

giThis command will create folder with root group and root owner.


Now we need to give the web server user write access to the storage and cache folders, where stores application-generated files, in this case we will acces to main forlder application by example.


 
sudo chown -R www-data.www-data /var/www/basic_app


You will see a change over permision like this.



The application files are now in order, but we still need to configure Nginx to serve the content. To do this, we’ll create a new virtual host configuration file at /etc/nginx/sites-available:

Create a new file with your site name, example:


 
sudo nano /etc/nginx/sites-available/basic_app

Set this configuration, adjust for you preferences.


 

server {

    listen 80;

    server_name server_domain_or_IP;

    root /var/www/travel_list/public;



    add_header X-Frame-Options "SAMEORIGIN";

    add_header X-XSS-Protection "1; mode=block";

    add_header X-Content-Type-Options "nosniff";



    index index.html index.htm index.php;



    charset utf-8;



    location / {

        try_files $uri $uri/ /index.php?$query_string;

    }



    location = /favicon.ico { access_log off; log_not_found off; }

    location = /robots.txt  { access_log off; log_not_found off; }



    error_page 404 /index.php;



    location ~ \.php$ {

        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

 asia       include fastcgi_params;

    }



    location ~ /\.(?!well-known).* {

        deny all;

    }

}



To confirm that the configuration doesn’t contain any syntax errors, you can use:


 
sudo nginx -t

You should see output like this:



To apply the changes, reload Nginx with:


 
sudo systemctl reload nginx


Now go to your browser and access the application using the server’s domain name or IP address, as defined by the server_name directive in your configuration file:

http://server_domain_or_IP


6 Installing MySQL

Install mysql server using command. 

 
sudo apt install mysql-server


4.1 Seting password

Execute command:


 
 sudo mysql_secure_installation



Ask confirmation for set passwords, type "y" for continue.

Ask for a level security for password, for low password set "0"


Here set your password.


Confirm password and press enter. 

This password will be your MySQL root password.

Will ask  if you wish disable remove anonymous user, you can to type "Y".


Will ask if you wish allow remote acces to root user, type "n".

Will ask if you wish remove test data base and access to it, type "y".

To test your acces type command: 


 
  sudo mysql -u root -p


Will ask your root password previously saved.




Thanks for read this tutorial leave your comments.


References:

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-laravel-with-lemp-on-ubuntu-18-04

https://www.cyberciti.biz/faq/install-and-configure-nginx-on-ubuntu-linux-18-04-lts/

Comments