Skip to content

Instantly share code, notes, and snippets.

@gr1zix
Last active July 18, 2022 19:44
Show Gist options
  • Save gr1zix/936aa1cdea016c766e89cb2137a929bc to your computer and use it in GitHub Desktop.
Save gr1zix/936aa1cdea016c766e89cb2137a929bc to your computer and use it in GitHub Desktop.
Laravel LEMP server configurations [Nginx, MySQL PhpMyAdmin, php7.2-fpm, Redis, Composer)
When the development process has finished and the application meets the client’s requirements it’s ready to be pushed into a live hosting environment. The most used service for this is usually Git and this is where the transition from development to production is done. The most common steps involve:

1. Cloning the ready production code:

git clone {Repository}
2. Installing Composer dependencies

composer install --no-dev –optimize-autoloader
3. Laravel uses the PHP library dotenv for its configuration, for this the .env file needs to be adjusted to meet the server settings

4. Applying caching the configuration and route cache via Artisan:

php artisan config:cache

php artisan route:cache
location /phpmyadmin {
root /usr/share/;
index index.php;
try_files $uri $uri/ =404;
location ~ ^/phpmyadmin/(doc|sql|setup)/ {
deny all;
}
location ~ /phpmyadmin/(.+\.php)$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|png|ico|css|js|html|xml|text))$ {
root /usr/share/;
}
}
server {
listen 80;
listen [80]:80 default_server ipv6only=on;
server_name localhost;
charset utf-8;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
root /var/www/project/public;
index index.php index.html index.htm;
gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;
location ~* \.(jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf|html)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
location ~* \.(css|js)$ {
expires max;
access_log off;
add_header Cache-Control "public";
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
access_log off;
error_log /var/log/nginx/localhost-error.log error;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
## Sources with good tips
# Source: [*] https://gist.github.com/butschster/48d33bd1e3a8192ca4059d8e6a459118
# Source: [*] https://medium.com/@asked_io/how-to-install-php-7-2-x-nginx-1-10-x-laravel-5-6-f9e30ee30eff
# Source: [*] https://gist.github.com/santoshachari/87bf77baeb45a65eb83b553aceb135a3
# Source: [*] https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04
# Source: https://websiteforstudents.com/install-laravel-on-ubuntu-17-04-17-10-with-nginx-mariadb-and-php-support/
# Source: https://www.howtoforge.com/tutorial/how-to-install-laravel-5-with-nginx-on-ubuntu-1604/
# Source: [RU] https://www.8host.com/blog/razvertyvanie-prilozheniya-laravel-na-nginx-v-ubuntu-16-04/
# ================================================
# Remove Permanently Apache 2 for newly created server
#
# See: https://askubuntu.com/questions/642238/why-do-i-still-see-an-apache-site-on-nginx
# See: https://askubuntu.com/questions/176964/permanently-removing-apache2
# ================================================
# Step 1
sudo service apache2 stop # First stop the apache2 service if it is running
# Step 2
sudo apt-get --purge remove apache2.* # use --purge if you want the configuration files to be deleted as well
# otherwise use
# sudo apt remove apache2.*
# Step 3
#You can do the following two tests to confirm apache has been removed:
# 1)
which apache2 #should return a blank line
# or
dpkg -S `which apache2`
# 2)
sudo service apache2 start #should return apache2: unrecognized service
# Step 4
# Check whether there are any configuration files that have not been removed.
whereis apache2
# Step 5
# If you get a response as follows apache2: /etc/apache2 remove the directory and existing configuration files.
sudo rm -rf /etc/apache2
# ================================================
# PHP 7.1
#
# See https://www.colinodell.com/blog/201711/installing-php-72
# ================================================
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php7.1-cli php7.1-gd php7.1-mbstring php7.1-curl php7.1-xml php7.1-zip php7.1-json php7.1-common php7.1-gettext
sudo apt-get install -y php7.1-mcrypt
# Check what user PHP-FPM is running as (it's www-data)
ps aux | grep php
# ================================================
# MySQL
#
# See: https://tecadmin.net/install-apache-mysql-php-lamp-stack-on-ubuntu-16-04/
# Documentation: https://docs.mongodb.com/
# ================================================
#sudo apt-get install -y mysql-server mysql-client php7.1-mysql
sudo apt-get install -y mysql-server php7.1-mysql
mysql
# Create new database and user from file mysql.conf
# Check mysql extension is enabled
php --ini
# Secure MySQL Server
sudo mysql_secure_installation
# Input your root password, remove anonymous users, disallow root login remotely etc.
# Set root password? [Y/n] y
# - Remove anonymous users? [Y/n] y
# - Disallow root login remotely? [Y/n] y
# - Remove test database and access to it? [Y/n] y
# - Reload privilege tables now? [Y/n] y
# ================================================
# PhpMyAdmin
#
# See https://tecadmin.net/install-apache-mysql-php-lamp-stack-on-ubuntu-16-04/
# Documentation:
# ================================================
sudo apt-get install phpmyadmin
# Test if MySQL is working properly
netstat -ln | grep 3306
# tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
# ================================================
# Nginx
#
# See: https://www.digitalocean.com/community/tutorials/nginx-ubuntu-16-04-ru
# See https://laravel.com/docs/5.6/deployment
# Documentation: https://nginx.ru/en/docs/
# ================================================
sudo apt-get install -y nginx php7.1-fpm
systemctl stop nginx.service
systemctl stop php7.1-fpm.service
# Check if nginx and php-fpm process are closed
netstat -plntu
# ===============================
# IF Default webiste
#
# use config from file nginx-host.conf
# ===============================
vim /etc/nginx/sites-available/default
# ===============================
# IF Custom webiste
#
# See: https://websiteforstudents.com/install-laravel-on-ubuntu-17-04-17-10-with-nginx-mariadb-and-php-support/
# See: https://www.howtoforge.com/tutorial/how-to-install-laravel-5-with-nginx-on-ubuntu-1604/
# use config from file nginx-host.conf
# ===============================
vim /etc/nginx/sites-available/project
ln -s /etc/nginx/sites-available/project /etc/nginx/sites-enabled/
# Custom end
# Syntax errors checking
sudo nginx -t
systemctl start nginx.service
systemctl start php7.1-fpm.service
# Test if Ngixn is working properly
netstat -ln | grep 80
# tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
# ================================================
# Redis
#
# Documentation: https://redis.io/documentation
# ================================================
apt-get install -y build-essential tcl software-properties-common
# From redis PPA repository [Prefer way for me]
#
# See https://linode.com/docs/databases/redis/how-to-install-a-redis-server-on-ubuntu-or-debian8/
sudo add-apt-repository ppa:chris-lea/redis-server
sudo apt-get update
sudo apt-get install redis-server
sudo service redis-server start
# Run CLI
redis-cli
# Test
127.0.0.1:6379>ping
PONG
# From sources
#
# See https://www.hugeserver.com/kb/install-redis-4-debian-9-stretch/
wget http://download.redis.io/releases/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable/
make && make install
make test
cd utils/
bash install_server.sh
systemctl start redis_6379
systemctl enable redis_6379
# Test if Redis is working properly
netstat -ln | grep 6379
# tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
cd ../../
rm redis-stable -Rf
rm redis-stable.tar.gz
# Note:
# Use Horizon to queues monitor
# https://laravel.com/docs/5.6/horizon
# ================================================
# Composer
#
# See https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx
# Documentation: https://getcomposer.org/doc/
# ================================================
sudo apt-get install -y composer
# ================================================
# Git
#
# See https://laravel.com/docs/5.6/installation
# ================================================
sudo apt-get install -y git
# ================================================
# Laravel
#
# See https://laravel.com/docs/5.6/installation
# See https://laravel.com/docs/5.6/filesystem#the-public-disk
# Documentation: https://laravel.com/docs/
# ================================================
# Clone your project into /var/www
git clone ... /var/www
# Change folders permissons
chown -R www-data:www-data /var/www/project
chmod -R 755 /var/www/project
chmod -R 777 /var/www/project/storage
# if needed set read write rules for public folder
chmod -R 775 /var/www/project/public
cd /var/www/project
# Install packages via composer
composer install
cp .env.example .env
php artisan key:generate
php artisan storage:link
php artisan migrate
# ================================================
#
Link
# must be: http://your_domain_or_ip/phpmyadmin
#
# See https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-an-ubuntu-14-04-server
#
# ================================================
# ======= WAY 1 ===================================
#
# See: [nginx] https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-ubuntu-16-04
# See: [nginx] https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-an-ubuntu-14-04-server
# See: [apache] https://www.digitalocean.com/community/tutorials/phpmyadmin-ubuntu-16-04-ru
# See: [apache] https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-16-04
# =================================================
# ================================================
# Install Mcrypt
#
# See: https://lukasmestan.com/install-mcrypt-extension-in-php7-2/
# ================================================
# if not result of
pecl version
# setup phpize7.1
sudo apt-get install -y php7.1-dev
#check phpize7.1 instalation
phpize7.1 -v
sudo apt-get -y install php-pear libmcrypt-dev
sudo pecl install mcrypt-snapshot
# ===
# Add the mcrypt to php extensions
# See: https://gist.github.com/cwhsu1984/e13d4648ddd7723eabd2a01b9dffcf15
# ===
# add mcrypt.so to php cli and fpm
sudo bash -c "echo 'extension=mcrypt.so' >> /etc/php/7.1/cli/php.ini"
# OR
sudo bash -c "echo 'extension=mcrypt.so' >> /etc/php/7.1/fpm/php.ini"
# ?? XZ
sudo bash -c "echo extension=/usr/lib/php/20170718/mcrypt.so > /etc/php/7.1/cli/conf.d/mcrypt.ini" # cli
sudo bash -c "echo extension=/usr/lib/php/20170718/mcrypt.so > /etc/php/7.1/fpm/mods-available/mcrypt.ini" # fpm
# sudo bash -c "echo extension=/usr/lib/php/20170718/mcrypt.so > /etc/php/7.1/apache2/conf.d/mcrypt.ini" # Apache
sudo phpenmod mcrypt
systemctl restart php7.1-fpm
# ==== WAY 2 ====================
#
# If previous not working
#
# See: https://www.digitalocean.com/community/questions/installing-phpmyadmin-on-ubuntu-16-04-lemp
# See: https://serverfault.com/questions/521298/cant-get-access-to-phpmyadmin-with-nginx-under-debian-no-input-file-specified
# ===============================
# 1)
vim /etc/nginx/sites-availables/default # or your project name
# 2) Place at the file bottom
phpmyadmin.conf # file contains
# 3) Restart nginx
sudo service nginx restart
# =============================
#
# Secure PhpMyAdmin
# Adding Admin auth
#
# See: [Reference] https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-ubuntu-16-04
# =============================
# 1) Create an encrypted password
openssl passwd
# 2) type the password and store it (input password is password what you need to type when going on /phpmyadmin)
# 3) Copy hashed output password from openssl passwd
# 4) Create new auth file
sudo vim /etc/nginx/pma_pass
#paste Example
username:generated_password
sudo vim /etc/nginx/sites-available/default
sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/html
# Paste inside at start location
/phpmyadmin {
auth_basic "Secured Section";
auth_basic_user_file /etc/nginx/pma_pass;
# Example:
# location /phpmyadmin {
# #Auth
# auth_basic "Secured Section";
# auth_basic_user_file /etc/nginx/pma_pass;
# #Context
# root /usr/share/;
# index index.php;
# try_files $uri $uri/ =404;
# location ~ ^/phpmyadmin/(doc|sql|setup)/ {
# deny all;
# }
#}
sudo service nginx restart
# For authentication use your username from pma_pass file and password what you input before hashing (done)
# ===============================
# PhpMyAdmin Issue: Fix Bug Phpmyadmin [plugin_interface.lib.php] + Php7.1 + Ubuntu 16.04
#
# Solution: https://medium.com/@chaloemphonthipkasorn/%E0%B9%81%E0%B8%81%E0%B9%89-bug-phpmyadmin-php7-2-ubuntu-16-04-92b287090b01
# ===============================
# ================================================
# Crontab
#
# See: https://gist.github.com/butschster/48d33bd1e3a8192ca4059d8e6a459118
# See https://laravel.com/docs/5.6/scheduling#introduction
# ================================================
# raw
crontab -e
# or open in nano
export VISUAL=nano; crontab -e
#or vim
export VISUAL=vim; crontab -e
# Append new line
* * * * * php /var/www/your_project/artisan schedule:run >> /var/www/storage/logs/cron.log 2>&1
# =================================================
# NPM and NodeJS install
#
# See https://websiteforstudents.com/install-the-latest-node-js-and-nmp-packages-on-ubuntu-16-04-18-04-lts/
# =================================================
# ================================================
# Key based authentication (Git | BitBucket SSH Authorization)
#
# See https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server
# ================================================
# generate key
ssh-keygen
# create authorization file for auth via ssh
touch ~/.ssh/authorized_keys
# store generated key and allow auth
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
ssh-copy-id username@remote_host # Example root@127.0.0.1
# connet
ssh sername@remote_host
# allow connecting without password [Important services]
nano /etc/ssh/sshd_config
# PasswordAuthentication no
systemctl restart ssh
# ===================================================
# Downgrade PHP 7.2 To PHP 7.1 With Nginx Support On Ubuntu 16.04 / 17.10 And 18.04
#
# Reason: count(): Parameter must be an array or an object that implements Countable (php7.2 counting error)
#
# Source: https://websiteforstudents.com/downgrade-php-7-2-to-php-7-1-with-nginx-support-on-ubuntu-16-04-17-10-and-18-04/
# ===================================================
# After installing all php7.1 packages and extension needed replace an older php ver folder to newest
sudo rm /usr/bin/php
sudo ln -s /usr/bin/php7.1 /usr/bin/php
# ===================================================
# Imagic extension
#
#
# Source: https://gist.github.com/jackmu95/e17c225b7eb4baa9485ecec91b15477e
# ===================================================
wget https://gist.githubusercontent.com/jackmu95/e17c225b7eb4baa9485ecec91b15477e/raw/ccdf51f75941f33543947762516284fae7d245fe/imagick3.4.3-PHP7.1-forge.sh
sudo bash imagick3.4.3-PHP7.1-forge.sh
# ===================================================
# Increase PhpMyAdmin File Loading Size
#
#
# Source: https://www.digitalocean.com/community/questions/how-to-extend-limit-of-import-file-in-phpmyadmin
# ===================================================
vim /etc/php/7.1/fpm/php.ini
# chage
# 1)
post_max_size = 256M
# 2)
upload_max_filesize = 256M
# save
:wq
# restart FPM and NGINX
systemctl restart nginx.service
systemctl restart php7.1-fpm.sevice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment