Skip to content

Instantly share code, notes, and snippets.

@dreamorosi
Last active April 6, 2023 15:22
Show Gist options
  • Save dreamorosi/828861e850bbb14893ae144c28f39e0e to your computer and use it in GitHub Desktop.
Save dreamorosi/828861e850bbb14893ae144c28f39e0e to your computer and use it in GitHub Desktop.
Install and configure webserver based on nginx with php 7 and fpm over socket configured.

Install and configure webserver based on nginx with php 7 and fpm over socket configured plus a couple of utils.

While launching a new instance open the section Advanced details in step 3. Configure Instance paste this code that will clone this gist and run the install commands.

#!/bin/bash
yum install git -y
git clone https://gist.github.com/828861e850bbb14893ae144c28f39e0e.git /home/ec2-user/install
chmod a+x install/install.sh
bash /home/ec2-user/install/install.sh

Available commands:

  • sudo service nginx start|stop|status
  • sudo service php-fpm start|stop|status
  • bash /home/ec2-user/clean.sh
  • /home/ec2-user/install/certbot-auto --nginx --debug
  • sudo mysql_secure_installation

###Todo

  • Add public-ip and nvm to bash
#!/bin/bash
sudo rm -rf ./install
sed -i '$ d' .bashrc && source .bashrc
rm -- "$0"
#!/bin/bash
wget -O /var/www/html/phpMyAdmin.tar.gz https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
mkdir /var/www/html/phpMyAdmin && tar xf /var/www/html/phpMyAdmin.tar.gz -C /var/www/html/phpMyAdmin --strip-components 1
rm /var/www/html/phpMyAdmin.tar.gz
randomBlowfishSecret=`openssl rand -base64 32`;
sed -e "s|cfg\['blowfish_secret'\] = ''|cfg['blowfish_secret'] = '$randomBlowfishSecret'|" /var/www/html/phpMyAdmin/config.sample.inc.php > /var/www/html/phpMyAdmin/config.inc.php
echo "\$cfg['TempDir'] = '/var/www/tmp/';" >> /var/www/html/phpMyAdmin/config.inc.php
sudo service nginx restart
#!/bin/bash
sudo yum update -y
sudo yum install -y nginx mysql56-server php70 php70-fpm php70-devel php70-pdo php70-mbstring php70-cli php70-odbc php70-imap php70-gd php70-xml php70-soap php70-opcache php70-odbc php70-mysqli php70-mysqlnd php70-zip -y
sudo chkconfig nginx on
sudo chkconfig php-fpm on
sudo chkconfig mysqld on
sudo service nginx start
sudo service mysqld start
public_ip=$( dig +short myip.opendns.com @resolver1.opendns.com )
sed -r 's/(\b[0-9]{1,3}\.){3}[0-9]{1,3}\b'/$public_ip/ -i /home/ec2-user/install/server.conf
sudo mv /home/ec2-user/install/server.conf /etc/nginx/conf.d/
sudo chmod a+x /home/ec2-user/install/php-ini-patch.sh
bash /home/ec2-user/install/php-ini-patch.sh
sudo chmod a+x /home/ec2-user/install/www-conf-patch.sh
bash /home/ec2-user/install/www-conf-patch.sh
sudo usermod -a -G nginx ec2-user
mkdir /var/www
mkdir /var/www/html
mkdir /var/www/tmp
mkdir /var/www/phpMyAdmin
chown -R ec2-user:nginx /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
sudo chown -R root:nginx /var/lib/php/7.0/
find /var/lib/php/7.0/ -type d -exec sudo chmod 2775 {} \;
find /var/lib/php/7.0/ -type f -exec sudo chmod 0664 {} \;
sudo mv /home/ec2-user/install/clean.sh /home/ec2-user/
chmod a+x /home/ec2-user/clean.sh
chown ec2-user:ec2-user /home/ec2-user/clean.sh
wget -O /home/ec2-user/install/certbot-auto https://dl.eff.org/certbot-auto
chmod a+x /home/ec2-user/install/certbot-auto
echo "printf \"\nInstallation completed successfully!\n\nRun bash clean.sh or follow the url for more info on the configuration\n\nhttps://gist.github.com/dreamorosi/828861e850bbb14893ae144c28f39e0e.\n\n\"" >> /home/ec2-user/.bashrc
sudo reboot
#!/bin/bash
-i 's/\;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini
service nginx restartsed
#chmod -R a+w /var/www/html/adserver/var
#chmod -R a+w /var/www/html/adserver/plugins
#chmod -R a+w /var/www/html/adserver/www/admin/plugins
#chmod -R a+w /var/www/html/adserver/www/images
# /etc/nginx/conf.d/server.conf
server {
root /var/www/html/;
index index.php;
server_name 127.0.0.1;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 or socket
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php-fpm:
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#!/bin/bash
sed -i 's/apache/nginx/g' /etc/php-fpm-7.0.d/www.conf
sed -i 's/127.0.0.1:9000/\/var\/run\/php-fpm\/php-fpm.sock/g' /etc/php-fpm-7.0.d/www.conf
sed -i 's/nobody/nginx/g' /etc/php-fpm-7.0.d/www.conf
sed -i 's/0660/0664/g' /etc/php-fpm-7.0.d/www.conf
sed -i 's/;listen.owner/listen.owner/g' /etc/php-fpm-7.0.d/www.conf
sed -i 's/;listen.group/listen.group/g' /etc/php-fpm-7.0.d/www.conf
sed -i 's/;listen.mode/listen.mode/g' /etc/php-fpm-7.0.d/www.conf
service php-fpm restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment