"P" is for PHP
Tags: codenewbie, todayilearned, sideprojects, security
Published: 2020-12-25 00:16:54

When I last left off I had just finished the PostgreSQL database install and basic configuration so now it's time for the "P" in the LAMP stack, PHP. The "P" in LAMP technically represents "PHP" (PHP: Hypertext Preprocessor); though like MySQL, PHP has also been substituted for other server-side scripting languages in recent years; such as, Perl, and Python. 

[:o:]Linux
[:o:]Apache
[:o:]~~MySQL~~ PostgreSQL
[:x:]__PHP__



PHP is an server-side scripting language, and general purpose programming language, that can be deployed on most web servers, works on almost every operating system and platform, and is free to use. It was created in 1994 and in spite of its historical security issues it has been widely ported and remains popular for its ability to efficiency process code and create dynamic web content on/for a web server. <sup>[4](#L4)</sup>
![PHP](https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Webysther_20160423_-_Elephpant.svg/200px-Webysther_20160423_-_Elephpant.svg.png)

When building a dynamic website/application PHP commands are either embedded directly into HTML source code or in external files that are linked to. When a web server gets a request for a PHP file, like `index.php`, or encounters a link to an external PHP file in an HTML file, like `index.html`, the web server passes that part of the request to a PHP interpreter. The PHP interpreter will read the file and execute any PHP code it finds. Once done interpreting the file the output of the code will be passed back to the web server which will then send the dynamically created content back to the client/browser that made the original request. 

This all means that any PHP code that is run will not be visible to the user making a request but instead will be run by/on the web server and will result in a dynamically created HTML document that will be visible to the user. <sup>[3](#L3)</sup>


![LAMPhp location](https://dev-to-uploads.s3.amazonaws.com/i/civwq0g4l7hm05l3l9kk.png)




# <a name="TOC">Table Of Contents</a>
[Step 1: Installing PHP](#S1)
[Step 2: Testing PHP](#S2)








# <a name="S1">Step 1: Installing PHP</a>
1. To install the PHP module run `sudo apt install php libapache2-mod-php php-pgsql`. 
 - This will install the PHP module with 2 dependencies of the PHP module for Apache and the PHP PostgreSQL connector.
 - `sudo apt install` is the command to install something on Ubuntu.
 - `php` is the thing that is being installed.
 - `libapache2-mod-php` is the apache2 PHP module dependency.
 - `php-pgsql` is the PostgreSQL module for PHP dependency.
2. Restart the apache2 service with `systemctl restart apache`.

{[Back to the Table Of Contents](#TOC)}








# <a name="S2">Step 2: Testing PHP</a>
That's it. There is no configuring PHP. The set up is done but you need to test it to ensure that it is working.

1. Navigate into the "etcpasswdapp" site folder with `cd /var/www/etcpasswdapp/`.
2. Make a new PHP index file with `nano index.php`.
 - This will also open up the "nano" editor.
3. Edit the "index.php"  file by adding some simple PHP code to the file such as:```
<!DOCTYPE html>
<html>
    <head>
        <title>Example</title>
    </head>
    <body>

        <?php
            echo phpinfo();
        ?>

    </body>
</html>
```or ![Example PHP page](https://dev-to-uploads.s3.amazonaws.com/i/cw5dteet0ijlc1vo20af.png)
4. Save the file and exit the editor.
6. On the client computer navigate to the address of the PHP file such as "etcpasswdapp.com/index.php" and notice that there is now the PHP in the page. Below you can see the output of my `phpinfo()` command from my server computer.  ![phpinfos](https://dev-to-uploads.s3.amazonaws.com/i/7c2tv5w56eu4pbzyu8uz.png)
 
- Notice that I didn't delete my `index.html` page but instead am able to call the `index.php` in place of it.

Notice that if you view the "source code" in the browser that you don't see the simple PHP page that I created by instead the dynamically created HTML output of the command in the next picture. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/suj18cfwz7lu4jb4hmkv.png)




{[Back to the Table Of Contents](#TOC)}






<hr>
DONE!! Now that the LAMP stack is installed and has some basic configurations it's time to move to setting up your website. For this I will be setting up the React frontend separate from the Ruby on Rails API backend and setting up their SSL configuration and then hardening the server itself.






[:o:]Linux
[:o:]Apache
[:o:]~~MySQL~~ PostgreSQL
[:o:]__PHP__



<hr>

Happy Hacking
![Happy Hacking ^_^](https://media.giphy.com/media/l3vRmVv5P01I5NDAA/giphy.gif)

### Resources:
<a name="L1">1.</a> https://create-react-app.dev/docs/deployment/
<a name="L2">2.</a> https://www.ibm.com/cloud/learn/lamp-stack-explained
<a name="L3">3.</a> https://stillat.com/blog/2014/04/02/how-does-php-work-with-the-web-server-and-browser
<a name="L4">4.</a> https://en.wikipedia.org/wiki/PHP




##### <figcaption>Please Note: that I am still learning and if something that I have stated is incorrect please let me know. I would love to learn more about what I may not understand fully.</figcaption>