Skip to content

Instantly share code, notes, and snippets.

@aceraven777
Last active July 20, 2023 02:38
Show Gist options
  • Save aceraven777/32ec163ba29f36587d3386be51144d74 to your computer and use it in GitHub Desktop.
Save aceraven777/32ec163ba29f36587d3386be51144d74 to your computer and use it in GitHub Desktop.
Setup Server (PHP, nginx, SSL, supervisord, beanstalkd)
sudo su
# Set local timezone
timedatectl set-timezone Asia/Manila
yum update -y
# Search for nginx package
amazon-linux-extras | grep nginx
# replace 'nginx1' with whatever is outputed in the previous command
amazon-linux-extras install nginx1
# Search for php packages
amazon-linux-extras | grep php
# replace 'php8.0' with whatever version of php you want
amazon-linux-extras install php8.0
yum install -y git php-xml php-mbstring php-mysqlnd php-gd php-pecl-redis
service php-fpm start
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
#####################################
#####################################
### SETUP YOUR WEBSITE INSIDE /usr/share/nginx/html
#####################################
#####################################
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
vim /etc/nginx/nginx.conf
# Change the ff lines
user ec2-user;
...
http {
...
# Add sites-enabled folder for virtual hosts
include /etc/nginx/sites-enabled/*;
}
...
# Create nginx configuration site for the website
vim /etc/nginx/sites-available/website.com
...
server {
listen 80 default_server;
server_name website.com;
root /usr/share/nginx/html/site/public;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml;
gzip_disable "MSIE [1-6]\.";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
client_max_body_size 10M;
fastcgi_buffers 32 32k;
fastcgi_buffer_size 32k;
index index.php;
charset utf-8;
location / {
try_files $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; }
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(css|svg|gif|jpg|js|png|ico|otf|sng|xls|doc|exe|jpeg|tgx)$ {
access_log off;
expires max;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
...
ln -s /etc/nginx/sites-available/website.com /etc/nginx/sites-enabled/website.com
vim /etc/php-fpm.d/www.conf
# Change the ff lines
user = ec2-user
group = ec2-user
...
listen = /var/run/php-fpm/www.sock
...
listen.owner = ec2-user
listen.group = ec2-user
listen.mode = 0660
...
# COMMENT THIS ONE
;listen.acl_users = apache,nginx
systemctl start nginx
service php-fpm restart
chkconfig nginx on
chkconfig php-fpm on
chown -R ec2-user.ec2-user /usr/share/nginx/html
chown -R ec2-user:ec2-user /var/lib/nginx
# logout root user, go back to ec2-user
exit
cd ~
ln -s /usr/share/nginx/html html
sudo su
cd ~
systemctl stop nginx
wget -r --no-parent -A 'epel-release-*.rpm' https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm
yum-config-manager --enable epel*
yum install -y certbot
yum install -y python-certbot-nginx
# SSL START
certbot certonly --standalone --agree-tos -m it@qairos.asia -d website.com
# Setup cronjob to auto renew the SSL
crontab -e
...
0 1 * * * certbot renew --agree-tos -m it@qairos.asia --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"
...
# Add SSL cert to nginx configuration
vim /etc/nginx/sites-available/website.com
...
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name website.com;
root /usr/share/nginx/html/site/public;
ssl_certificate "/etc/letsencrypt/live/website.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/website.com/privkey.pem";
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml;
gzip_disable "MSIE [1-6]\.";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
client_max_body_size 10M;
fastcgi_buffers 32 32k;
fastcgi_buffer_size 32k;
index index.php;
charset utf-8;
location / {
try_files $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; }
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(css|svg|gif|jpg|js|png|ico|otf|sng|xls|doc|exe|jpeg|tgx)$ {
access_log off;
expires max;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
server {
listen 80 default_server;
server_name website.com;
return 301 https://$host$request_uri;
}
...
systemctl start nginx
sudo su
# SUPERVISORD and BEANSTALKD START
yum install -y supervisor beanstalkd
systemctl start beanstalkd
vim /etc/supervisord.conf
# At the bottom change/add the ff line
files = supervisord.d/*.conf
vim /etc/supervisord.d/website-worker.conf
...
[program:website-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /usr/share/nginx/html/site/artisan queue:work beanstalkd --name=website --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=ec2-user
group=ec2-user
numprocs=8
redirect_stderr=true
stdout_logfile=/usr/share/nginx/html/site/storage/logs/worker.log
stopwaitsecs=3600
...
systemctl start supervisord
supervisorctl reread
supervisorctl update
# Auto start on startup
systemctl enable supervisord
systemctl enable beanstalkd
# SUPERVISORD and BEANSTALKD END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment