This page looks best with JavaScript enabled

Setting up LAMP Apache, Mysql (MariaDB), and php on raspberry pi ubuntu

 ·   ·  β˜• 5 min read

    Setting up LAMP server

    Install Apache

    First, update the available packages by typing the following command into the Terminal:

    sudo apt update
    

    Then, install the apache2 package with this command:

    sudo apt install apache2 -y
    

    Test the web server

    By default, Apache puts a test HTML file in the web folder. This default web page is served when you browse to http://localhost/ on the Pi itself, or http://192.168.1.10 (whatever the Pi’s IP address is) from another computer on the network. To find the Pi’s IP address, type hostname -I at the command line (or read more about finding your IP address).

    Browse to the default web page either on the Pi or from another computer on the network and you should see the following:

    This means you have Apache working!

    Changing the default web page

    This default web page is just an HTML file on the filesystem. It is located at /var/www/html/index.html.

    Navigate to this directory in a terminal window and have a look at what’s inside:

    cd /var/www/html
    ls -al
    

    This will show you:

    total 12
    drwxr-xr-x  2 root root 4096 Jan  8 01:29 .
    drwxr-xr-x 12 root root 4096 Jan  8 01:28 ..
    -rw-r--r--  1 root root  177 Jan  8 01:29 index.html
    

    This shows that by default there is one file in /var/www/html/ called index.htmland it is owned by the root user (as is the enclosing folder). In order to edit the file, you need to change its ownership to your own username. Change the owner of the file (the default pi user is assumed here) using sudo chown pi: index.html.

    You can now try editing this file and then refreshing the browser to see the web page change.

    Your own website

    If you know HTML you can put your own HTML files and other assets in this directory and serve them as a website on your local network.

    Additional - install PHP

    To allow your Apache server to process PHP files, you’ll need to install the latest version of PHP and the PHP module for Apache. Type the following command to install these:

    sudo apt install php libapache2-mod-php -y
    

    Now remove the index.html file:

    sudo rm index.html
    

    and create the file index.php:

    sudo nano index.php
    

    Put some PHP content in it:

    <?php echo "hello world"; ?>
    

    If you see the raw PHP above instead of β€œhello world”, reload and restart Apache like so:

    sudo service apache2 restart

    Now save and refresh your browser. You should see “hello world”. This is not dynamic but still served by PHP. Try something dynamic:

    <?php echo date('Y-m-d H:i:s'); ?>
    

    or show your PHP info:

    <?php phpinfo(); ?>
    

    Install MariaDB

    Install the MariaDB Server and PHP-MySQL packages by entering the following command into the terminal window:

    sudo apt-get install mariadb-server php-mysql -y
    

    Now restart Apache:

    sudo service apache2 restart
    

    Download WordPress

    • Download WordPress using wget.
    sudo wget http://wordpress.org/latest.tar.gz
    
    • Extract the WordPress tarball to get at the WordPress files.
    sudo tar xzf latest.tar.gz
    
    • Move the contents of the extracted wordpress directory to the current directory.
    sudo mv wordpress/* .
    
    • Tidy up by removing the tarball and the now empty wordpress directory.
    sudo rm -rf wordpress latest.tar.gz
    
    • Running the ls or tree -L 1 command now will show you the contents of a WordPress project:

    • You should now change the ownership of all these files to the Apache user:

    sudo chown -R www-data: .
    

    in my case: chown -R ubuntu .

    chown example

    chown root /u        Change the owner of /u to "root".
    chown root:staff /u  Likewise, but also change its group to "staff".
    chown -hR root /u    Change the owner of /u and subfiles to "root".
    
    

    Set up your WordPress Database

    Set up MySQL/MariaDB

    To get your WordPress site set up, you need a database. This is where MySQL and MariaDB come in!

    • Run the MySQL secure installation command in the terminal window.
    sudo mysql_secure_installation
    
    • You will be asked Enter current password for root (enter for none): β€” press Enter.

    • Type in Y and press Enter to Set root password?.

    • Type in a password at the New password: prompt, and press Enter. Important: remember this root password, as you will need it later to set up WordPress.

    • Type in Y to Remove anonymous users.

    • Type in Y to Disallow root login remotely.

    • Type in Y to Remove test database and access to it.

    • Type in Y to Reload privilege tables now.

    When complete, you will see the message All done! and Thanks for using MariaDB!.

    Create the WordPress database

    • Run mysql in the terminal window:
    sudo mysql -uroot -p
    
    • Enter the root password you created.

    You will be greeted by the message Welcome to the MariaDB monitor.

    • Create the database for your WordPress installation at the MariaDB [(none)]> prompt using:
    create database wordpress;
    

    Note the semi-colon ending the statement.

    If this has been successful, you should see this:

    Query OK, 1 row affected (0.00 sec)
    
    • Now grant database privileges to the root user. Note: you will need to enter your own password after IDENTIFIED BY.
    GRANT ALL PRIVILEGES ON wordpress.* TO 'root'@'localhost' IDENTIFIED BY 'YOURPASSWORD';
    
    • For the changes to take effect, you will need to flush the database privileges:
    FLUSH PRIVILEGES;
    
    • Exit the MariaDB prompt with Ctrl + D.

    • Restart your Raspberry Pi:

    sudo reboot
    

    Ohidur Rahman Bappy
    WRITTEN BY
    Ohidur Rahman Bappy
    πŸ“šLearner 🐍 Developer