How to Install Apache on Ubuntu 22.04 using WSL (Windows Subsystem for Linux)
Apache is a popular open-source web server that powers a large fraction of the world's websites. In this blog article, we will take you through the installation of Apache on Ubuntu 22.04, allowing you to host and serve web pages and applications on your Ubuntu system.
Prerequisites:
Before you begin, ensure that you have:
- An Ubuntu 22.04 system already installed with administrative access.
- A terminal or command-line interface to execute commands. (For example: Using Visual Studio Code terminal)
- An active internet connection.
Step 1: Update System Packages
Before installing new packages, it's usually a good idea to update your system packages to the most recent versions. Open a terminal window and enter the following commands:
sudo apt update
sudo apt upgrade
This will update the package lists and upgrade any existing packages to their latest versions.
Step 2: Install Apache
To install Apache, execute the following command in the terminal:
sudo apt install apache2
This command will download and install Apache along with its dependencies. You may be prompted to confirm the installation by entering your password.
When prompted to enter your password during the installation of Apache or any other package using the sudo command, simply enter the password for your user account. This is the same password you use to log into your Ubuntu system. Type your password and press Enter, without the password being displayed on the screen. Make sure you have administrative privileges with the user account you are using for the installation.
Step 3: Start and Enable Apache
Once the installation is complete, start the Apache service using the following command:
sudo systemctl start apache2
To have Apache start automatically when the system boots, use the following command:
sudo systemctl enable apache2
Step 4: Verify Apache Installation
To verify that Apache is installed and running correctly, open a web browser and enter your server's IP address or domain name in the address bar. If Apache is running, you should see the default Apache landing page, indicating a successful installation.
To verify that Apache is installed and running correctly, follow these steps:
- Determine your server's IP address:
- Open a terminal on your Ubuntu system.
- Execute the following command: ip addr show
- Look for the line that starts with inet or inet6 followed by your IP address. It may look like inet 192.168.0.100 or inet6 fe80::1234:5678:abcd:efgh/64. Note down this IP address.
- Open a web browser:
- Launch any web browser installed on your computer.
- Access Apache's default landing page:
- In the address bar of the web browser, enter the IP address of your server that you obtained in step 1.
- For example, if your server's IP address is 192.168.0.100, enter http://192.168.0.100 in the address bar.
- Check the Apache landing page:
- If Apache is running correctly, you should see the default Apache landing page in your web browser. This page confirms that Apache is installed and functioning properly.
- The appearance of the default landing page may vary depending on the version of Apache installed, but it usually contains information about Apache and may display a "It works!" message.
If you encounter any issues or see an error page instead of the Apache landing page, double-check the installation steps and configuration settings to ensure everything is set up correctly.
Step 5: Adjust Firewall Settings (Optional)
If you have an active firewall, you may need to allow incoming HTTP (port 80) connections to access your Apache server. Use the following command to enable incoming HTTP traffic:
sudo ufw allow 'Apache'
Step 6: Configure Apache (Optional)
Apache's configuration files are located in the /etc/apache2/ directory. You can modify these files to customize Apache's behavior according to your requirements. However, be cautious when making changes to these files, as incorrect configurations can cause issues.
Step 7: Manage Apache Service
To manage the Apache service, you can use various commands. Some common commands include:
- To stop Apache: sudo systemctl stop apache2
- To restart Apache: sudo systemctl restart apache2
- To check Apache status: sudo systemctl status apache2
Is PHP Included with an Apache Installation?
Apache and PHP are separate software packages. Installing Apache does not automatically install PHP. PHP is a server-side scripting language commonly used for web development, and it needs to be installed separately on your system.
To run PHP scripts with Apache, you will need to install PHP and configure Apache to work with PHP. This typically involves installing the PHP module for Apache, configuring Apache to recognize PHP files, and restarting the Apache service.
You can install PHP on your Ubuntu system by executing the following command in the terminal:
sudo apt install php
This command will install the default version of PHP available in the Ubuntu package repositories.
After installing PHP, you may also need to install additional PHP extensions or modules depending on your specific requirements. These can be installed using the apt package manager as well.
Remember to restart the Apache service after installing PHP and any necessary extensions for the changes to take effect:
sudo systemctl restart apache2
Once both Apache and PHP are installed and properly configured, you can develop and host dynamic websites and applications using PHP scripts on your Ubuntu system.
Conclusion: Congratulations! Apache has been successfully installed on your Ubuntu 22.04 system. You now have a robust web server that can host and deliver web pages and applications. Configure Apache as needed, and then begin developing and delivering your websites and online applications.
Remember to keep Apache up to date and safe by installing updates on a regular basis and according to recommended practices for web server maintenance.
Please keep in mind that the actions detailed in this blog are relevant to Ubuntu 22.04. The installation procedure for other Ubuntu versions may differ significantly.