A complete step-by-step guide to setting up a web server on CentOS

WBOY
Release: 2023-08-06 13:29:16
Original
2371 people have browsed it

A complete step-by-step guide to setting up a web server on CentOS

Building your own web server is an important skill that allows you to host your own website or application on the Internet. This article will introduce you to a complete guide to setting up a web server on the CentOS operating system, including steps such as installing the necessary software, configuring the server, and setting up firewall rules.

Step 1: Install the necessary software

First, you need to install some necessary software, including Apache web server, MySQL database and PHP interpreter. Enter the following command in the terminal to install them:

sudo yum install httpd mysql-server php php-mysql
Copy after login

Step 2: Start and configure services

After the installation is complete, you need to start and configure these services. First start the Apache server and enter the following command in the terminal:

sudo service httpd start
Copy after login

Then, set it to start at boot:

sudo chkconfig httpd on
Copy after login

Next, start the MySQL database service:

sudo service mysqld start
Copy after login

Likewise, set it to start at boot:

sudo chkconfig mysqld on
Copy after login

Step 3: Set firewall rules

On CentOS, the firewall is enabled by default, but it may block access to the web server Access. Therefore, you need to configure firewall rules to allow HTTP and HTTPS traffic through. Enter the following command in the terminal:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Copy after login

Step Four: Create and Configure Virtual Host

Next, you need to configure the virtual host to host multiple websites or applications. Enter the following command in the terminal to create a virtual host configuration file:

sudo nano /etc/httpd/conf.d/example.conf
Copy after login

In the file that opens, enter the following code example:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html/example
    ErrorLog /var/log/httpd/example_error.log
    CustomLog /var/log/httpd/example_access.log combined

    <Directory /var/www/html/example>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
Copy after login

Replace "example.com" with your domain name or IP address and replace "/var/www/html/example" with your website directory. After saving and closing the file, restart the Apache server:

sudo service httpd restart
Copy after login

Step 5: Test the server

Now, your web server has been set up and your website or application can be accessed through the browser program. Enter your domain name or IP address into your browser and you should be able to see your website's homepage.

If you want to use PHP in your website, you can create a simple test file to verify that PHP is working properly. Create a file named "test.php" in the website directory with the following content:

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

After saving and closing the file, reload the web page and you will see the PHP information page.

Summary:

By following the above steps, you have successfully set up your own web server on CentOS. You can host any type of website or application using this server. Explore further and learn how to configure and secure your server to ensure it runs securely and efficiently. Hope this complete guide helps!

The above is the detailed content of A complete step-by-step 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!