Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Raspberry Pi Install PHP7 + Nginx + MySQL + PhpMyAdmin (last versions)
#!/bin/bash
# Thanks to https://gist.github.com/Lewiscowles1986/ce14296e3f5222082dbaa088ca1954f7
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 \"jessie\";" > /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 _;
root /var/www/default/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;
}
}
# Zonaisc.com server configuration
server {
listen 80;
listen [::]:80;
server_name zonaisc.com www.zonaisc.com;
root /var/www/zonaisc.com/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/default/public
cat > /var/www/default/public/index.php << "EOF"
<?php
class Application
{
public function __construct()
{
phpinfo();
}
}
$application = new Application();
EOF
mkdir -p /var/www/zonaisc.com/public
cat > /var/www/zonaisc.com/public/index.php << "EOF"
<?php
class Application
{
public function __construct()
{
echo 'Hi ZonaIsc.com';
}
}
$application = new Application();
EOF
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/default/public
echo "http://192.168.XXX.XXX/phpmyadmin to enter PhpMyAdmin"
fi
apt-get -y autoremove
@isc30

This comment has been minimized.

Copy link
Owner Author

@isc30 isc30 commented Jul 17, 2016

To get it on the RaspberryPi:
wget https://gist.github.com/isc30/aa80d81df44de8a91dc0a82d55806381/raw/install.bash
sudo bash install.bash

@isc30

This comment has been minimized.

Copy link
Owner Author

@isc30 isc30 commented Aug 7, 2016

TODO: improve performance with this guide: https://tweaked.io/guide/nginx/

@isc30

This comment has been minimized.

Copy link
Owner Author

@isc30 isc30 commented Oct 16, 2016

Added default permissions

@BetaStacks

This comment has been minimized.

Copy link

@BetaStacks BetaStacks commented Jul 26, 2017

I get an error when I try to run this command.

root@piClone:/home/pi# mysql --user="root" --password="echoEchoMl99!" --database="mysql" --execute="GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$mysqlPass'; FLUSH PRIVILEGES;" ERROR 1133 (28000) at line 1: Can't find any matching row in the user table

I also get this message without ever being propted for a mysql password prior.
read -s -p "Type the password you just entered (MySQL): " mysqlPass

I presume this is why I'm not able to login to phpmyadmin

@meads85

This comment has been minimized.

Copy link

@meads85 meads85 commented Aug 9, 2017

What @BetaStacks said, what is the mysql password? the script does not let you choose one.

Edit: From the command line I run mysql_secure_installation and was able to reset the mysql password. I can login to mysql with the following command: sudo mysql -u root.

Is this bad practice, please let me know if it is. Thanks.

Everything is now working as it should be.

@RossosHEX

This comment has been minimized.

Copy link

@RossosHEX RossosHEX commented Sep 20, 2017

If I read correctly here: https://www.raspberrypi.org/forums/viewtopic.php?t=56703#p1206337
It seems it's more of the default packages/installation issues.
You type in "apt-get install mysql-server" and you get mariadb-server with unset password for the root account with no access from "localhost"

I wasn't using this script to setup my server, I was in process of googling my problems and I stumped upon this script.
Anyway, I needed the following for the myPhpAdmin to allow me to log into database.

mysql --user="root" --password="xxxMySqlSecretPassxxx" --database="mysql" --execute="GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxMySqlSecretPassxxx'; FLUSH PRIVILEGES;"

and then I had to input it again with modification regarding @'localhost'

mysql --user="root" --password="xxxMySqlSecretPassxxx" --database="mysql" --execute="GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'xxxMySqlSecretPassxxx'; FLUSH PRIVILEGES;"

Notice the dummy password, the console ate it up for the authentication in order to set up the password xD

Good fun! I hope this helps someone out there!

@loonix

This comment has been minimized.

Copy link

@loonix loonix commented Oct 17, 2017

very helpful @RossosHEX, cheers!

@sjuktstarkgrogg

This comment has been minimized.

Copy link

@sjuktstarkgrogg sjuktstarkgrogg commented Nov 1, 2017

I got a couple of errors while running this install script because I'm not running 'Jessie', I'm running the later version of raspbian:stretch lite.. and I would like to reverse it so that I delete everything that got installed. How would I proceed with this ?

@vbenner

This comment has been minimized.

Copy link

@vbenner vbenner commented Nov 22, 2017

Same problem for me.
JESSIE error (in french)
E: La valeur « jessie » n'est pas valable pour APT::Default-Release car cette version ne fait pas partie des sources disponibles.

@Novile

This comment has been minimized.

Copy link

@Novile Novile commented Jan 17, 2018

Solved for my Raspbian (Debian 9) Stretch
Written by install.bash, script line 10:
echo "APT::Default-Release "jessie";" > /etc/apt/apt.conf.d/99-default-release
After script execution:
folder opened: /etc/apt/apt.conf.d
and file of this folder: 99-default-release modified:
sudo nano /etc/apt/apt.conf.d/99-default-release
Text modified:
APT::Default-Release "jessie";
New:
APT::Default-Release "stretch";

@hsuanpai

This comment has been minimized.

Copy link

@hsuanpai hsuanpai commented Jun 7, 2018

I found an error when I execute the script on RPI3:
It may could be changed from as following 2 lines:
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
To:
sed -i 's/^bind-address/#bind-address/' /etc/mysql/mariadb.conf.d/50-server.cnf
sed -i 's/^skip-networking/#skip-networking/' /etc/mysql/mariadb.conf.d/50-server.cnf
And save as the new script.

@mikebkk

This comment has been minimized.

Copy link

@mikebkk mikebkk commented Aug 27, 2018

thank you very much!
After an update from ¨raspberry jessie¨ mpa did not work properly anymore.A fresh image did all I needed and this script worked flawlessly!
great

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