Skip to content

Building

Hayden Schiff edited this page Apr 14, 2024 · 3 revisions

This page documents how to build the site for production use. You need to have already done the initial setup before building.

Build script

Here is the script used to build the site in production. The script creates a copy of the project (as sportsarchive-next/), updates that copy, then moves it back to sportsarchive/. The previous un-updated copy of the project is moved to sportsarchive-prev/, and is not deleted until the next time that the build script is run (allowing it to be restored in case of a failed deploy).

#!/usr/bin/env bash
rm -rf sportsarchive-prev/
cp -r sportsarchive/ sportsarchive-next/
cd sportsarchive-next/
git pull
sudo chmod -R 777 var/cache/ var/log/
symfony composer install --no-dev --optimize-autoloader
yarn install
yarn icons
yarn encore production
sudo chown -R www-data:www-data var/cache/ var/log/
symfony console doctrine:migrations:migrate
symfony console cache:clear
cd ..
mv sportsarchive/ sportsarchive-prev/
mv sportsarchive-next/ sportsarchive/
systemctl daemon-reload
systemctl restart sportsarchive-messenger@*.service

Web server config

I use Apache as my web server, and Certbot to automatically acquire SSL certificates.

Here is my site config file for Apache:

<Macro sportsarchive>
	DocumentRoot /opt/sportsarchive/public
	<Directory "/opt/sportsarchive/public">
		Options -Indexes +MultiViews
		AllowOverride None
		Require all granted

		FallbackResource /index.php
	</Directory>

	php_value upload_max_filesize 1000M
	php_value post_max_size 1000M

	Header unset Server
	ServerSignature Off

	RewriteEngine on
</Macro>
<VirtualHost *:80>
	ServerName www.sportsarchive.net
	ServerAlias sportsarchive.net

	Use sportsarchive

	RewriteCond %{SERVER_NAME} =www.sportsarchive.net [OR]
	RewriteCond %{SERVER_NAME} =sportsarchive.net
	RewriteRule ^ https://www.sportsarchive.net%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
	ServerName www.sportsarchive.net
	ServerAlias sportsarchive.net

	Use sportsarchive

        RewriteCond %{SERVER_NAME} =sportsarchive.net
        RewriteRule ^ https://www.sportsarchive.net%{REQUEST_URI} [END,NE,R=permanent]

	Include /etc/letsencrypt/options-ssl-apache.conf
	SSLCertificateFile /etc/letsencrypt/live/sportsarchive.net/fullchain.pem
	SSLCertificateKeyFile /etc/letsencrypt/live/sportsarchive.net/privkey.pem
</VirtualHost>

Messenger service

You need to have one or more messenger workers running so that PDFs get automatically rebuilt. For production use, an easy way to run workers is with systemd; here is my config file, which I have installed at /etc/systemd/system/sportsarchive-messenger@.service:

[Unit]
Description=Symfony messenger-consume %i

[Service]
WorkingDirectory=/opt/sportsarchive
ExecStart=symfony console messenger:consume async --time-limit=3600
Restart=always
RestartSec=30

[Install]
WantedBy=default.target

You can then spin up any number of workers with systemctl start sportsarchive-messenger@1. Change 1 to 2, 3, 4, etc to make more workers (currently, I use 5 workers on the production server). You can configure systemd to automatically start the service with systemctl enable sportsarchive-messenger@1.

Image thumbnail server

The site uses imgproxy for creating thumbnail versions of headshots. In production, this is currently installed via Docker with docker pull darthsim/imgproxy:latest. A script at /root/start-imgproxy.sh is used to start the Docker container:

docker run -d -p 8080:8080 -it \
	-e "IMGPROXY_USE_S3=true" \
	-e "IMGPROXY_S3_REGION=us-east-1" \
	-e "IMGPROXY_MAX_SRC_RESOLUTION=100" \
	-e "AWS_ACCESS_KEY_ID=keygoeshere" \
	-e "AWS_SECRET_ACCESS_KEY=secretgoeshere" \
	-e "IMGPROXY_S3_ENDPOINT=nyc3.digitaloceanspaces.com" \
	darthsim/imgproxy > /dev/null

Cron is used to automatically start this script upon reboot; run crontab -e and add this line:

@reboot /root/start-imgproxy.sh
Clone this wiki locally