Home > Article > Operation and Maintenance > Best practices for building web servers under CentOS 7
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)
Open the terminal and execute the following command to install Apache:
sudo yum install httpd
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
Configure the firewall to allow HTTP (port 80) access:
sudo firewall-cmd --zone=public --add-service=http --permanent sudo firewall-cmd --reload
Modify the Apache configuration file:
sudo vi /etc/httpd/conf/httpd.conf
Set ServerName (if not set):
ServerName your_domain_name
Configure the website root directory:
DocumentRoot /var/www/html <Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
Restart Apache for the changes to take effect:
sudo systemctl restart httpd
3. Install and configure MySQL (database server)
Execute the following command to install the MySQL server:
sudo yum install mariadb-server
After the installation is complete, start MySQL and set it to start automatically at boot:
sudo systemctl start mariadb sudo systemctl enable mariadb
Run the security script and configure the MySQL root password:
sudo mysql_secure_installation
4. Install PHP
Execute the following command to install PHP and Related extensions:
sudo yum install php php-mysql
Modify php.ini configuration file:
sudo vi /etc/php.ini
Set time zone:
date.timezone = Asia/Shanghai
Restart Apache to make the configuration take effect:
sudo systemctl restart httpd
5. Create and test the website
Create one in the /var/www/html directory Simple index.php file:
sudo vi /var/www/html/index.php
Enter the following code:
<?php phpinfo(); ?>
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!