Skip to content

Instantly share code, notes, and snippets.

@Taidgh
Forked from isc30/install.bash
Last active April 1, 2018 19:18
Show Gist options
  • Save Taidgh/8a44217964078b63bd44b46bd04613e6 to your computer and use it in GitHub Desktop.
Save Taidgh/8a44217964078b63bd44b46bd04613e6 to your computer and use it in GitHub Desktop.
Raspberry Pi Install PHP7 + Nginx + MySQL + PhpMyAdmin (last versions)
#!/bin/bash
# Thanks to https://gist.github.com/Lewiscowles1986/ce14296e3f5222082dbaa088ca1954f7
# To install
# Setup
# wget https://gist.githubusercontent.com/Taidgh/8a44217964078b63bd44b46bd04613e6/raw/setup.bash
# sudo bash setup.bash
if [ "$(whoami)" != "root" ]; then
echo "Run script as ROOT please. (sudo !!)"
exit
fi
echo "deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi" > /etc/apt/sources.list.d/stretch.list
echo "APT::Default-Release \"stretch\";" > /etc/apt/apt.conf.d/99-default-release
apt-get update -y
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get install -y rpi-update
apt-get install -t stretch -y php7.0 php7.0-fpm php7.0-cli php7.0-opcache php7.0-mbstring php7.0-curl php7.0-xml php7.0-gd php7.0-mysql
apt-get install -t stretch -y nginx
update-rc.d nginx defaults
update-rc.d php7.0-fpm defaults
sed -i 's/^;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/7.0/fpm/php.ini
sed -i 's/# server_names_hash_bucket_size/server_names_hash_bucket_size/' /etc/nginx/nginx.conf
cat > /etc/nginx/sites-enabled/default << "EOF"
# Default server
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name dev.pi;
root /var/www/dev.pi/public;
index index.php index.html index.htm default.html;
location / {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# optimize static file serving
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 30d;
}
# deny access to .htaccess files, should an Apache document root conflict with nginx
location ~ /\.ht {
deny all;
}
}
# ofcourseitalian.pi server configuration
server {
listen 80;
listen [::]:80;
server_name ofcourseitalian.pi www.ofcourseitalian.pi;
root /var/www/ofcourseitalian.pi/public;
index index.php index.html index.htm default.html;
location / {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# optimize static file serving
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 30d;
}
# deny access to .htaccess files, should an Apache document root conflict with nginx
location ~ /\.ht {
deny all;
}
}
EOF
mkdir -p /var/www/dev.pi/public
cat > /var/www/dev.pi/public/index.php << "EOF"
<?php
class Application
{
public function __construct()
{
echo 'Done. Your websites is at /var/www/dev.pi/public/';
}
}
$application = new Application();
EOF
mkdir -p /var/www/ofcourseitalian.pi/public && cd $_
#install Wordpress.
rm *
wget http://wordpress.org/latest.tar.gz
tar xzf latest.tar.gz
mv wordpress/* .
rm -rf wordpress latest.tar.gz
cd /var/www/ofcourseitalian.pi/public
chown -R www-data: .
sudo rm -rf /var/www/html
usermod -a -G www-data pi
chown -R pi:www-data /var/www
chgrp -R www-data /var/www
chmod -R g+rw /var/www
setfacl -d -R -m g::rw /var/www
apt-get -y autoremove
service nginx restart
service php7.0-fpm restart
# MySQL
apt-get -t stretch -y install mysql-server
read -s -p "Type the password you just entered (MySQL): " mysqlPass
mysql --user="root" --password="$mysqlPass" --database="mysql" --execute="GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$mysqlPass'; FLUSH PRIVILEGES;"
sed -i 's/^bind-address/#bind-address/' /etc/mysql/mysql.conf.d/mysqld.cnf
sed -i 's/^skip-networking/#skip-networking/' /etc/mysql/mysql.conf.d/mysqld.cnf
service mysql restart
# PhpMyAdmin
read -p "Do you want to install PhpMyAdmin? <y/N> " prompt
if [ "$prompt" = "y" ]; then
apt-get install -t stretch -y phpmyadmin
ln -s /usr/share/phpmyadmin /var/www/dev.pi/public
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf " \n http://%s/phpmyadmin to enter PhpMyAdmin \n \n""$_IP"
fi
fi
apt-get -y autoremove
#Create the DB for WP
read -p "Do you want to create a Database?? <y/N> " createdb
if [ "$createdb" = "y" ]; then
set -e
echo "Type the name for your database, followed by [ENTER]:"
read DB
echo "Type the username for your database, followed by [ENTER]:"
read USR
echo "Type the password for your new user, followed by [ENTER]:"
read PASS
mysql -uroot <<MYSQL_SCRIPT
CREATE DATABASE $DB;
CREATE USER '$USR'@'localhost' IDENTIFIED BY '$PASS';
GRANT ALL PRIVILEGES ON $DB.* TO '$USR'@'localhost';
FLUSH PRIVILEGES;
MYSQL_SCRIPT
echo "MySQL user created."
echo "Database: $DB"
_IP=$(hostname -I)
echo " Log in at: http://$_IP/phpmyadmin"
echo " Username: $USR"
echo " Password: $PASS"
echo " These details are saved in $DB-details.txt"
cat > $DB-details.txt << EOF
Your login details for the Database '$DB' is:
Username: $USR
Password: $PASS
You can login to your database at http://$_IP/phpmyadmin
EOF
fi
# Further instructions
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "You can now access your site at http://%s\n" "$_IP"
printf "Your Database admin is at http://%s/PhpMyAdmin\n" "$_IP"
printf "You still need to edit the hosts file on your computer to add:\n%s dev.pi\n" "$_IP"
printf "%s ofcourseitalian.pi\n" "$_IP"
printf " Going to reboot now! \n \n"
fi
sleep 20
reboot
@Taidgh
Copy link
Author

Taidgh commented Mar 31, 2018

SD card flashed with 2018-03-13-raspbian-stretch-lite.
It seems that the packages are missing eg:
E: Unable to locate package nginx
Need to reboot and run the script again. Will review for next .img of stretch

Not sure if you can upgrade, reboot then continue script?

@Taidgh
Copy link
Author

Taidgh commented Apr 1, 2018

Need to add:
apt-get install ssmtp -y sudo apt-get install mailutils -y sudo apt-get install mpack
and add creds to
/etc/ssmtp/ssmtp.conf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment