Home > Article > Operation and Maintenance > 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.
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:
ServerName
, set the domain name or IP address of the server. DocumentRoot
and set the root directory of the website. <virtualhost></virtualhost>
tag. After the configuration is completed, use the following command to start Apache:
systemctl start httpd
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
yum install mysql-serverAfter the installation is complete, start MySQL and set it to start automatically at boot:
systemctl start mysqld systemctl enable mysqldNext, we need to do some configuration. You can use the following command to set the MySQL root password:
mysql_secure_installationFollow the prompts to set the password and complete other security configurations.
php-fpm for installation and configuration. The following are the installation steps:
yum install php php-fpm php-mysql
file and find the
date .timezone and set the time zone.
systemctl start php-fpm systemctl enable php-fpm
httpd.conf:
LoadModule cache_module modules/mod_cache.so <IfModule mod_cache.c> CacheEnable disk / </IfModule>
httpd.conf:
LoadModule deflate_module modules/mod_deflate.so <IfModule mod_deflate.c> SetOutputFilter DEFLATE </IfModule>
<?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(); ?>SummaryThis 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!