Host Multiple Websites on Raspberry PI

Raspberry PI 3/B does not have the compute capacity or resources to run multiple sites for production environment. This is a tutorial for at home experimental purposes only. You can probably get away with one personal very low traffic WordPress site on Raspberry PI.

I assume you have access to update the domain names DNS records with your public IP.

This tutorial is written for Raspbian release 9.3, Apache2, PHP 7.0

To find your Raspbian release number:
lsb_release -a

1- Install fresh Raspbian Lite, at startup of your Raspberry PI click Shift when prompt. Select Raspbian Lite and click Install.

2- Login ID: pi Password: raspberry

3- Change the default password
passwd

4- Enbale SSH on boot
sudo systemctl enable ssh

5- Run update
sudo apt-get update

6- Install Apache and PHP
sudo apt-get install apache2 -y
sudo apt-get install php -y
sudo service apache2 restart

7 – Test your web server. First, let\’s get your IP address. Write down your IP address
ifconfig

From another computer connected to your internal network, open a browser and enter your Raspberry PI IP address. You should get Apache default page.

8- Create port forwarding on your router. Forward port 80 (http://) to your Raspberry PI IP address. If you are doing SSL (https://) which is covered here then forward port 443 as well to your Raspberry IP address. Check your router manual for instructions.

9- Lookup your public IP address. The easiest way, from a computer attached to your network Google \”What is my IP\”, take note of your public IP address and update your domain names DNS records. Please follow your provider\’s instruction on how to update your A record for all sites that you will be setting up on your Raspberry PI.
10 – Let\’s test PHP
cd /var/www/html/sudo nano index.php
<?phpphpinfo();?>

Use CTRL+O to save file and CTRL-X to exit. Let\’s delete the Apache default test page.

sudo rm index.html

Again, from another computer open a browser and enter your Raspberry PI IP address. You should get a bunch of PHP stuff page.

In some cases for some crazy reasons with Raspbian, PHP shows the code instead of page. Try this to fix the issue:sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php7.0-fpm
sudo service apache2 restart

Test URL again and see if PHP now works.

11- Install MySQLsudo apt-get install mysql-server php-mysql -y
sudo service apache2 restart

12- Let\’s start building our sites. For the demonstration purposes, will call the sites Example1.com, Example2.com and Example3.com. I will name the folders without \”.com\” – it is not preferred method in production environment. Will keep our Apache default site (which is the PHP info page) for now. This will help us in case of bump into directive failure. Let\’s do it.

cd /var/www/sudo mkdir example1sudo mkdir example2sudo mkdir example3

13- Let\’s install WordPress on all three test sitessudo wget http://wordpress.org/latest.tar.gz
sudo tar xzf latest.tar.gz
sudo cp -r wordpress/* /var/www/example1/sudo cp -r wordpress/* /var/www/example2/sudo cp -r wordpress/* /var/www/example3/

14- Change ownership of all these files to the Apache user. I like to do it from inside the directory itself.

cd /var/www/example1/sudo chown -R www-data: .cd /var/www/example2/sudo chown -R www-data: .cd /var/www/example3/sudo chown -R www-data: .

15- Secure MySQL. Answer yes to all the question EXCEPT when asked about disallow remote access, this will be NO. We will later install phpMyadmin to manage your databases through a web portal.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]: Y
  • Reload privilege tables now? [Y/n]: Y

16- Let\’s create the databases manually.sudo mysql -uroot -p
create database example1;
GRANT ALL PRIVILEGES ON example1.* TO \’root\’@\’localhost\’ IDENTIFIED BY \’ROOT_PASSWORD_HERE\’;


FLUSH PRIVILEGES;

create database example2;
GRANT ALL PRIVILEGES ON example2.* TO \’root\’@\’localhost\’ IDENTIFIED BY \’ROOT_PASSWORD_HERE\’;


FLUSH PRIVILEGES;

create database example3;
GRANT ALL PRIVILEGES ON example3.* TO \’root\’@\’localhost\’ IDENTIFIED BY \’ROOT_PASSWORD_HERE\’;
FLUSH PRIVILEGES;

Use CTRL-D to exit

sudo a2enmod rewrite
sudo systemctl restart apache2

17 – Now let\’s loosen up Apache and do some configuration. Tell the virtual host serving the sites to allow requests to be overwritten.

IMPORTANT – Add the following lines after line 1. right after <VirtualHost *:80>Will make this change in the default site configuration file then will copy this file and edit them for the other 3 sites.

sudo nano /etc/apache2/sites-available/000-default.conf
<Directory \”/var/www/html\”>
AllowOverride All
</Directory>sudo service apache2 restart

18- Create the configuration files for Example1, Example2 and Example 3 sites

cd /etc/apache2/sites-available

FOR EXAMPLE1.COM
sudo cp 000-default.conf example1.com.conf
sudo nano example1.com.conf
change html to example1
<VirtualHost *:80>
<Directory \”/var/www/example1\”>
AllowOverride All
</Directory>
:
:
ServerName www.example1.com
ServerAlias example1.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example1

SAVE AND EXIT (CTRL-O, enter, CTRL D)

Create new .htaccess if does not exist and add the below content
cd /var/www/example1/
sudo nano .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

SAVE AND EXIT (CTRL-O, enter, CTRL D)
sudo a2ensite example1.com.conf

FOR EXAMPLE2.COM
cd /etc/apache2/sites-available
sudo cp 000-default.conf example2.com.conf
sudo nano example2.com.conf

change html to example2
<VirtualHost *:80>
<Directory \”/var/www/example2\”>
AllowOverride All
</Directory>
:
:
ServerName www.example2.com
ServerAlias example2.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example2

SAVE AND EXIT (CTRL-O, enter, CTRL D)

Create new .htaccess if does not exist and add the below content
cd /var/www/example2/
sudo nano .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

SAVE AND EXIT (CTRL-O, enter, CTRL D)
sudo a2ensite example2.com.conf
FOR EXAMPLE3.COM
cd /etc/apache2/sites-available
sudo cp 000-default.conf example3.com.conf
sudo nano example3.com.conf

change html to example3
<VirtualHost *:80>
<Directory \”/var/www/example3\”>
AllowOverride All
</Directory>
:
:
ServerName www.example3.com
ServerAlias example3.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example3

SAVE AND EXIT (CTRL-O, enter, CTRL D)

Create new .htaccess if does not exist and add the below content
cd /var/www/example3/
sudo nano .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

SAVE AND EXIT (CTRL-O, enter, CTRL D)
sudo a2ensite example3.com.conf

Time to test your sites. From another computer test your sites, Example1.com, Example2.com and Example3.com

Troubleshooting tips: If you get page not found errors, some routers/cable modems do not allow loopback. Try to access your sites from external computer or phone – external means not attached to your network. For example, turn off your WiFi on your phone and rerun your test.
19- I think it is about time to install phpMyadmin.
IMPORTANT: during installation you will be prompt to enter MySQL password so that the installer can create a database for phpMyadmin. Also, you will be prompt to choose your web server (DO NOT PRESS ENTER), I repeat, (DO NOT PRESS ENTER), highlight Apache, press the SPACE bar to select Apache (Asterisk will show) , then now you can press Enter.
sudo apt-get install phpmyadmin -y
sudo apt-get install php-mbstring
sudo apt-get install php-gettext
sudo mysql -uroot -p
GRANT ALL PRIVILEGES ON phpmyadmin.* TO \’root\’@\’localhost\’ IDENTIFIED BY \’ROOT_PASSWORD_HERE\’;
FLUSH PRIVILEGES;
sudo systemctl restart apache2Test phpMyadmin by going to http://example1.com/phpmyadmin, http://example2.com/phpmyadmin and http://example3.com/phpmyadmin

20- If you want to disable Apache default page – html folder which currently contains the PHP info
sudo a2dissite 000-default.conf
sudo systemctl restart apache221- If you are not too happy with the default PHP configuration like most of us do, you can modify the ini files and enable modules and increase max sizes of upload and posts limitations

, etc.
sudo nano /etc/php/7.0/apache2/php.ini
change:
upload_max_filesize = 2M
post_max_size = 2M
sudo nano /etc/php/7.0/cli/php.ini
change
upload_max_filesize = 2M
post_max_size = 2M
sudo systemctl restart apache2


upload_max_filesize = 2M
post_max_size = 2M
sudo systemctl restart apache2

22- If you want to enable FTPsudo apt-get install vsftpd
sudo nano /etc/vsftpd.conf
uncomment:
write_enable=YES
local_umask=022 if your users are expecting 022
add the following lines:
pasv_enable=Yes
pasv_min_port=40000
pasv_max_port=40100
sudo service vsftpd restart

Scroll to Top