DEV Community

Mateusz Jasiński
Mateusz Jasiński

Posted on • Updated on

PHP 0 to hero pt. 1 - Setting up

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 (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

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

sudo dpkg -i [Path to .deb package]
Enter fullscreen mode Exit fullscreen mode

When you really can't download VSCode you can always use web version of it

Visual studio Code in it's pure form

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

We have 2 important places here

  1. htdocs folder

htdocs folder in explorer
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

echo "Hello World";

?>
Enter fullscreen mode Exit fullscreen mode

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

  1. XAMPP Control panel

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

XAMPP control panel in folder

Open it - this window should pop up

Control panel

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

Last thing is - go to 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

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

$ sudo su
# apt-get update
Enter fullscreen mode Exit fullscreen mode

And install everything using apt package manager

# apt-get install apache2 mysql-server mysql-client php php-mysql
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

# apt-get install phpmyadmin
Enter fullscreen mode Exit fullscreen mode

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

# nano /etc/apache2/apache2.conf
Enter fullscreen mode Exit fullscreen mode

Then, at the very end add

Include /etc/phpmyadmin/apache.conf
Enter fullscreen mode Exit fullscreen mode

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

Restart both MySQL and Apache

# systemctl restart apache2
# systemctl restart mysql
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

Then set mysql as used database

mysql> USE mysql;
Enter fullscreen mode Exit fullscreen mode

And we need to grant those privileges

mysql> GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost';
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Restart both services

# systemctl restart apache2
# systemctl restart mysql
Enter fullscreen mode Exit fullscreen mode

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

# nano /etc/php/[Your Version]/apache2/php.ini
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

# systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

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

# nano /etc/apache2/apache2.conf
Enter fullscreen mode Exit fullscreen mode

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

AllowOverride All
Enter fullscreen mode Exit fullscreen mode

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

# systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

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.

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

$ sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

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

And if you are familiar with PHP and just checking something, maybe Creating private messages system in PHP will be interesting

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

Top comments (2)

Collapse
 
naizhao profile image
Sam Ng

nice post!

BTW, If you recommend a development environment on macOS, there's a best alternative to XAMMP called ServBay, it includes Web Server, PHP, NodeJS, Database and NoSQL like redis, you can visit servbay.dev/ to get more information

Collapse
 
wizarddos profile image
Mateusz Jasiński

Thanks, good to know. As I said I don't have access to mac, so no way for me to check it, but still.
As I looked at their website, I kind of wish they had made something like that for linux or windows as ServBay looks just amazing as a tool