PHP 0 to hero pt. 1 - Setting up
Tags: webdev, php, tutorial, beginners
Published: 2023-11-23 13:52:02


Welcome again - today I can actually welcome you in the first part of this tutorial

Our main objective will we to set up a working PHP server and test it if it works - we will also learn a bit about executing PHP code

### Content
1. Code editor
2. Environment - Windows 
3. Environment - Linux Ubuntu
4. Conclusion

So, let's get started

### 1. Code editor

First thing we need to get is **code editor**

There are plenty of them available - If really want to, you can even write it in a Windows Notepad (But I don't recommend it, it's painful)

Here, Linux users have easier, as their default editors often support syntax highlighting (special parts of code change colors, it helps a lot) and allows us to manage tabs and spaces pretty good(Yet it doesn't mean you can't use mentioned below code editors)

So, the most important part. Which one to choose?

Every programmer has their opinion - I use [Visual Studio Code](https://code.visualstudio.com/) (I'll refer to it as **VSCode**).

But Atom, Sublime or even Notepad++ are suitable choices
There are also paid options like PHPStorm (I never used it, so I won't say anything)

If you are just starting - go with VSCode. It's offering tutorial on how to use it and supports most of the languages

If you coded before - you can use whatever suits you the most


For Visual Studio Code I recommend you go to `Extensions` (Ctrl+Shift+X) and install 2 of them

- [PHP Intelephense](https://marketplace.visualstudio.com/items?itemName=bmewburn.vscode-intelephense-client)
- [Path Intellisense](https://marketplace.visualstudio.com/items?itemName=christian-kohler.path-intellisense)

First one suggests you functions as you type, and second helps you with paths (especially relative ones, you will learn about them in this course too)


If you chose VSCode, then install it with original `.exe` installer on windows, and with specific package on your linux

Installing no debian-based linuxes
```bash
sudo dpkg -i [Path to .deb package]
```

When you really can't download VSCode you can always use [web version](https://vscode.dev/) of it

![Visual studio Code in it's pure form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6w0jhdkzkkh8rwe2kw9.png)

Up here we see visual studio Code in it's pure form - Don't worry about code, you will learn it late




### 2. Environment - Windows 

I will write about windows first - as it's more often used and easier one actually

We will use [XAMPP](https://www.apachefriends.org/index.html) package

XAMPP stands for
X - Cross-platform
A - Apache (Server used to run PHP)
M - MariaDB (Our database, DBMS to be more specific)
P - PHP (Programming language we will learn)
P - Perl (Another programming language not mentioned in this course)

Choose version for windows. Then open downloaded .exe file and install it

Once installed go to `C:/Xampp` (Or any other place where you have installed it). It should look like this


![XAMPP folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qwvvw2t2mvy2xv2cxao.png)

XAMPP folder view

We have 2 important places here

1. `htdocs` folder

![htdocs folder in explorer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3lsgh0fgh4lphqikynb.png)
This is the place where we place PHP code

Delete it's whole content - we won't need it

Inside it, create a new folder called `server-test` and inside it file called `index.php`. 

Why like this? Because `index` is a name for a main page of the website. When you look for php-based website like `www.example.com/` you actually go to `www.example.com/index.php`. It's just hidden

Inside it, write this code
```php
<?php

echo "Hello World";

?>
```
It displays us message `Hello World` - About `<?php` tags we will learn in the next part

We can close it. Now second most important part

2. XAMPP Control panel

Actually `xampp-control.exe` - that's the application that allows you to control your whole package

![XAMPP control panel in folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrwhxgv2ha729m5v9an8.png)

Open it - this window should pop up

![Control panel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhjjl7qqrp3anboq517a.png)

Today, we are only focused on `Apache` - we will get familiar with `MySQL` too, but not today

Start `Apache` by clicking on the `start` button next to it

Then, If everything went good you should have Apache highlighted with green color, like this


![Working Apache](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32ppzcfa2sg7do1iv78i.png)


Last thing is - go to [http://localhost/server-test/](http://localhost/server-test/) and check. If you see `Hello World` written on it - that means your server worked

And that's it - now how to configure it on Linux?

### Environment - Linux Ubuntu

This part covers only debian-based Linuxes - If you own a different one, like Arch, you can check [this gist](https://gist.github.com/superjojo140/18e250786d977b27571124f81bba5018)

Of course you can use XAMPP on linux too - but I wanted to show you another method - to manually set up everything

#### Package installation

First thing we need to do is installing required packages:

- `apache2`
- `mysql-server`
- `mysql-client`
- `php`
- `php-mysql`


Switch to root and update package lists
```bash
$ sudo su
# apt-get update
```

And install everything using `apt` package manager
```
# apt-get install apache2 mysql-server mysql-client php php-mysql
```

After installing we have new catalogue - `/var/www/html`

But there is one problem - Only root has `write` privilege, let's give everyone write access

Note: This shouldn't be done on production sever (One accessible from other computers) - but here we are setting up development server

```
# chmod 777 /var/www/html
```

Now we can visit `localhost` and check if it works

If you see the page with header `Apache default page` - everything works cool

#### Setting up PHPMyAdmin

Then, let's install `phpmyadmin` - download the package
```bash
# apt-get install phpmyadmin
```

If a window pops out about configuration - choose `apache2`, then choose `dbconfig-common` - we have mysql so no worries

It will create `phpmyadmin` user - we should use this account, not root, that's just better and safer

As password I have chosen `phpmyadmin` - so it's simple to remember

![Setting up password](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7pw8ev4l7bukoa6j0m11.png)
Password configuration window

Retype it, and we should be done - go to `localhost/phpmyadmin`

If you get `404 Not found` error - don't panic, we can fix it

Open `/etc/apache2/apache2.conf` file in your favourite editor - I'll open it in terminal with `nano`
```bash
# nano /etc/apache2/apache2.conf
```

Then, at the very end add
```
Include /etc/phpmyadmin/apache.conf
```

And save using `Ctrl-O + Enter` combination, then use `Ctrl-X` to exit

Restart both `MySQL` and `Apache`
```bash
# systemctl restart apache2
# systemctl restart mysql
```

And now it should work - log into it with username `phpmyadmin` and password you have set up - let's log out using icon under logo

This one:

![Log out icon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dnv30bxzal2qatlrdr5.png)

Now, `phpmyadmin` works - but there are two things left, luckily quick ones


#### Granting privileges for `phpmyadmin` user

Right now `phpmyadmin` can't really do anything except for browsing - we should change this

Log into `mysql` as root
```
# mysql
```

Then set `mysql` as used database
```
mysql> USE mysql;
```

And we need to grant those privileges
```
mysql> GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost';
```

This basically means:
" Grant all privileges on every database to phpmyadmin at localhost" 

You see - SQL is easy. But about it later in this course

Now we need to `FLUSH` (restart) privileges on mysql server - then we can exit
```
mysql> FLUSH PRIVILEGES;
mysql> exit;
```

Restart both services
```bash
# systemctl restart apache2
# systemctl restart mysql
```

Now, last required part

#### Displaying errors in PHP

Because this is a development server, we should know what is wrong in our code - that's why we have to set up displaying errors and warnings 

Open PHP configuration file `php.ini`

```bash
# nano /etc/php/[Your Version]/apache2/php.ini
```
You can check version by
1. `php -v` and copying first 2 digits. For me it was `8.1`
2. You can list directories in `/etc/php`, like this
```bash
# ls -la /etc/php
```
And copy directory name in place of `[Your Version]`

Now find these lines and change them like this:

-  `error_reporting = E_ALL`
-  `display_errors = On`
-  `display_startup_errors = On`
-  `log_error = On`
   

Here remember one thing - change those values only for lines that doesn't start with semicolon (`;`)

Semicolon-starting lines are comments - changing them will do nothing, except for frustrating you in the future

We can save and close it (like before), and restart apache
```bash
# systemctl restart apache2
```

That's actually everything required - but I'd like to show you one another thing


#### Adding `.htaccess` file handling

That part is optional - but will be useful at the end

Let's add that handling - open apache config
```bash
# nano /etc/apache2/apache2.conf
```

Scrolll to `<Directory /var/www/>` and set `AllowOverride` to `All` instead of `None` - this line should look like this
```
AllowOverride All
```

And that's it - save and exit `nano`, then restart Apache

```
# systemctl restart apache2
```

That's it - we have successfully set up Apache server on linux from scratch

#### Another ways of configuring this

Of course, `Apache` is not the only choice here

Even just PHP package has it's own [development server](https://www.php.net/manual/en/features.commandline.webserver.php).

If not `Apache`, nor PHP development server - Nginx is also a solid choice. You can install it like `Apache`
```
$ sudo apt install nginx
```


### 4. Conclusion

That's it for today - I didn't include MacOS in here, as I don't have access to it. But you can use XAMPP there

I hope you learned something - and now have working setup for it

By the way, big thank you to polish IT channel on YouTube [Pasja Informatyki](https://www.youtube.com/channel/UCzn6vAfspIcagLax1fck_jw), as without them and their video about setting up PHP and SQL server on Linux, last part of this article would have been much harder to write for me.

This article draws to an end - you can leave me a follow and check out my other articles such as:

- [Linux Privileges Guide](https://dev.to/wizarddos/quick-guide-for-linux-privileges-4dd6)
- [Part 0 of this course](https://dev.to/wizarddos/php-0-to-hero-pt0-introduction-58l9)
- [Dreaming - TryHackMe writeup](https://dev.to/wizarddos/dreaming-tryhackme-writeup-3d53)


And if you are familiar with PHP and just checking something, maybe [Creating private messages system in PHP](https://dev.to/wizarddos/coding-diy-pt2-making-dm-system-with-php-3459) will be interesting

That's it. see you in the next part of this course. We will finally start coding something