User Tools

Site Tools


servers:nginx:lychee

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
servers:nginx:lychee [2018/12/31 04:10]
Sean Rhone [Server Block]
servers:nginx:lychee [2018/12/31 04:19] (current)
Sean Rhone old revision restored (2018/12/10 17:43)
Line 1: Line 1:
 +====== Information ======
  
 +  * Lychee ((https://lychee.electerious.com/)) ((https://github.com/LycheeOrg/Lychee))
 +  * [[Information:Realm of Espionage]]
 +  * https://media.realmofespionage.xyz
 +
 +===== Prerequisites =====
 +
 +  * [[distros:ubuntu_server | Ubuntu Server]]
 +  * [[servers:mariadb | MariaDB]]
 +  * [[servers:nginx_php_php-fpm | nginx + PHP + PHP-FPM]]
 +  * [[servers:nginx:lets_encrypt | Let's Encrypt]]
 +
 +====== Dependencies ======
 +
 +****
 +
 +  sudo apt install composer ffmpeg
 +
 +====== Download Source ======
 +
 +****
 +
 +  sudo git clone --branch 'master' --depth '1' --recurse-submodules 'https://github.com/LycheeOrg/Lychee.git' '/var/www/media' && sudo rm -Rf '/var/www/media/Lychee-front' && sudo git clone --branch 'master' --depth '1' --recurse-submodules 'https://github.com/LycheeOrg/Lychee-front.git' '/var/www/media/Lychee-front' && sudo chown -R 'www-data':'www-data' '/var/www/media' && sync
 +
 +===== PHP Dependencies =====
 +
 +****
 +
 +  sudo -u 'www-data' composer update --working-dir='/var/www/media'
 +
 +====== Database ======
 +
 +  sudo mysql
 +
 +  CREATE DATABASE lychee;
 +
 +  GRANT ALL PRIVILEGES ON lychee.* to 'lychee'@'localhost' IDENTIFIED BY 'x';
 +
 +  FLUSH PRIVILEGES;
 +
 +====== nginx + PHP-FPM Configuration ======
 +
 +===== PHP-FPM Socket =====
 +
 +  sudo -e '/etc/php/7.2/fpm/pool.d/media.conf' && sudo systemctl restart 'php7.2-fpm'
 +
 +<code>
 +[media]
 +user = www-data
 +group = www-data
 +
 +listen = /run/php/media.sock
 +listen.owner = www-data
 +listen.group = www-data
 +listen.allowed_clients = 127.0.0.1
 +
 +pm = dynamic
 +pm.max_children = 8
 +pm.start_servers = 3
 +pm.min_spare_servers = 2
 +pm.max_spare_servers = 4
 +
 +php_value[date.timezone] = "America/New_York"
 +php_value[upload_tmp_dir] = "/tmp"
 +
 +php_value[max_execution_time] = "200"
 +php_value[memory_limit] = "512M"
 +php_value[post_max_size] = "500M"
 +php_value[upload_max_filesize] = "500M"
 +php_value[max_file_uploads] = "100"
 +</code>
 +
 +===== FastCGI =====
 +
 +  sudo -e '/etc/nginx/snippets/media.conf'
 +
 +<code>
 +location ~ \.php$ {
 +    include /etc/nginx/fastcgi.conf;
 +    fastcgi_intercept_errors on;
 +    fastcgi_pass unix:/run/php/media.sock;
 +}</code>
 +
 +===== Server Block =====
 +
 +  * If ''Diagnostics'' or ''Logs'' from the website are needed, comment the deny section out at the bottom
 +
 +  sudo -e '/etc/nginx/sites-available/media.conf'
 +
 +<code>
 +server {
 +    listen '443' 'ssl' 'http2';
 +    server_name 'media.realmofespionage.xyz';
 +    root '/var/www/media';
 +    index 'index.html' 'index.php';
 +
 +    include '/etc/nginx/snippets/media.conf';
 +    include '/etc/nginx/snippets/restrictions.conf';
 +    include '/etc/nginx/snippets/headers.conf';
 +
 +    client_max_body_size '500M';
 +
 +    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'" always;
 +
 +#    access_log /var/log/nginx/media-access.log;
 +#    error_log /var/log/nginx/media-error.log;
 +}</code>
 +
 +==== Enable Server Block ====
 +
 +****
 +
 +  sudo rm -f '/etc/nginx/sites-enabled/media.conf' && sudo ln -s '/etc/nginx/sites-available/media.conf' '/etc/nginx/sites-enabled' && sudo systemctl reload 'nginx'
 +
 +====== Initial Setup ======
 +
 +  * https://media.realmofespionage.xyz
 +
 +====== Services ======
 +
 +===== Updater =====
 +
 +==== Service ====
 +
 +  sudo -e '/etc/systemd/system/media-up.service'
 +
 +<code>
 +[Service]
 +User=www-data
 +Group=www-data
 +Type=oneshot
 +WorkingDirectory=/var/www/media
 +Environment="COMPOSER_CACHE_DIR=/dev/null"
 +ExecStart='/usr/bin/git' -C '/var/www/media' pull origin 'master'
 +ExecStart='/usr/bin/git' -C '/var/www/media/Lychee-front' pull origin 'master'
 +ExecStart='/usr/bin/composer' update --working-dir='/var/www/media'
 +ExecStartPost='/bin/sync'</code>
 +
 +==== Timer ====
 +
 +  * Every day at ''05:00:00''
 +
 +  sudo -e '/etc/systemd/system/media-up.timer' && sudo systemctl daemon-reload && sudo systemctl enable 'media-up.timer' --now && sudo systemctl start 'media-up' && sudo systemctl status 'media-up' -l
 +
 +<code>
 +[Unit]
 +Description=Lychee Updater
 +After=network-online.target
 +Wants=network-online.target
 +
 +[Timer]
 +OnCalendar=*-*-* 05:00:00
 +Persistent=true
 +
 +[Install]
 +WantedBy=timers.target</code>
 +
 +===== Maintenance =====
 +
 +==== Service ====
 +
 +  sudo -e '/etc/systemd/system/media-m.service'
 +
 +<code>
 +[Service]
 +User=www-data
 +Group=www-data
 +Type=oneshot
 +ExecStart='/usr/bin/git' -C '/var/www/media' gc --aggressive --prune='all'
 +ExecStart='/usr/bin/git' -C '/var/www/media' fsck --full --strict
 +ExecStart='/usr/bin/git' -C '/var/www/media/Lychee-front' gc --aggressive --prune='all'
 +ExecStart='/usr/bin/git' -C '/var/www/media/Lychee-front' fsck --full --strict
 +ExecStartPost='/bin/sync'</code>
 +
 +==== Timer ====
 +
 +  * ''01'' day of every month at ''05:20:00''
 +
 +  sudo -e '/etc/systemd/system/media-m.timer' && sudo systemctl daemon-reload && sudo systemctl enable 'media-m.timer' --now
 +
 +<code>
 +[Unit]
 +Description=Lychee Maintenance
 +After=network-online.target
 +Wants=network-online.target
 +
 +[Timer]
 +OnCalendar=*-*-01 05:20:00
 +Persistent=true
 +
 +[Install]
 +WantedBy=timers.target</code>
 +
 +===== Backup =====
 +
 +==== Files ====
 +
 +=== Service ===
 +
 +  mkdir -p ~/'backups' && sudo -e '/etc/systemd/system/media-fb.service' && sudo sed -i 's/CHANGEME/'$USER'/g' '/etc/systemd/system/media-fb.service'
 +
 +  [Service]
 +  Type=oneshot
 +  WorkingDirectory=/var/www
 +  ExecStart='/bin/bash' -c '"/bin/tar" -cvzf "/home/CHANGEME/backups/lychee-files-auto-"$$(date +%%Y-%%m-%%d)".tar.gz" "media"'
 +  ExecStartPost='/bin/sync'
 +
 +=== Timer ===
 +
 +  * ''01'' day of every month at ''05:50:00''
 +
 +  sudo -e '/etc/systemd/system/media-fb.timer' && sudo systemctl daemon-reload && sudo systemctl enable 'media-fb.timer' --now && sudo systemctl start 'media-fb' && sudo systemctl status 'media-fb' -l
 +
 +<code>
 +[Unit]
 +Description=Lychee Files Backup
 +
 +[Timer]
 +OnCalendar=*-*-01 05:50:00
 +Persistent=true
 +
 +[Install]
 +WantedBy=timers.target</code>
 +
 +==== Database ====
 +
 +=== Database Auth ===
 +
 +  sudo -e '/var/lib/mysqlauth/lychee' && sudo chmod '600' '/var/lib/mysqlauth/lychee'
 +
 +  [mysqldump]
 +  user=lychee
 +  password=x
 +
 +=== Service ===
 +
 +  mkdir -p ~/'backups' && sudo mkdir -p '/var/lib/mysqltmp' && sudo -e '/etc/systemd/system/media-db.service' && sudo sed -i 's/'CHANGEME'/'$USER'/g' '/etc/systemd/system/media-db.service'
 +
 +  [Service]
 +  Type=oneshot
 +  WorkingDirectory=/var/lib/mysqltmp
 +  ExecStartPre='/usr/bin/mysqldump' --defaults-extra-file='/var/lib/mysqlauth/lychee' --single-transaction 'lychee' -r '/var/lib/mysqltmp/lychee.sql'
 +  ExecStart='/bin/gzip' -f '/var/lib/mysqltmp/lychee.sql'
 +  ExecStart='/bin/bash' -c '"/bin/mv" "/var/lib/mysqltmp/lychee.sql.gz" "/home/CHANGEME/backups/lychee-database-auto-"$$(date +%%Y-%%m-%%d)".sql.gz"'
 +  ExecStartPost='/bin/sync'
 +
 +=== Timer ===
 +
 +  * Every day at ''05:15:00''
 +
 +  sudo -e '/etc/systemd/system/media-db.timer' && sudo systemctl daemon-reload && sudo systemctl enable 'media-db.timer' --now && sudo systemctl start 'media-db' && sudo systemctl status 'media-db' -l
 +
 +<code>
 +[Unit]
 +Description=Lychee Database Backup
 +After=mariadb.service
 +
 +[Timer]
 +OnCalendar=*-*-* 05:15:00
 +Persistent=true
 +
 +[Install]
 +WantedBy=timers.target</code>
 +
 +====== Backup ======
 +
 +  * Create backup archive on server and transfer to client computer
 +
 +===== Server =====
 +
 +==== Stop Services ====
 +
 +****
 +
 +  sudo systemctl stop nginx php7.2-fpm
 +
 +==== Backup Folder ====
 +
 +****
 +
 +  cd '/var/www' && sudo tar -cvzf ~/'lychee-files-manual-'$(date +%Y-%m-%d)'.tar.gz' 'media' && cd ~ && sync
 +
 +==== Backup Database ====
 +
 +****
 +
 +  sudo mysqldump --defaults-extra-file='/var/lib/mysqlauth/lychee' --single-transaction 'lychee' -r ~/'lychee-database-manual-'$(date +%Y-%m-%d)'.sql' && sync
 +
 +==== Start Services ====
 +
 +****
 +
 +  sudo systemctl start nginx php7.2-fpm
 +
 +===== Client =====
 +
 +==== Transfer Files To Client ====
 +
 +****
 +
 +  scp espionage724@192.168.1.153:~/'lychee-files-'*'.tar.gz' espionage724@192.168.1.153:~/'lychee-database-'*'.sql' ~/'Downloads' && sync
 +
 +====== Restore ======
 +
 +===== Client =====
 +
 +==== Uncompress Database ====
 +
 +  * This is only needed if restoring an **automated** database backup ((manual doesn't gzip))
 +
 +  gunzip ~/'Downloads/lychee-database-'*'.sql.gz'
 +
 +==== Transfer Files To Server ====
 +
 +****
 +
 +  scp ~/'Downloads/lychee-files-'*'.tar.gz' ~/'Downloads/lychee-database-'*'.sql' espionage724@192.168.1.153:~
 +
 +==== Remove Files ====
 +
 +****
 +
 +  rm -f ~/'Downloads/lychee-files-'*'.tar.gz' ~/'Downloads/lychee-database-'*'.sql' && sync
 +
 +===== Server =====
 +
 +==== Stop Services ====
 +
 +****
 +
 +  sudo systemctl stop nginx php7.2-fpm
 +
 +==== Remove Previous Folder ====
 +
 +****
 +
 +  sudo rm -Rf '/var/www/media'
 +
 +==== Restore Lychee Folder ====
 +
 +****
 +
 +  cd '/var/www' && sudo tar -xvzf ~/'lychee-files-'*'.tar.gz' 'media' && sudo chown -R 'www-data':'www-data' '/var/www/media' && cd ~ && sync
 +
 +==== Drop Previous Database ====
 +
 +  sudo mysql
 +
 +  DROP DATABASE lychee;
 +
 +  FLUSH TABLES;
 +
 +==== Re-create Databases ====
 +
 +  sudo mysql
 +
 +  CREATE DATABASE lychee;
 +
 +==== Restore Database ====
 +
 +****
 +
 +  sudo mysql 'lychee' < ~/'lychee-database-'*'.sql' && sync
 +
 +==== Reapply Permissions ====
 +
 +  sudo mysql
 +
 +  GRANT ALL PRIVILEGES ON lychee.* to 'lychee'@'localhost' IDENTIFIED BY 'x';
 +
 +  FLUSH PRIVILEGES;
 +
 +==== Start Services ====
 +
 +****
 +
 +  sudo systemctl start nginx php7.2-fpm
 +
 +==== Remove Backups ====
 +
 +  * Verify that Lychee works before running
 +
 +  rm ~/'lychee-files-'*'.tar.gz' ~/'lychee-database-'*'.sql' && sync
servers/nginx/lychee.txt ยท Last modified: 2018/12/31 04:19 by Sean Rhone