|

Apache2 Web Server Setup

This is a simple chronological LAMP server setup. You can use it on a NUC/RasPI/ or Full-blown server. Check this out if you are interested in setting up Nginx instead of Apache .

Apache

Lets make sure our packages are up to date. Then install Apache.

sudo apt-get update
sudo apt-get upgrade

sudo apt install apache2 -y

You can check your install in a browser. Get your ip address by typing

hostname -I

User Setup

Now that your server is up and running, we need the ability to change files in the /var/www/html directory. This is where Apache serves files from.

Since we are doing this on a Raspberry Pi we add the user pi (or whatever our user is) to the www-data group, the default group for Apache2.

Then we allow ownership to all the files and folders in the /var/www/html directory to the www-data group.

sudo usermod -a -G www-data pi
sudo chown -R -f www-data:www-data /var/www/html

The following snippets are helpful for SFTP reading and writing to a directory in /var/www/…

... for sftp ...

//sudo chmod -R 0775 /var/www/html
//or
//chmod g+s /var/www/html So that new files inherit the group

sudo find /var/www/html -group www-data -exec chmod g+rwX {} \+
sudo find /var/www/html -type d -group www-data -exec chmod g+s {} \+

You will need to logout and log back in for changes to take effect.

Modifying HTML

You can now open files and properly modify them, so try it out.

nano /var/www/html/index.html

Adding PHP

Start by installing these packages to your server.

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-mbstring php7.4-mysql php7.4-curl php7.4-gd php7.4-zip -y
sudo nano /var/www/html/test.php
<?php
echo "I just installed PHP! ".date('Y-m-d H:i:s');
phpinfo();

Now open 192.your.ip.address/test.php to see if php is working. You should se todays date and a long list of info about what is installed.

Virtual Hosts

Virtual hosts are Apache’s way of handling multiple websites.

sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R www-data:www-data /var/www/example.com/public_html
sudo a2ensite example.com.conf
sudo systemctl reload apache2

MySQL

Install the package

sudo apt install mariadb-server
sudo apt install php-mysql
sudo mysql_secure_installation

Answer Y to all the questions and remember the password you create.

Now log into mysql.

sudo mysql -u root -p

*type “quit” or CRTL + D to exit the command line interface.

Now we can create a database

CREATE DATABASE testdb;

and create a user. Change the “username” and “password” values to something more relevant.

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON testdb.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

and finally PHPMyAdmin

sudo apt install phpmyadmin

Select the “apache2”  and then “Yes” at the configuring PHP prompt.

Then set a PHPMyAdmin Password, make it different, keep it secure. PHPMyAdmin by default will block you from logging into the PHPMyAdmin interface using the “root” user.

Log into mysql to create the new PHPMyAdmin user.

sudo mysql -u root -p
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
quit

Now lets configure the PHPMyAdmin install.

sudo nano /etc/apache2/apache2.conf

..and at the bottom of that file include ..

Include /etc/phpmyadmin/apache.conf

reload

sudo service apache2 restart

Now you can check out a login into your database

http://192.your.ip.address/phpmyadmin

To access this from the live internet, we will need to setup port forwarding and static ip but that is for another post.

References

https://superuser.com/questions/1399812/user-cannot-connect-via-sftp-to-var-www-html/1399858

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *