Skip to content

Instantly share code, notes, and snippets.

@AtsushiA
Created February 12, 2017 15:42
Show Gist options
  • Save AtsushiA/189f78efd0024f483eb92391fedd2a86 to your computer and use it in GitHub Desktop.
Save AtsushiA/189f78efd0024f483eb92391fedd2a86 to your computer and use it in GitHub Desktop.
LAMP on AWS (PHP7.1)
#!/bin/bash
#set -ex
## Set sqlpassword
MYSQL_PASSSWORD="$(curl http://169.254.169.254/latest/meta-data/instance-id)"
## Set Timezon
ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
sed -i -e "s/ZONE=.*$/ZONE=\"Asia\/Tokyo\"/" /etc/sysconfig/clock
sed -i -e "s/UTC=.*$/UTC=false/" /etc/sysconfig/clock
echo 'ARC=false' >> /etc/sysconfig/clock
service crond restart
## Set Locale
echo \
'LANG=ja_JP.UTF-8
LC_CTYPE=ja_JP.UTF-8'\
>/etc/sysconfig/i18n
## update
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum update -y
## install apache
yum groupinstall -y "Web Server"
## Set apache setting
sed -e 's/AddDefaultCharset UTF-8/AddDefaultCharset Off/g' /etc/httpd/conf/httpd.conf
sed '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride all/' /etc/httpd/conf/httpd.conf
sed -i -e 's/User apache/User ec2-user/g' /etc/httpd/conf/httpd.conf
sed -i -e 's/Group apache/Group apache/g' /etc/httpd/conf/httpd.conf
## Create TestFiles
echo '<?php phpinfo(); ?>' > /var/www/html/index.php
## Set VirtualHost User Group & Permission
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www/
chmod g+s -R ec2-user:apache /var/www/
find /var/www/ -type d -exec chmod 755 {} +
find /var/www/ -type f -exec chmod 644 {} +
## install php
yum install -y --enablerepo=remi-php71 --disablerepo=amzn-main php
yum install -y --enablerepo=remi-php71 --disablerepo=amzn-main php-pdo php-mbstring php-pecl-zip
#install libwebp
yum install -y --disablerepo=amzn-main --enablerepo=epel libwebp
yum install -y libmcrypt
yum install -y libtool-ltdl gd-last enchant
yum install -y --enablerepo=remi-php71 --disablerepo=amzn-main php-xml php-bcmath php-opcache php-mysqlnd php-pecl-apc
## Set php setting
sed -i "s/^;date.timezone =$/date.timezone = \"Asia\/Tokyo\"/" /etc/php.ini
## Service Start & Set apache
service httpd restart
chkconfig httpd on
## MySQL
yum groupinstall -y "MySQL Database"
## we'll install 'expect' to input keystrokes/y/n/passwords
yum install -y expect
## Set Set
service mysqld start
SECURE_MYSQL=$(expect -c "
set timeout 10
spawn mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"\r\"
expect \"Set root password?\"
send \"y\r\"
expect \"New password:\"
send \"$MYSQL_PASSSWORD\r\"
expect \"Re-enter new password:\"
send \"$MYSQL_PASSSWORD\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"y\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
")
echo "Done : SECURE_MYSQL Settings"
## Remove 'expect' to input keystrokes/y/n/passwords
yum remove -y expect
## Service Start &Set MySQL
service mysqld start
chkconfig mysqld on
## Create swap files so we need to make one
## adding line to fstab so it's enabled on boot
dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
chmod 0600 /swapfile
swapon /swapfile
sed -i '$ a\/swapfile swap swap defaults 0 0' /etc/fstab
## Done & Reboot
shutdown -r now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment