Home  >  Article  >  Operation and Maintenance  >  Precautions for operation, maintenance and performance optimization of building web servers on CentOS

Precautions for operation, maintenance and performance optimization of building web servers on CentOS

WBOY
WBOYOriginal
2023-08-09 14:03:291479browse

Precautions for operation, maintenance and performance optimization of building web servers on CentOS

Operation, maintenance and performance optimization considerations for building a web server on CentOS

With the rapid development of the Internet, building your own web server has become an issue for more and more companies and individuals. needs. As a free and stable operating system, CentOS has become the first choice of many people. This article will introduce some operation and maintenance and performance optimization considerations when building a web server on CentOS, and provide some code examples.

  1. Installing and Configuring Apache

First, we need to install Apache as our web server. In CentOS, you can use the following command to install:

yum install httpd

After the installation is complete, we need to do some configuration. Open Apache's main configuration file httpd.conf, usually located at /etc/httpd/conf/httpd.conf, and make the following settings:

  • Modify ServerName, set the domain name or IP address of the server.
  • ModifyDocumentRoot and set the root directory of the website.
  • Configure a virtual host (if required) by adding the <virtualhost></virtualhost> tag.

After the configuration is completed, use the following command to start Apache:

systemctl start httpd
  1. Firewall settings

In order to protect the security of the server, we need to set firewall rules . CentOS uses firewalld to manage firewalls. The following are some commonly used commands:

  • Check the firewall status:

    systemctl status firewalld
  • Turn on the firewall:

    systemctl start firewalld
  • Add allowed ports:

    firewall-cmd --permanent --add-port=80/tcp
  • Restart the firewall:

    firewall-cmd --reload
    ##Install and configure MySQL
Many websites need to use databases to store data, so we also need to install MySQL. In CentOS, you can use the following command to install:

yum install mysql-server

After the installation is complete, start MySQL and set it to start automatically at boot:

systemctl start mysqld
systemctl enable mysqld

Next, we need to do some configuration. You can use the following command to set the MySQL root password:

mysql_secure_installation

Follow the prompts to set the password and complete other security configurations.

    PHP Configuration
PHP is a commonly used server-side scripting language that we can use with MySQL and Apache. There are many ways to install PHP on CentOS. Here we choose to use

php-fpm for installation and configuration. The following are the installation steps:

  • Install PHP and related extensions:

    yum install php php-fpm php-mysql

  • Modify the
  • php.ini file and find the date .timezone and set the time zone.
  • Start php-fpm:

    systemctl start php-fpm
    systemctl enable php-fpm

    Performance optimization considerations
For a web server, performance optimization is very important. The following are some areas that can be optimized:

  • Use HTTP caching to reduce server load. Caching can be enabled by adding the following configuration in

    httpd.conf:

    LoadModule cache_module modules/mod_cache.so
    <IfModule mod_cache.c>
    CacheEnable disk /
    </IfModule>

  • Compresses transmitted data to reduce network transmission time. You can add the following configuration in

    httpd.conf:

    LoadModule deflate_module modules/mod_deflate.so
    <IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    </IfModule>

  • Reasonably configure the number of Apache processes and threads, and adjust them according to the actual situation.
  • Use the index of the database to speed up the query.
  • Use CDN (Content Delivery Network) to distribute server load and improve access speed.
    Sample code
The following is a simple PHP code example for connecting to a MySQL database and querying data:

<?php
$servername = "localhost";
$username = "root";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM your_table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

Summary

This article introduces some operation and maintenance and performance optimization considerations for building a web server on CentOS, and provides some code examples. I hope readers can successfully build their own web servers through the guidance of this article and provide users with stable and efficient services.

The above is the detailed content of Precautions for operation, maintenance and performance optimization of building web servers 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