Home >Operation and Maintenance >Linux Operation and Maintenance >Best practices for building web servers under CentOS 7

Best practices for building web servers under CentOS 7

WBOY
WBOYOriginal
2023-08-06 13:25:061428browse

Best practices for building web servers under CentOS 7

Introduction:
With the rapid development of the Internet, building your own web server has become a need for many people, especially in enterprises and individuals. The website is under construction. This article will introduce the best practices for building a web server under the CentOS 7 operating system and provide relevant code examples.

1. Install Apache (HTTP server)

  1. Open the terminal and execute the following command to install Apache:

    sudo yum install httpd
  2. Installation completed After that, start Apache and set it to start automatically at boot:

    sudo systemctl start httpd
    sudo systemctl enable httpd

2. Configure Apache

  1. Configure the firewall to allow HTTP (port 80) access:

    sudo firewall-cmd --zone=public --add-service=http --permanent
    sudo firewall-cmd --reload
  2. Modify the Apache configuration file:

    sudo vi /etc/httpd/conf/httpd.conf
  3. Set ServerName (if not set):

    ServerName your_domain_name
  4. Configure the website root directory:

    DocumentRoot /var/www/html
    <Directory /var/www/html>
     Options Indexes FollowSymLinks
     AllowOverride All
     Require all granted
    </Directory>
  5. Restart Apache for the changes to take effect:

    sudo systemctl restart httpd

3. Install and configure MySQL (database server)

  1. Execute the following command to install the MySQL server:

    sudo yum install mariadb-server
  2. After the installation is complete, start MySQL and set it to start automatically at boot:

    sudo systemctl start mariadb
    sudo systemctl enable mariadb
  3. Run the security script and configure the MySQL root password:

    sudo mysql_secure_installation

4. Install PHP

  1. Execute the following command to install PHP and Related extensions:

    sudo yum install php php-mysql
  2. Modify php.ini configuration file:

    sudo vi /etc/php.ini
  3. Set time zone:

    date.timezone = Asia/Shanghai
  4. Restart Apache to make the configuration take effect:

    sudo systemctl restart httpd

5. Create and test the website

  1. Create one in the /var/www/html directory Simple index.php file:

    sudo vi /var/www/html/index.php
  2. Enter the following code:

    <?php
    phpinfo();
    ?>
  3. Open the browser, enter the server IP address, and see the phpinfo information indicating the website Accessed successfully.

Conclusion:
Through this article, we learned the best practices for building a web server under the CentOS 7 operating system. The code examples provided above allow you to quickly build and configure a simple web server. However, depending on actual needs, you may need to make more configuration and security considerations. I hope this article can provide you with some help so that you can build your own web server more easily.

The above is the detailed content of Best practices for building web servers under CentOS 7. 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