Skip to content

Instantly share code, notes, and snippets.

@webmaster-z1lab
Last active December 15, 2020 18:49
Show Gist options
  • Save webmaster-z1lab/234f1d9f73f400cda31b5def28191b9f to your computer and use it in GitHub Desktop.
Save webmaster-z1lab/234f1d9f73f400cda31b5def28191b9f to your computer and use it in GitHub Desktop.
Setup a Ubuntu 18.04 LTS webserver (Laravel ready) with Nginx (PageSpeed build), Brotli compression, PHP, PHP-FPM, MongoDB, LetsEncrypt SSL, Composer, Yarn (NPM) and Supervisor(for Laravel Queues).

Setup a Ubuntu 18.04 LTS webserver (Laravel ready) with Nginx (PageSpeed build), Brotli compression, PHP, PHP-FPM, MongoDB, LetsEncrypt SSL, Composer, Yarn (NPM) and Supervisor(for Laravel Queues).

Add brotli repository

apt install libssl-dev
cd /usr/local/src
git clone --recursive https://github.com/google/ngx_brotli.git

Install certbot

sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python-certbot-nginx 

Build nginx with pagespeed

[https://www.linode.com/docs/web-servers/nginx/build-nginx-with-pagespeed-from-source/]

bash <(curl -f -L -sS https://ngxpagespeed.com/install) \
--nginx-version latest

Add the build parameters

--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --add-module=/usr/local/src/ngx_brotli

Config the nginx initializer

sudo nano /lib/systemd/system/nginx.service
sudo systemctl enable nginx
sudo systemctl start nginx

Config the nginx server

sudo useradd --no-create-home nginx
sudo mkdir -p /var/cache/nginx/client_temp
sudo mkdir /etc/nginx/conf.d/
sudo mkdir /var/www/example.com #your domain
sudo chown nginx:nginx /var/www/example.com
sudo mv /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf.backup-default

Config the server block

sudo nano /etc/nginx/conf.d/example.com.conf

Config PageSpeed

sudo mkdir /var/cache/ngx_pagespeed/
sudo chown nginx:nginx /var/cache/ngx_pagespeed/
curl -I -X GET example.com #Test your server

Config Brotli compression

[https://www.howtoforge.com/tutorial/how-to-install-nginx-with-brotli-compression-on-ubuntu-1804/]

sudo nano /etc/nginx/nginx.conf

Install php

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt-get install php7.3 php7.3-fpm php-xml php7.3-xml php7.3-zip php7.3-xmlrpc php7.3-gd php7.3-curl php7.3-pdo php-pear php7.3-mbstring php7.3-gmp php7.3-mongodb php7.3-dev php7.3-pgsql php7.3-intl

Config php and php-fpm

sudo usermod -aG www-data nginx

Config php-fpm

sudo nano /etc/php/7.3/fpm/pool.d/www.conf

Run certbot

sudo certbot --nginx

Install MongoDB

[https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/]

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org

Config MongoDB

systemctl enable mongod.service
sudo nano /etc/mongod.conf
sudo service mongod start

Add MongoDB PHP Extension

sudo pecl install mongodb
sudo bash
sudo echo "extension=mongodb.so" >> /etc/php/7.3/fpm/php.ini
exit
sudo service nginx restart

install Composer

cd ~
sudo curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composer
sudo ln -s /usr/local/bin/composer /usr/bin/composer #add composer to sudo user
sudo chown ubuntu: /home/ubuntu/.composer/cache/

Config git

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global credential.helper 'cache --timeout=3600' #cache your password

Set permission for cache Git credentials

sudo chown ubuntu ~/.cache/git/credential/

Install Node and NPM

sudo apt-get install nodejs
sudo apt-get install npm

Install Yarn

[https://yarnpkg.com/lang/en/docs/install/#debian-stable]

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

Install Supervisor

sudo apt-get install supervisor
sudo nano /etc/supervisor/conf.d/example.worker.conf #Your custom worker

Optional

Install Redis Server

sudo apt install redis-server
sudo systemctl enable redis-server
sudo nano /etc/redis/redis.conf

Install PostgreSQL

sudo apt install postgresql postgresql-contrib
sudo -u postgres psql postgres
\password postgres
\q

Install Laravel Echo Server

sudo chown -R ubuntu: /usr/local/lib
sudo chown -R ubuntu: /usr/local/bin
npm install -g laravel-echo-server
sudo nano /etc/supervisor/conf.d/laravel-echo-worker.conf

Clone the repository to webserver dir and set the permissions

cd /var/www
sudo git clone example.rep example.com
sudo chown -R ubuntu /var/www/example.com
sudo chgrp nginx /var/www/example.com
sudo chmod g+rwx /var/www/example.com
sudo chgrp -R nginx /var/www/example.com/storage
sudo chmod -R g+rwx /var/www/example.com/storage
[program:example-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/example.com/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
user=nginx
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/example.com/worker.log
# /etc/nginx/conf.d/example.com.conf
server {
server_name example.com www.example.com;
listen 80;
listen [::]:80;
# ssl_protocols TLSv1.2 TLSv1.3;
location / {
root /var/www/example.com; #your domain
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
root /var/www/example.com;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; #find the location of sock using: ls /var/run/php/
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#PageSpeed config
pagespeed on;
pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
pagespeed RewriteLevel OptimizeForBandwidth;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
}
[program:laravel-echo-worker]
process_name=%(program_name)s_%(process_num)02d
directory=/var/www/example.com
command=laravel-echo-server start
autostart=true
autorestart=true
user=nginx
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/example.com/worker.log
# /etc/mongod.conf - In the first config line
storageEngine=wiredTiger
# /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
client_max_body_size 100m;
index index.html index.htm;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
ssl_protocols TLSv1.2 TLSv1.3;
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server_names_hash_bucket_size 64;
}
# /etc/redis/redis.conf
...
supervised systemd
...
# /etc/php/7.2/fpm/pool.d/www.conf
[global]
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s
[www]
listen = /run/php/php7.3-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0664
user = nginx
group = nginx
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 200
php_admin_value[memory_limit] = 128M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment