So, by now, if you've followed Part 1 and Part 2, you should have a website that is reachable from the web. The main issue you have now is that most modern websites are not just HTML, but are instead written in a dynamic language. The most common language on the web today is PHP, and the most common framework is Laravel.
The first step to get things going will be to install the necessary dependencies:
sudo apt install mysql-server mysql-client php-fpm php php-curl php-bcmath php-mbstring php-xml php-tokenizer php-zip php-soap php-mysql supervisor git nodejs -y
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
Here, we installed the MySQL database client and server, the necessary PHP libraries for Laravel, and the PHP package manager Composer. We should now make a few changes to the default PHP configurations. In /etc/php/fpm/pool.d/www.conf you will want to add something like:
[www]
user = www-data
group = www-data
listen = /run/php/php-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 512
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 0
listen.backlog = -1
request_terminate_timeout = 10s
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = no
env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
Essentially, this configuration is setting the user/group for the PHP FPM daemon, setting the controls for the number of FPM instances and the like, setting our timeout, and temp file locations, and so on. I strongly encourage playing with these variables, doing a bunch of requests, and see what happens on your host. There's no better way to learn. Now in /etc/php/fpm/php-fpm.conf you will want something like:
[global]
pid = /run/php/php-fpm.pid
error_log = /var/log/php-fpm.log
emergency_restart_threshold = 5
emergency_restart_interval = 1m
process_control_timeout = 10s
include=/etc/php/fpm/pool.d/*.conf
This is a more straight-forward configuration where we set som paths and timeouts. Nothing too fancy. Now is a good time to restart FPM with sudo systemctl restart php-fpm and should it fail check sudo journalctl -xeu php-fpm. Let's now install Laravel:
sudo su
cd /var/www
composer global require laravel/installer
laravel new mywebsite.com
cd mywebsite.com
npm install && npm run build
chown -R yourUserName:www-data /var/www/mywebsite.com
Our nginx configuration will now need to change in /etc/nginx/sites-available/mywebsite.com.conf:
server {
listen 80;
listen [::]:80;
server_name mywebsite.com www.mywebsite.com;
root /var/www/mywebsite.com/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 = /robots.txt {
add_header Content-Type text/plain;
return 200 "User-agent: *\nDisallow: /\n";
}
location / {
if ($scheme = http) {
return 301 https://$host$request_uri;
}
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
access_log /var/log/nginx/mywebsite.com-access.log;
error_log /var/log/nginx/mywebsite.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Now just test and reload the configuration: sudo nginx -t && sudo nginx -s reload
© MMIX - MMXXVI,
Bradford Morgan White
Licentiam Absurdum