Ubuntu Server: How to Install PHP 7.4 with Nginx. Your own VPS for Laravel Stuck
A very short note, to setup your VPS server for Laravel or other PHP apps step by step. First of all you have to update the list of available packages and their versions.
sudo apt-get update
Next one you need to install a software provides an abstraction of the used apt repositories. It allows you to easily manage your distribution and independent software vendor software sources:
sudo apt-get install software-properties-common
Now add a repository with latest versions of PHP
sudo add-apt-repository ppa:ondrej/php
Again update your packages:
sudo apt-get update
Now you need to install all PHP related extensions for your PHP stack in only one command.
By the way if you need other version of PHP like 7.4 or latest you only need to change the number in example above, because the repository that we have added previously has all the versions and works with the same syntax
apt-get install php-pear php7.4-curl php7.4-dev php7.4-gd php7.4-mbstring php7.4-zip php7.4-mysql php7.4-xml php7.4-fpm
Now install MySQL.
Note that I do not install PHP MyAdmin because i find it slow and redundant. I can recommend you adminer.org it's only one php file and do the same.
sudo apt install mysql-server-5.7
Now for create web server and web proxy install Nginx:
sudo apt install nginx
Next trick places link to sites-enabled and www folder in to your home folder to fast access
ln -s /etc/nginx/sites-enabled /root
ln -s /var/www /root
Now create yourdomain.com folder in your www path and then create new file with name yourdomain.com in sites-enabled directory with this content
server {
listen 80;
root /var/www/yourdomain.com;
index index.php index.html index.htm;
server_name yourdomain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Now restart Nginx and PHP FPM for apply changes
sudo systemctl reload nginx
service php7.4-fpm restart
If you have problem with permissions to read / write your site folder apply this command to your site folder:
sudo chown -R www-data:www-data /var/www/yourdomain.com
To complete your VPS configuration let's setup MySQL settings. First one set MySQL root password with this command:
sudo mysql_secure_installation
To create your first database, create a user and grant all user privileges to certain database execute next commands in turn.
It will crate database with name mysite and user with the same name of database and with password mysitepass
mysql
CREATE DATABASE mysite;
CREATE USER 'mysite'@'localhost' IDENTIFIED BY 'mysitepass';
GRANT ALL PRIVILEGES ON mysite.* TO 'mysite'@'localhost';
Thats all. Now you have your web server ready to use with php applications like Laravel etc.