These instructions are meant for beginners and they make sense as I'm writing them at Sept 2020. I'll try to keep it updated.
First, let's just update the OS
$ apt update
Now install Nginx
$ apt install nginx
Install and configure UFW, which is basically the firewall of the server
$ apt install ufw
$ ufw allow ssh
$ ufw allow 'Nginx HTTP'
$ ufw allow 'Nginx HTTPS'
Install the php version you require (in my case is 7.4)
$ apt -y install lsb-release apt-transport-https ca-certificates wget
$ wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
$ apt install curl php-mbstring git unzip
$ apt install php7.4 php7.4-cli php7.4-common
$ apt update
$ apt install php7.4-fpm
$ apt install php7.4-xml
$ apt install php-xml
$ apt install php7.4-gd
$ apt install php7.4-mbstring
$ apt install php7.4-zip
$ apt install php7.4-soap
Install composer to install the dependencies of your project
$ curl -sS https://getcomposer.org/installer -o composer-setup.php
Now this is a important one, do not just copy and paste, replace the HASH for whatever is here
$ HASH=e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a
Verify you did it right
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
That should throw a Installation verified message.
$ php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Install MySLQ
$ apt install mariadb-server
$ mysql_secure_installation
Get the connector between php (in my case 7.4) and MySQL
$ apt install php7.4-mysql
Create a user for your application
$ mysql
$ use mysql;
$ CREATE USER '{ my_db_username }'@'localhost' IDENTIFIED BY '{ my_db_password }';
$ GRANT ALL PRIVILEGES ON *.* TO '{ my_db_username }'@'localhost';
$ FLUSH PRIVILEGES;
$ exit
$ service mysql restart
Personally, I find git as a great tool to deploy, specially in pre-alpha and alpha stages of the project. Here is how I do it:
Get git
$ apt install git-all
Prepare the dir where you're gonna store your ssh key for git
$ mkdir ~/.ssh/
Create your ssh keys
$ cd ~/.ssh/ && ssh-keygen
This last command is going to give you the options to create your keys, I usually go for id_rsa
Now let's see our public key
$ cat id_rsa.pub
Copy the key and paste it on your git repository as a deploy key. Here's how to do it in github . With this, you're telling your repository to let your server pull the code. At least in github, it asks if you want to allow the server to write into the repo, which in most of cases I would think you don't want that (you're making a deploy, you're not interested in getting code or file from the server)
Clone the repo into the usual path
$ cd /var/www/ && git clone { your-repo-url }
First off, you don't really "install" laravel, you will only install the dependencies of your project and configure some necessary files.
While you're in your project directory, install the composer dependencies
$ composer install
Copy the .env.example into a new file
$ cp .env.example .env
Or create it
$ touch .env
I'm not going to get deep into the .env file configuration, just be aware your DB configuration should match with what you prepared before in the MySQL installation
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={ my_db_name }
DB_USERNAME={ my_db_username }
DB_PASSWORD={ my_db_password }
You must grant these permissions in order for laravel to start
$ chown -R www-data.www-data /var/www/{ your-application }
$ chmod -R 755 /var/www/{ your-application }
$ chmod -R 777 /var/www/{ your-application }/storage
Now, after granting those permissions, take on count that git will consider them as changes in your repository, and it can become a pain in the butt when you try to pull (a.k.a. deploy) because it may consider it as a confict. Correct that just by fetching to the origin
$ git fetch origin
$ git reset --hard origin/master
There is very little documentation about how to configure Laravel 7.x for php7.4 in Nginx, but this default file for Nginx has worked for me so far:
server {
listen 80;
server_name example.com;
root /var/www/{ YOUR-APPLICATION }/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;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}