DigitalOcean: Install Apache & PHP - Quick Guide

by Jhon Lennon 49 views

Alright, guys! Let's dive straight into setting up Apache and PHP on your DigitalOcean droplet. This guide is designed to be super easy to follow, even if you're not a tech whiz. We'll cover everything step-by-step, ensuring you get your web server up and running smoothly. So, let's get started!

Why Apache and PHP?

Before we jump into the how-to, let's quickly chat about why Apache and PHP are such a popular combo. Apache is one of the most widely used web servers out there. It's reliable, flexible, and can handle a ton of traffic. Think of it as the engine that powers your website, serving up all your files to visitors. PHP, on the other hand, is a scripting language that's perfect for creating dynamic web content. It lets you do things like connect to databases, process forms, and build interactive elements on your site. Together, they're a match made in web development heaven, especially for those using platforms like WordPress, Drupal, or custom PHP applications.

Prerequisites

Before we begin, there are a couple of things you'll need:

  • A DigitalOcean Droplet: You should already have a droplet set up. If not, head over to DigitalOcean and create one. A basic Ubuntu droplet will work perfectly for this guide.
  • SSH Access: Make sure you can connect to your droplet via SSH. You'll need this to run commands and configure your server.
  • Basic Terminal Knowledge: Knowing a few basic terminal commands will be helpful. Don't worry, we'll walk you through everything, but a little familiarity goes a long way.

Step 1: Connect to Your DigitalOcean Droplet

First things first, you need to connect to your DigitalOcean droplet. Open your terminal and use the following command:

ssh root@your_droplet_ip

Replace your_droplet_ip with the actual IP address of your droplet. If this is your first time connecting, you might see a warning about the authenticity of the host. Just type yes and hit enter. You'll then be prompted for your root password. Enter it, and you're in!

Step 2: Update the Package Repository

Once you're connected, it's a good idea to update your package repository. This ensures you're getting the latest versions of the software. Run the following commands:

apt update
apt upgrade

The apt update command refreshes the package lists, while apt upgrade installs the newest versions of all packages currently installed on your system. This might take a few minutes, so sit tight.

Step 3: Install Apache

Now, let's get Apache installed. This is super easy. Just run:

apt install apache2

You'll be asked if you want to continue. Type y and hit enter. The installation will proceed, and Apache will be installed on your droplet.

Once the installation is complete, it's a good idea to check if Apache is running. You can do this with the following command:

systemctl status apache2

If Apache is running, you'll see a message indicating that it's active and running. If it's not, you can start it with:

systemctl start apache2

You can also enable Apache to start automatically on boot with:

systemctl enable apache2

To verify that Apache is working, open your web browser and navigate to your droplet's IP address. You should see the default Apache welcome page. If you do, congratulations! Apache is up and running.

Step 4: Install PHP

Next up, let's install PHP. This is just as easy as installing Apache. Run the following command:

apt install php libapache2-mod-php php-mysql

This command installs PHP, the Apache module for PHP (libapache2-mod-php), and the MySQL extension for PHP (php-mysql). The MySQL extension is useful if you plan to connect to a MySQL database from your PHP scripts.

Again, you'll be asked if you want to continue. Type y and hit enter. The installation will proceed.

Once PHP is installed, you need to restart Apache to enable the PHP module. You can do this with:

systemctl restart apache2

Step 5: Test PHP

To make sure PHP is working correctly, let's create a simple PHP file and load it in your web browser. First, create a file called info.php in the /var/www/html/ directory:

nano /var/www/html/info.php

Then, add the following PHP code to the file:

<?php
phpinfo();
?>

Save the file and exit the text editor. Now, open your web browser and navigate to http://your_droplet_ip/info.php. You should see a page with detailed information about your PHP installation. If you do, PHP is working perfectly.

Important: It's a good idea to remove this file once you're done testing, as it can expose sensitive information about your server.

rm /var/www/html/info.php

Step 6: Configure Virtual Hosts (Optional)

If you plan to host multiple websites on your droplet, you'll want to set up virtual hosts. This allows you to use different domain names for each website. Here's how to do it:

  1. Create a directory for your website: Create a directory for your website files. For example, if your website is example.com, you might create a directory called /var/www/example.com:

mkdir /var/www/example.com chown -R USER:USER:USER /var/www/example.com chmod -R 755 /var/www ```

  1. Create a virtual host file: Create a virtual host file for your website. This file tells Apache how to handle requests for your domain. Create a new configuration file in /etc/apache2/sites-available/ using nano or your favorite text editor:

nano /etc/apache2/sites-available/example.com.conf ```

Add the following configuration, adjusting the `ServerName`, `DocumentRoot`, and `Directory` directives to match your setup:

```apache
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```

Save and close the file.
  1. Enable the virtual host: Enable the virtual host using the a2ensite command:

a2ensite example.com.conf ```

  1. Disable the default site: Disable the default Apache site:

a2dissite 000-default.conf ```

  1. Restart Apache: Restart Apache to apply the changes:

systemctl restart apache2 ```

Now, when you navigate to your domain name in your web browser, you should see the content from your website directory.

Step 7: Secure Your Website with SSL (Optional but Recommended)

To secure your website with SSL, you can use Let's Encrypt, a free and automated certificate authority. Here's how to do it:

  1. Install Certbot: Install the Certbot Let's Encrypt client:

apt install certbot python3-certbot-apache ```

  1. Obtain an SSL certificate: Run Certbot to obtain an SSL certificate for your domain:

certbot --apache -d example.com -d www.example.com ```

Certbot will ask you a few questions, such as your email address and whether you want to redirect HTTP traffic to HTTPS. Answer the questions and Certbot will automatically configure Apache to use SSL.
  1. Verify SSL: Open your web browser and navigate to https://your_domain_name. You should see the padlock icon in your browser's address bar, indicating that your connection is secure.

Conclusion

And that's it! You've successfully installed Apache and PHP on your DigitalOcean droplet. You can now start building and deploying your web applications. Remember to keep your server updated and secure. Happy coding, folks! By following these steps, you've set up a solid foundation for your web projects. Whether you're building a blog, an e-commerce site, or a complex web application, you now have the tools you need to get started. Don't be afraid to explore and experiment with different configurations and technologies. The world of web development is vast and exciting, and there's always something new to learn. Keep practicing and building, and you'll be amazed at what you can achieve. And remember, the internet is full of resources and communities that can help you along the way. So don't hesitate to ask questions and seek help when you need it. With a little bit of effort and a lot of curiosity, you can become a proficient web developer and create amazing things. Now go out there and build something awesome!