This content originally appeared on DEV Community and was authored by DEV Community
Why PHP in 2022?
I've recently picked up an interest in learning PHP because of how prevalent it still is in 2022. Although developers are moving away from it, 8 out 10 websites use PHP as their server-side language of choice. Also, 27% and 19% of developers from US and Canada picked it as their primary programming language in 2021.
Why I Decided to Write this
Coming from JavaScript ecosystem where there is a lot of attention, I found it more challenging to find an up-to-date resource on how to set up a development environment that goes over everything you need.
After going through a bajillion resources on how to do every little thing, I have developed a decent idea for the easiest and most sustainable way to set up the basic tools you need to get started.
This is tutorial is an attempt at what I wish I found when I started.
PHP
To install the latest version of PHP, 8.1 at the time of writing, you have to add a repository first. This is Ondřej Surý's PPA, who is the lead developer on PHP and Debian. You will be able to find the latest PHP version in his repository.
If you want to install PHP 7.4, the most used version, and you happen to be on Ubuntu 20.04 and later, you can skip adding the PPA and install php7.4 right away.
You can check what version of PHP you have available by executing apt-cache madison php
.
Add Ondřej's PPA
sudo apt install software-properties-common && sudo add-apt-repository ppa:ondrej/php -y
sudo apt update && sudo apt upgrade -y
Install PHP
sudo apt install php
Verify You've Installed The Right Version
php --version
PHP has modules that extend its functionality and are often necessary to work with other services. For now, all we need is the php-cli module which is installed by default with PHP. It will allow us to run a development server that refreshes on saves.
It is a good idea to dedicate a directory for PHP projects. This will be especially useful when you install a webserver later on in this tutorial. Lets make a directory and place a PHP file inside it to test our current setup. I've made my projects folder in my home directory, but you can place it deeper down there.
Make a PHP Directory And File For Testing
mkdir ~/php && echo "<?php echo 'Hello World!';" > ~/php/index.php
Run the Dev Server
php -S localhost:5500 -t ~/php
Now you should be able to go to localhost:5500 on your browser and see "Hello World!".
You can change the address, localhost, and the port, 5500, to your liking. You might see an error if the port is taken, just use another like 8080.
-t ~/php
specifies the base directory for the webserver. If it is not specified, it will choose the current directory. You can access other files and directories in your browser by writing their path. For example, you have a directory called "proj" inside the "php" directory; you would access it by inputting localhost:5500/proj/
assuming your server was started on "php".
VS Code
Open VS Code, go to preferences, then type in "php" in the search bar. Enable "Suggest" and "Validate". You will also see Validate: Executable Path
with a Edit in settings.json
option.
VS Code uses PHP for linting by running php -l
so we need to tell it where to find the PHP executable. On the Validate: Executable Path
, click Edit in settings.json
and add the line below to your JSON file. It is important that you do not delete anything that is already there.
Add line to VS Code's Settings.json
{
// There may be other config lines here. Do not delete them.
// Add the line below
"php.validate.executablePath": "/usr/bin/php",
}
While editing a PHP file in VS Code, you can press CTRL + SPACE
for context specific snippets.
You can install "PHP Debug" and "PHP Intelephense" extensions for a debugging and intelliSense (code completion).
Apache Webserver
You will need to deploy your PHP code sooner or later, and you will need a webserver then. It is not a bad idea to install a webserver on your local machine to test your code on it every now and then. Apache serves that purpose very well as you can use it both in production and development, though I will not be going into setting it up for production.
Install Apache
sudo apt install apache2
Run Apache
sudo service apache2 start
Now you have the server running, open your browser and type in localhost
in your address bar. You should see a webpage informing you that Apache is working.
By default Apache loads up files from /var/www/html/
. You can place your projects in that folder but that would require root access making it impractical in a development setting.
You can enable an Apache mod, called userdir
, that allows us to store projects in our home directory. By default that directory is called public_html, but you can change that too.
Enabling Userdir
sudo a2enmod userdir
Configure Apache for Development
<IfModule mod_userdir.c>
UserDir php
UserDir disabled root
<Directory /home/*/php>
AllowOverride All
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require all granted
</Directory>
</IfModule>
After changing AllowOverride
and Require
, you can also change the default directory to php
(path of PHP projects directory from home) like I did, but that is optional.
Restart the server
sudo service apache2 restart
Now open up your browser's address bar and access localhost/[YOUR USERNAME]
. If you do not know your username, execute whoami
in your terminal to reveal your username.
If you changed your default directory to your projects directory like I did earlier, you should see "Hello World!" like you saw on the PHP development server.
MySQL and phpMyAdmin
You might need a database for a project. MySQL is a popular choice that works well with PHP once you add a module called php8.1-mysql. You can go with other solutions, chances are there is a module for it.
Install MySQL And php8.1-mysql
sudo apt install mysql-server php8.1-mysql
You might also want a GUI to interact with your database. The de facto choice is PhpMyAdmin, but at the time of writing, version 5 is not yet available to download from the default Ubuntu repository. You can check if it is available for you by executing dkpg -l | grep phpmyadmin
. If it is, you can skip adding the PhpMyAdmin repository.
Add the PhpMyAdmin Repository
sudo add-apt-repository ppa:phpmyadmin/ppa
sudo apt update
Install phpmyadmin
sudo install phpmyadmin
Configure phpmyadmin
You will need to add a line to the bottom of a config file to get PhpMyAdmin working with Apache.
echo "Include /etc/phpmyadmin/apache.conf" | sudo tee -a /etc/apache2/apache2.conf
You Are Done!
I hope you found this tutorial beneficial.
Cover photo by Ben Griffiths
This content originally appeared on DEV Community and was authored by DEV Community
DEV Community | Sciencx (2022-03-05T15:02:41+00:00) How to Setup a PHP Development Environment on Ubuntu Using the Latest PHP, VS Code, Apache, PhpMyAdmin 5. Retrieved from https://www.scien.cx/2022/03/05/how-to-setup-a-php-development-environment-on-ubuntu-using-the-latest-php-vs-code-apache-phpmyadmin-5/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.