Welcome again - Let’s begin our journey

What will we do today? Sorry, but not coding yet

We’ll be setting up our PHP development server. Then we’ll test if it works

Sooo, there will be some code.

To the point - what will we learn today?

  1. Choosing code editor
  2. Setting up environment - Windows
  3. Setting up environment - Debian-based Linuxes

Let’s get started

1. Code editor

First thing we need is code editor

There’ve been countless discussions about it online - One google search gave me about 234,000,000 results, so I think this shows the scale pretty damm great

As many developers, as many opinions - every code tutorial might suggest you a diffretent one So, which one to choose?

Personally, I use Visual Studio Code (Or VSCode in short).

But, that’s not the only one. To name a few other

  • Sublime Text - Also a solid choice. I love it’s theme (called monokai) and use it everywhere (in this blog as well)
  • Notepad++ - My first code editor. I have some sort of sentiment towards it, but it has pretty outdated UI
  • PHPStorm - Paid IDE for PHP. Most PHP devs love it, I never tried it to be fair.

If you coded before - you can use whatever suits you the most. There are much more important things than code editor

I’ll use VSCode here and I suggest you to do it as well, especially if you’ve never wrote code in your life It offers built-in tutorial on how to use it and supports most of the languages

Also, go to Extensions (Ctrl+Shift+X) and install these 2-

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)

To install it, download official package from their website and

  • Follow it’s instructions (on windows)
  • Or install it with dpkg (in debian-based linux)
sudo dpkg -i [Path to .deb package]

When you really can’t download it, there’s always web version of it

Visual studio Code in it's pure form

Don’t worry about code, you will learn it late

2. Environment - Windows

Now we need to have a place running our codes - let’s start with windows

We will use XAMPP 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

XAMPP folder view

I have file extensions shown - you should as well.

In windows explorer, click on “View,” select “Show,” and choose the “File name extensions” option to show extensions

Coming back, we have 2 important places here

  1. htdocs folder

htdocs folder in explorer That’s where PHP code goes Delete it’s whole content - we won’t need it

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

Why is this file called index.php and not first.php?

index is commonly agreed name of main page - it comes from old internet versions. Index file used to redirect us to other subpages

If we wanted to visit https://example.com/, but developer hadn’t declared index.php - we would see every file and folder from website’s directory (So called directory listing)

But index.php is not the only index file - for example Apache in XAMPP has pretty decent list of those

    DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
                   default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
                   home.php home.pl home.cgi home.asp home.shtml home.html home.htm

So, if you create a file with any of those names - Apache will serve it as index page

No more digression - coming back to work.

Inside index.php, write this code

<?php

echo "Hello World";

?>

It displays us Hello World - more about it in the next part

We’re done there - what to do now?

  1. XAMPP Control panel

Actually xampp-control.exe - this little guy allows us to control every package in XAMPP

XAMPP control panel in folder

Open it - this window should pop up

Control panel

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

That’s the only one we’re interested in today. Later I’ll also talk about MySQL

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

Working Apache

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

Done and dusted. But what if I use linux?

Environment - Debian-based

For other distros, like Fedora or Red Hat this won’t be really useful If you use Arch (btw) I’ve found this gist for you

Here, we’ll configure everything separately - yet remember, XAMPP for linux (LAMPP) also exists

No more intro - let’s go

Package installation

First thing - installing required packages, such as:

  • apache2
  • mysql-server
  • mysql-client
  • php
  • php-mysql

Switch to root and update package lists - we need the newest version don’t we?

$ sudo su
# apt-get update

And install everything using apt package manager

# apt-get install apache2 mysql-server mysql-client php php-mysql

Then, we have new catalogue - /var/www/html

But there is a 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, so convenience is more important than security

# chmod 777 /var/www/html

Now visit localhost and check if it works

You see the page with header Apache default page? Then concgrats - everything works as should

Setting up PHPMyAdmin

Then, let’s install phpmyadmin - download the package

# apt-get install phpmyadmin

While installing, there config window should pop-up - choose apache2 and then dbconfig-common - we have mysql, so no worries

It will create phpmyadmin mysql user - we should use it, rather than root, as it’s and safer overall

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

Setting up password Password configuration window

Retype it, and we should be good to go - visit 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

# 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

# systemctl restart apache2
# systemctl restart mysql

Now it should work - log into phpmyadmin with username phpmyadmin and password you have set up

Succesfully loged? Cool, that’s everything we need

Log out using icon under logo

This one:

Log out icon

Now, phpmyadmin works - but not fully Luckily, these will be quick

Granting privileges for phpmyadmin user

Right now phpmyadmin can’t really do anything except for browsing databases - It can’t stay like this

Log into mysql as root

# mysql

Then set mysql as used database

mysql> USE mysql;

AWe need to grant necessary privileges to our user

mysql> GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost';

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

You see - That’s SQL, but about it later in this course

Now, FLUSH (reset) privileges on mysql server - then exit

mysql> FLUSH PRIVILEGES;
mysql> exit;

Restart both services

# systemctl restart apache2
# systemctl restart mysql

And the last (required) thing

Displaying errors in PHP

This is a development server, so we should know what’s’ wrong in our code - then server needs to show us errors and warnings

Open PHP configuration file (php.ini)

# 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
    # ls -la /etc/php
    

    And copy directory name in place of [Your Version]

Now find these lines and change them accordingly:

  • error_reporting = E_ALL
  • display_errors = On
  • display_startup_errors = On
  • log_error = On

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

Semicolon-starting lines are comments - changing them will do nothing (except for frustrating future you)

We can save and close it (like before), then restart apache

# systemctl restart apache2

Everything necessary done - I also want to show you one mor thing

Adding .htaccess file handling

That part is optional, as it’s not really PHP But.. we’ll need it in the last part

So, let’s add .htaccess handling - open apache config

# 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

That’s it - save and exit nano, then restart Apache

# systemctl restart apache2

Now, we have successfully set up Apache server on linux from scratch

Congrats

Another ways of configuring this

Of course, Apache is not the only choice here

Even just PHP package has it’s own development server.

If not Apache, nor PHP development server - Nginx can be a solid choice as well. 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, as name says, XAMPP is for every platform

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, 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.

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

We’re done - see you in the next part (We will finally start coding something)