Giter VIP home page Giter VIP logo

laravel-prod-server's Introduction

Install and run a production server for a Laravel application

You already own:

  • A domain name
  • A Github account
  • A fresh Ubuntu 16.04 Server with root access

You want to deploy an application for:

  • Laravel with a PostgreSQL database
  • Running on PHP7 and served by Nginx
  • Optionally, Redis for Queue, Session and Cache

If so, let’s go, this is for you.

Recipe

Let's say:

  • raphael is your name.
  • nantes is your hostname.
  • myapp is your app name.

You will have to replace each occurrence of theses values in the code below.

This recipe has 11 steps, it may take half an hour to make it.

  1. Initialization
  2. Composer
  3. PostgreSQL
  4. Firewall
  5. Application user
  6. Facteur
  7. Laravel
  8. Nginx
  9. Your user
  10. Disable password auth
  11. Https

Initialization

Connect to your server with root user. Then, name your host

hostname nantes

Install packages (PHP7, Nginx, PostgreSQL, etc.)

apt update
apt upgrade
apt install php php-mbstring php-xml php-zip git nginx redis-server postgresql fail2ban htop php-pgsql

You can go to your server IP with your browser, you should see a welcome message from Nginx

Composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer

PostgreSQL

Run psql:

sudo -u postgres psql

Create the user (don't forget to use your own password):

CREATE ROLE myapp LOGIN UNENCRYPTED PASSWORD 'xxxxx' SUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;

Write down the password, you will need it later. Disconnect from psql with \q. Then restart service and create the database:

service postgresql restart
sudo -u postgres /usr/bin/createdb --echo --owner=myapp myapp

Firewall

Configure and enable ufw

ufw allow 443
ufw allow 80
ufw allow 22
ufw enable

Application user

You need to create an application user. It's not your name. Replace myapp everywhere with the name of your application.

useradd myapp
mkdir -p /home/myapp
chsh -s /bin/bash myapp
chown -R myapp:myapp /home/myapp
sudo -u myapp ssh-keygen -t rsa -b 4096 -C "myapp"

Optional step: Copy your public key, located in /home/myapp/.ssh/id_rsa.pub, to your github account or repository (only if the repository is private). It will be required to make continuous delivery work without connecting to your server.

Facteur

Facteur is a small tool for initializing and deploying Laravel applications. Install it.

cd /home/myapp
wget https://github.com/rap2hpoutre/facteur/releases/download/v0.1.0/facteur
chmod +x facteur
mv facteur /usr/local/bin/facteur

Init your app with deployer (replace https://github.com/your/app.git with the URL to your git repo).

sudo -u myapp HOME=/home/myapp facteur init www https://github.com/your/app.git

Laravel

Copy .env.example to .env

sudo -u myapp cp /home/myapp/www/current/.env.example  /home/myapp/www/current/.env

Edit /home/myapp/www/current/.env

vi /home/myapp/www/current/.env

Change the values of DB_* with your credentials.

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=myapp
DB_USERNAME=myapp
DB_PASSWORD=xxxxx

Change APP_ENV=local to APP_ENV=production (you could disable debug too).

Use redis for everything if you want (but don't forget to include predis/predis to your laravel project in that case)

CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis

Generate the laravel key:

cd /home/myapp/www/current
sudo -u myapp php artisan key:generate

Run your first migration

sudo -u myapp php artisan migrate --force

Authorize www-data to write in storage:

chown -R www-data:www-data /home/myapp/www/shared/storage

Nginx

Edit /etc/nginx/fastcgi.conf and replace SCRIPT_FILENAME and DOCUMENT_ROOT with:

fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;

Then create and edit /etc/nginx/sites-available/myapp

server {
    listen 80 default_server;
    server_name _;
    root /home/myapp/www/current/public;
    server_tokens off;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    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_log  /var/log/nginx/hello-error.log error;
    error_page 404 /index.php;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
    location ~ /\.ht {
        deny all;
    }
}

Disable default Nginx site and enable your myapp site:

rm /etc/nginx/sites-enabled/default
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/myapp

Reload nginx

service nginx reload

You should be able to connect to your website with the IP adress.

Your user

Create and configure your user (type a password when prompted)

useradd raphael
adduser raphael sudo
chsh -s /bin/bash raphael
mkdir /home/raphael
chown -R raphael:raphael /home/raphael
cp /root/.profile /home/raphael/.profile
cp /root/.bashrc /home/raphael/.bashrc
passwd raphael

Go to your computer (not your server) to authorize your SSH key (replace ADDRESS with your address and raphael with your name)

ssh-copy-id raphael@ADDRESS

Test if connection works (you should not be prompted for password)

ssh raphael@ADDRESS

Now you should be connected with your account. Good bye root, we will not use it anymore.

Disable password auth

Edit /etc/ssh/sshd_config and add this

PasswordAuthentication no

Then run

sudo service ssh restart

Https

Everything is https now, here is how to have one for your domain (replace your.domain.com)

sudo apt install letsencrypt
sudo service nginx stop
sudo letsencrypt certonly --standalone -d your.domain.com

Edit /etc/nginx/sites-available/myapp and change the first lines

server {
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/your.domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your.domain.com/privkey.pem;
    server_name _;
    # ...
}

Enable automatic renew of the certificate

sudo letsencrypt renew --dry-run --agree-tos
sudo service nginx start

Edit /home/raphael/renew.sh

#!/bin/bash
service nginx stop
letsencrypt renew
service nginx start

Edit /etc/crontab and add this

0 4 * * 0,3 root /home/raphael/renew.sh

Restart nginx

sudo service nginx start

Your website is ready. Go to https://your.domain.com and it works.

Next steps?

laravel-prod-server's People

Contributors

rap2hpoutre avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.