Local Web development Setup (PHP + Mysql)
Hey there, Its been so long since I have posted anything so here in this article I will try to share how I have setup simple web development environment with PHP & MySQL.
PS : This article assumes you have a Linux based system so, if you are on windows please checkout official guide here on how to setup WSL on windows.
Lets begin,
1) Setup Server (Apache2/httpd) :
I prefer to work with apache server so, the process will follow the steps tailored toward apache installation.
To install the apache simply run the below command.
> sudo apt-get install apache2
or
> sudo yum install httpd
Note : Please check which version of Linux installation and package manger you have installed before running the above commands.
Fun fact here
If the installation succeeds you should be able to start apache as service with below command.
> sudo service apache2 start
Configuring Your Web Project (Websites) :
To work with multiple projects/site you will have to setup Vhosts (Virtual hosts). Below are the steps for the same.
- Open vhost file which is located at /etc/apache2/sites-enabled/000-default.conf
- Paste the below lines to configure a project.
<VirtualHost yoursitedomain.test:80>
#ServerAdmin admin@test.com
ServerName yoursitedomain.test
ServerAlias yoursitedomain.test
DocumentRoot /home/rohits/projects/mysite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined<Directory /home/rohits/projects/mysite/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Order Deny,Allow
Allow from all</Directory></VirtualHost>
Check the highlighted lines you will need to modify them as per your requirements.
- The very first line specify the domain you will use to visit the site. (Note the port here defaults to 80)
<VirtualHost yoursitedomain.test:80>
- Server Name & Server Alias : As name suggests they sets the server alias and name for your project.
- DocumentRoot : It is simply the root of your site/project.
- Directory tag : It basically specify the permission and rules that apply to the files.
<Directory /home/rohits/projects/mysite/> </Directory>
For configuring other sites you can simply duplicate the section and update the values as required.
Once you are done, you can simply save the file and restart the apache server for changes to take effect with below command.
> sudo service apache2 restart
Note : Its a good practice to create a different vhost file for each site and then use a2ensite apache utility to enable/disable them. As I want to keep it simple I will go with the default file.
> sudo a2ensite <name of your config file>
2) Configure Host machine :
So far we have only configured the apache server to process our web request. However to pass web requests to apache, we need to map the domains to some IP. Here it will be our local system IP which is 127.0.0.1
To setup the mappings simply open the file at below location with admin/root privileges
Windows :
C:\Windows\System32\drivers\etc\hosts
Linux :
/etc/hosts
Add the below entry in your file after the existing entries and save the file. (Do not remove the existing entries unless you know what you are doing!).
127.0.0.1 yoursitedomain.test
To check if you have done everything correct you can simply try ping request to your newly added domain and you should see output similar to below one.
> ping yoursitedomain.test
3) Configure MySql (MariaDB) :
Here, we will install and configure MariaDb as our Database server which is fork of mysql and pretty much similar to Mysql.
- To Install MariaDb server run the below command.
> sudo apt-get install mariadb-server
- Once installed, you will need to configure Mariadb root user & its permissions. To update the configuration for the root user run the below command.
> sudo mysql_secure_installation
Follow the steps and you should be able to set the root password. You can simply press enter to keep everything default.
- Try connecting mysql with your username and password
> mysql -u root -p
Type above command and enter the password when prompted for.
If you have followed all steps correctly you should be able to connect to Mariadb.
Note : Its a good practice to create a separate user for your sites which you can do with below command
// enter the mysql prompt
> mysql -u -root -p<password>// Now when you are in mysql shell type below command to add an user.mysql> CREATE OR REPLACE USER username@localhost IDENTIFIED BY 'password';
and this bring us to end of this introductory article. I hope it helps!
Thanks & Cheers!
Happy Coding!