Home  >  Article  >  Operation and Maintenance  >  Introductory Tutorial: A quick guide to setting up a web server on CentOS

Introductory Tutorial: A quick guide to setting up a web server on CentOS

王林
王林Original
2023-08-04 18:04:451121browse

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

  1. Open the terminal and install the Apache server through the following command:

    sudo yum install httpd
  2. Install After completion, start the Apache service and set it to start automatically at boot:

    sudo systemctl start httpd
    sudo systemctl enable httpd
  3. Verify whether Apache is successfully installed:
    Enter the IP address or domain name of the server in the browser, if it can be seen The Apache welcome page indicates that the installation is successful.

Step 2: Install and configure MySQL

  1. Use the following command to install the MySQL database:

    sudo yum install mysql-server
  2. Install After completion, start the MySQL service and set it to start automatically at boot:

    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. Perform basic security settings, enter the following commands and follow the prompts:

    sudo mysql_secure_installation
  4. 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

  1. Use the following command to install PHP:

    sudo yum install php php-mysql
  2. After the installation is complete, restart the Apache service:

    sudo systemctl restart httpd
  3. 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

  1. Edit the Apache configuration file and enter the following command:

    sudo vi /etc/httpd/conf/httpd.conf
  2. 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">
  3. Save and exit, and Restart the Apache service:

    sudo systemctl restart httpd
  4. Create the example folder in the /var/www/html directory and place the website source code file in this folder.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn