Home > Article > Operation and Maintenance > Introductory Tutorial: A quick guide to setting up a web server on CentOS
Entry-level tutorial: A quick guide to building a web server on CentOS
Introduction:
In today's Internet era, building your own web server has become a need for many people. This article will introduce you to how to build a web server on the CentOS operating system, and provide code examples to help readers quickly implement it.
Step one: Install and configure Apache
Open the terminal and install the Apache server through the following command:
sudo yum install httpd
Install After completion, start the Apache service and set it to start automatically at boot:
sudo systemctl start httpd sudo systemctl enable httpd
Step 2: Install and configure MySQL
Use the following command to install the MySQL database:
sudo yum install mysql-server
Install After completion, start the MySQL service and set it to start automatically at boot:
sudo systemctl start mysqld sudo systemctl enable mysqld
Perform basic security settings, enter the following commands and follow the prompts:
sudo mysql_secure_installation
Use the following command for MySQL account management:
sudo mysql
After entering the MySQL command line mode, you can create new users, authorizations, etc.
Step Three: Install and Configure PHP
Use the following command to install PHP:
sudo yum install php php-mysql
After the installation is complete, restart the Apache service:
sudo systemctl restart httpd
Create a php test file and enter the following content:
<?php phpinfo(); ?>
Save and exit. Access this file in the browser. If you can see the PHP configuration information, the installation is successful.
Step 4: Configure the virtual host
Edit the Apache configuration file and enter the following command:
sudo vi /etc/httpd/conf/httpd.conf
Find the two lines DocumentRoot
and Directory
and modify them to the required directory path, for example:
DocumentRoot /var/www/html/example <Directory "/var/www/html/example">
Save and exit, and Restart the Apache service:
sudo systemctl restart httpd
Summary:
Through the above four steps, we successfully built a web server on CentOS. Readers can further configure and adjust according to their own needs, such as adding SSL certificates, setting firewall rules, etc. I hope this article can help beginners who are building web servers, so that everyone can get started quickly and enjoy the fun of web development.
Code example:
<!DOCTYPE html> <html> <body> <?php echo "Hello World!"; ?> </body> </html>
The above is a simple PHP sample code that outputs Hello World
to the browser. Readers can access this code file by setting a virtual host in the Apache configuration file.
The above is the detailed content of Introductory Tutorial: A quick guide to setting up a web server on CentOS. For more information, please follow other related articles on the PHP Chinese website!