XAMPP Tutorial: How to Set Up and Use XAMPP for Local Web Development
XAMPP is a free and open-source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB (formerly MySQL), and interpreters for scripts written in PHP and Perl. This tutorial will guide you through downloading, installing, and using XAMPP to create a local development environment.
Part 1: Installing XAMPP
Step 1: Download XAMPP
- Go to the official XAMPP website: https://www.apachefriends.org.
- Choose the version of XAMPP you want to install (there are versions with different versions of PHP).
- Download the XAMPP installer for your operating system (Windows, macOS, or Linux).
Step 2: Install XAMPP
- Run the installer you just downloaded.
- You will see a list of components to install. By default, the essential components (Apache, MySQL/MariaDB, PHP, Perl) are already selected. Leave them as is unless you have specific needs.
- Click Next, choose the installation location (default is usually fine), and click Next again.
- XAMPP will install all the necessary files. Once the installation is complete, click Finish.
Part 2: Starting XAMPP and Running the Control Panel
Step 1: Open XAMPP Control Panel
- After installation, launch the XAMPP Control Panel. If you don’t see it immediately, search for “XAMPP” on your computer and open it.
- The XAMPP Control Panel is where you can manage the various services (Apache, MySQL, FileZilla, Mercury, Tomcat) that XAMPP provides.
Step 2: Start Apache and MySQL
- In the control panel, click Start next to Apache to start the web server.
- Click Start next to MySQL (or MariaDB, depending on your version) to start the database server.
- If both services start successfully, they will turn green, and you will see “Running” next to them.
Part 3: Setting Up Your Local Web Server
Step 1: Accessing the Local Server
- Once Apache is running, open your browser and type http://localhost in the address bar.
- You should see the XAMPP dashboard, which means your local web server is working correctly.
Step 2: Setting Up Your Local Website
- The default location for your local web files is in the htdocs folder within the XAMPP installation directory (e.g.,
C:\xampp\htdocson Windows). - Create a new folder inside the
htdocsdirectory (e.g.,C:\xampp\htdocs\mywebsite). - In this folder, create a basic index.php file to test your setup:
<?php echo "Hello, World!"; ?> - Save the file and open your browser. Type http://localhost/mywebsite in the address bar, and you should see the “Hello, World!” message.
Part 4: Managing Databases with phpMyAdmin
Step 1: Access phpMyAdmin
- With XAMPP running, go to http://localhost/phpmyadmin in your browser.
- phpMyAdmin is the interface you will use to manage your MySQL/MariaDB databases.
Step 2: Create a New Database
- In phpMyAdmin, click on the Databases tab at the top.
- Enter a name for your new database and click Create.
- Your database is now created, and you can begin adding tables and data.
Step 3: Connecting to the Database from PHP
- In your PHP code, you can connect to your MySQL database using the following script:
<?php $servername = "localhost"; $username = "root"; $password = ""; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?> - Save this file in your web directory (e.g.,
C:\xampp\htdocs\mywebsite\connect.php). - Open http://localhost/mywebsite/connect.php in your browser to see if the connection is successful.
Part 5: Configuring XAMPP for Different Ports (Optional)
If another service (like Skype or another server) is using port 80, Apache might not start. You can configure Apache to use a different port.
Step 1: Changing Apache’s Port Number
- In the XAMPP Control Panel, click Config next to Apache.
- Select Apache (httpd.conf) from the dropdown menu.
- In the file, search for the line
Listen 80and change80to another available port (e.g.,8080). - Also, search for
ServerName localhost:80and change80to8080. - Save and close the file, then restart Apache.
Step 2: Accessing Apache on a Different Port
- After changing the port, open your browser and type http://localhost:8080 to access your server.
Part 6: Stopping XAMPP Services
Step 1: Stop Services
- In the XAMPP Control Panel, click Stop next to the services (Apache, MySQL) you wish to stop.
- It’s good practice to stop services when you’re not using them, to free up resources.
Part 7: Troubleshooting
Common Issues and Fixes:
- Apache not starting: If Apache won’t start, make sure no other application is using port 80 (e.g., Skype). You can change Apache’s port as shown in Part 5.
- Database connection errors: Double-check your database name, username, and password. By default, the MySQL/MariaDB username is
root, and there is no password (it can be set later if needed).
Conclusion
XAMPP is a powerful tool for local web development, allowing you to run Apache, PHP, and MySQL (or MariaDB) on your computer without needing an internet connection. This tutorial covers the basics of setting up XAMPP, starting services, setting up a local website, managing databases with phpMyAdmin, and troubleshooting common issues.