Setting up a LAMP server¶
The following uses the RaspberryPi website as its source reference RPiOrg-LAMP
Install Apache¶
sudo apt install apache2
[sudo] password for andrew:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libbrotli1 ssl-cert
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser openssl-blacklist
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libbrotli1 ssl-cert
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,262 kB of archives.
After this operation, 6,987 kB of additional disk space will be used.
Do you want to continue? [Y/n]
If this has worked, pointing a browser at the IP address of your server should result in the default apache homepage being displayed.
Todo
insert a screenshot image of the php page
Set up alternative html directory/directories
The default directory from which Apache will read its site data is /var/www/html. The initial Apache build point is set up to point at /var/www/html/index/html. This is not always convenient, for example because of space issues or fitting in with a backup regime.
For this example a different directory will be set up under the user’s home directory.
The apache mechanism to allow this is the ‘virtual host’ mechanism. By default apache comes set up with one ‘virtual host’, the configuration for which can be seen by running cat /etc/apache2/sites-available/000-default.conf, which should produce the following:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
So the default setting is for a virtual server with no name that is found at /var/www/html
To change the source you have to make four changes:
- change the DocumentRoot value to (for example) /mnt/raid0/36-Sphinx.
- add some definitions for Directory (not sure why, but it seems to be necessary.
- ensure that the www-data user has access to all files in the new directory
- restart apache (by running
sudo systemctl restart apache2)
The final /etc/apache2/sites-available/000-default.conf file should look like this (comments have been removed):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /mnt/raid0/36-Sphinx
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /mnt/raid0/36-Sphinx/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Additional information for this was taken from https://www.digitalocean.com/community/tutorials/how-to-move-an-apache-web-root-to-a-new-location-on-ubuntu-16-04.
There is a further article on setting up Multiple-Virtual-Hosts on a single machine which looks very interesting!
Install PHP¶
sudo apt install php
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libapache2-mod-php7.3 libsodium23 php-common php7.3 php7.3-cli php7.3-common php7.3-json php7.3-opcache php7.3-readline
Suggested packages:
php-pear
The following NEW packages will be installed:
libapache2-mod-php7.3 libsodium23 php php-common php7.3 php7.3-cli php7.3-common php7.3-json php7.3-opcache php7.3-readline
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,109 kB of archives.
After this operation, 14.4 MB of additional disk space will be used.
Do you want to continue? [Y/n]
To test that this is working a test php file is needed, and an additional link put in the index.html file to point to it.
So, my test.php file looks like:
<?php echo date('Y-m-d H:i:s'); ?>
<?php phpinfo(); ?>
… and my new entry in the index.html file is:
<li>Just to test that <a href="test.php">php is working</a></li>
Running up the website and clicking on the test.php link will show the date and time and the details of the php installation
Todo
insert a screenshot image of the php page
Install MySQL¶
Well actually MySQL is no longer available! So I’ve followed the ‘apt’ provided advice and used mariadb-server instead. The resultant operation is:
sudo apt-get install mariadb-server php-mysql
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
galera-3 gawk libaio1 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libfcgi-perl libhtml-template-perl libreadline5 libsigsegv2 libterm-readkey-perl lsof mariadb-client-10.3 mariadb-client-core-10.3 mariadb-server-10.3 mariadb-server-core-10.3 php7.3-mysql socat
Suggested packages:
gawk-doc libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl mariadb-test tinyca
The following NEW packages will be installed:
galera-3 gawk libaio1 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libfcgi-perl libhtml-template-perl libreadline5 libsigsegv2 libterm-readkey-perl lsof mariadb-client-10.3 mariadb-client-core-10.3 mariadb-server mariadb-server-10.3 mariadb-server-core-10.3 php-mysql php7.3-mysql socat
0 upgraded, 22 newly installed, 0 to remove and 0 not upgraded.
Need to get 18.1 MB of archives.
After this operation, 150 MB of additional disk space will be used.
Do you want to continue? [Y/n]
After which I want to change the default directory to which MariaDB points.
Changing the MariaDB default directory¶
The steps involved are:
- Stop mariadb.
sudo systemctl stop mariadb - Create a new directory with the snappy path of
/mnt/raid0/34-MySQL_Databases/c3Pio-LAMP-installation. - Copy all the data from the original directory in to the new one. This is done by running
sudo cp -R -p /var/lib/mysql/* /mnt/raid0/34-MySQL_Databases/c3Pio-LAMP-installation - Ensure that everything is owned by the user and group mysql:mysql.
sudo chown -R mysql:mysql /mnt/raid0/34-MySQL_Databases/c3Pio-LAMP-installation - Edit the configuration files.
Edit the configuration files¶
The mysqld programme (which to all intents and purposes is the mysql server) needs to know where to find it’s ‘socket’ and - whether by convention or necessity - the suggestions for the location of that socket always seem to be in the same directory as that to be used to store the databases themselves.
The data directory to be used needs to be specified.
The two config files to be updated are:
- /etc/mysql/mariadb.conf.d/50-client.cnf
- /etc/mysql/mariadb.conf.d/50-server.cnf
The client.cnf file only needs the location of the socket:
sudo nano /etc/mysql/mariadb.conf.d/50-client.cnf
#
# This group is read by the client library
# Use it for options that affect all clients, but not the server
#
[client]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = utf8mb4
# socket location
# socket = /var/run/mysqld/mysqld.sock
socket = /mnt/raid0/34-MySQL_Databases/c3Pio-LAMP-installation/mysqld.sock
# Example of client certificate usage
# ssl-cert=/etc/mysql/client-cert.pem
# ssl-key=/etc/mysql/client-key.pem
#
# Allow only TLS encrypted connections
# ssl-verify-server-cert=on
# This group is *never* read by mysql client library, though this
# /etc/mysql/mariadb.cnf.d/client.cnf file is not read by Oracle MySQL
# client anyway.
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
The server.cnf file needs both the socket and the data directory:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /run/mysqld/mysqld.pid
# socket = /run/mysqld/mysqld.sock
socket = /mnt/raid0/34-MySQL_Databases/c3Pio-LAMP-installation/mysqld.sock
#port = 3306
basedir = /usr
# datadir = /var/lib/mysql
datadir = /mnt/raid0/34-MySQL_Databases/c3Pio-LAMP-installation
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
#skip-external-locking
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
All this having been done all you need to do is restart mariadb and away you go!
sudo systemctl restart mariadbsudo mysql -u root -p -e "SELECT @@datadir;"
Enter password:
+--------------------------------------------------------+
| @@datadir |
+--------------------------------------------------------+
| /mnt/raid0/34-MySQL_Databases/c3Pio-LAMP-installation/ |
+--------------------------------------------------------+
How to restart MariaDB (for new configuration to apply)¶
- service mysqld start
- service mysqld stop
- service mysqld restart
OR
- service mysql start
- service mysql stop
- service mysql restart
Install Wordpress¶
I had hoped that this would be a straightforward matter of downloading the wordpress zip file, extracting it into a wordpress directory under the main html file and creating a link from my main website to index.php in that directory. Sadly not! Instead I get directed to the URL:
http://192.168.1.139/wordpress/wp-admin/setup-config.php
and the webpage comes up with the simple message:
Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
Back to the RPi-tutorial!
It might be worth pushing on with the tutorial but with a separate wordpress directory under the main one, or I might have to go back to the Multiple-Virtual-Hosts tutorial and work out how to set up two separate hosts - one for a Sphinx site and another for a Wordpress site. If taking this latter route what to do about the current ‘bog standard html’ site based on the apache default index page?