1. What is Apache?¶
Before starting to build a website, let’s briefly understand the main character—Apache. Apache is one of the most popular web server software in the world. It is open-source and free, and can run stably on systems like Linux and Windows. Its primary function is to receive and respond to web page requests from browsers. For beginners, it is easy to install and flexible to configure, making it an excellent choice for learning Linux server deployment.
2. Preparation Work¶
Before getting started, ensure you meet the following conditions:
1. A Linux server: This can be a cloud server (e.g., CentOS/Ubuntu instances on Alibaba Cloud or Tencent Cloud) or a local virtual machine (e.g., CentOS or Ubuntu installed via VMware).
2. Basic Linux operation skills: Ability to use the command line (e.g., cd, ls, vim), and basic concepts of files and permissions.
3. Permissions: Need root or sudo privileges (i.e., a user who can execute apt/yum installation commands).
3. Install Apache Server¶
Linux systems have different package management tools. The installation steps for mainstream distributions are introduced below:
1. CentOS/RHEL System (using yum command)¶
- Update system package list (optional but recommended):
sudo yum update -y
- Install Apache:
sudo yum install httpd -y
(httpd is the package name for Apache in CentOS)
2. Ubuntu/Debian System (using apt command)¶
- Update system package list:
sudo apt update
- Install Apache:
sudo apt install apache2 -y
(apache2 is the package name for Apache in Ubuntu)
4. Start Apache and Set It to Start Automatically on Boot¶
After installation, you need to start the Apache service and set it to run automatically when the server restarts:
Start the service:¶
- CentOS:
sudo systemctl start httpd
- Ubuntu:
sudo systemctl start apache2
Set to start automatically on boot:¶
- CentOS:
sudo systemctl enable httpd
- Ubuntu:
sudo systemctl enable apache2
Check service status:¶
Use the following command to confirm if Apache is running properly:
sudo systemctl status httpd # CentOS
sudo systemctl status apache2 # Ubuntu
If the output shows active (running), it means the startup was successful.
5. Open Firewall Port (Critical Step!)¶
If the server has a firewall enabled (e.g., firewalld on CentOS or ufw on Ubuntu), you need to open port 80 (the default HTTP port), otherwise, the website will not be accessible externally.
1. CentOS (using firewalld):¶
- Open port 80 and make it permanent:
sudo firewall-cmd --add-port=80/tcp --permanent
- Reload firewall rules:
sudo firewall-cmd --reload
2. Ubuntu (using ufw):¶
- Allow access to port 80:
sudo ufw allow 80/tcp
- Confirm firewall status:
sudo ufw status # If "80/tcp" is listed as allowed, it is successful
6. Deploy Your First Website¶
Now that Apache is installed and accessible, we need to place website files in Apache’s default website directory.
1. Find the website root directory:¶
- CentOS:
/var/www/html - Ubuntu:
/var/www/html(same path across different systems)
2. Create a simple HTML file:¶
Enter the website directory and create an index.html file (the default name for the website homepage) using vim:
cd /var/www/html
sudo vim index.html
In the vim editor, enter the following content (a simple HTML page):
<!DOCTYPE html>
<html>
<head>
<title>My First Linux Website</title>
</head>
<body>
<h1>Hello, Linux Server!</h1>
<p>This is my first website built with Apache.</p>
</body>
</html>
Press Esc and type :wq to save and exit vim.
7. Test Website Access¶
-
Restart Apache service:
To apply changes, usereloadfor a lighter operation:
- CentOS:sudo systemctl reload httpd
- Ubuntu:sudo systemctl reload apache2 -
Access via browser:
In a local or other device’s browser, enter the server’s public IP address (if using a cloud server, ensure port 80 is open in the security group). For example:
http://123.45.67.89
You should see theindex.htmlpage you created, displaying “Hello, Linux Server!”.
8. Troubleshooting Common Issues¶
If access fails, follow these steps to check:
1. Confirm Apache status: Run systemctl status httpd/apache2 to ensure the service is active.
2. Check firewall: Run firewall-cmd --list-ports (CentOS) or ufw status (Ubuntu) to confirm port 80 is open.
3. File permissions: The website directory and files may require permissions. Ensure the Apache user (CentOS: apache; Ubuntu: www-data) has read access.
For example: sudo chown -R apache:apache /var/www/html (CentOS) or sudo chown -R www-data:www-data /var/www/html (Ubuntu).
4. File path: Confirm the HTML file is in /var/www/html and named index.html (the default homepage).
9. Advanced: Simple Virtual Host Configuration (Optional)¶
If you need to host multiple websites (e.g., example.com and test.com), you can use virtual hosts. Taking CentOS as an example:
1. Create a website directory: sudo mkdir -p /var/www/example.com
2. Create an HTML file: sudo vim /var/www/example.com/index.html
3. Edit the configuration file: sudo vim /etc/httpd/conf.d/example.com.conf
4. Add the following content (replace example.com with your domain):
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Require all granted
</Directory>
</VirtualHost>
- Restart Apache:
sudo systemctl restart httpd - Access
http://example.comvia browser (after configuring domain resolution) to view the corresponding website.
10. Summary¶
Following the above steps, you have successfully built your first website on a Linux server using Apache! The core process is: Install Apache → Open ports → Deploy files → Test access. You can further explore advanced topics like PHP environment setup and SSL certificate configuration to enhance your website.
Key reminder: Server security is crucial. It is recommended to regularly update the system and Apache, avoid using weak passwords, and consider adding security modules like mod_security in the future.