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

王林
Release: 2023-08-04 18:04:45
Original
1137 people have browsed it

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
    Copy after login
  2. Install After completion, start the Apache service and set it to start automatically at boot:

    sudo systemctl start httpd
    sudo systemctl enable httpd
    Copy after login
  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
    Copy after login
  2. Install After completion, start the MySQL service and set it to start automatically at boot:

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

    sudo mysql_secure_installation
    Copy after login
  4. Use the following command for MySQL account management:

    sudo mysql
    Copy after login

    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
    Copy after login
  2. After the installation is complete, restart the Apache service:

    sudo systemctl restart httpd
    Copy after login
    Copy after login
  3. Create a php test file and enter the following content:

    <?php
    phpinfo();
    ?>
    Copy after login

    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
    Copy after login
  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">
    Copy after login
  3. Save and exit, and Restart the Apache service:

    sudo systemctl restart httpd
    Copy after login
    Copy after login
  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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template