Home >Operation and Maintenance >Linux Operation and Maintenance >Starting from Scratch: A Complete Guide to Building a Web Server on CentOS

Starting from Scratch: A Complete Guide to Building a Web Server on CentOS

王林
王林Original
2023-08-05 09:03:163462browse

Start from scratch: A complete guide to building a web server on CentOS

1. Install the CentOS operating system
To build a web server, you first need to install the operating system. This article uses CentOS as an example. Download and install the CentOS operating system, select the latest version of CentOS. After completing the installation, ensure that the system has been properly configured for network connections.
2. Install LAMP (Linux, Apache, MySQL, PHP) environment

  1. Install Apache
    Execute the following command in the terminal to install Apache:
    sudo yum install httpd
    After the installation is complete, start Apache:
    sudo systemctl start httpd
    At this time, you can enter the IP address of the server in the browser. If the Apache welcome page appears, it means the installation is successful.
  2. Install MySQL
    Execute the following command in the terminal to install MySQL:
    sudo yum install mariadb-server
    After the installation is complete, start MySQL:
    sudo systemctl start mariadb
    Then run the following command to configure MySQL security:
    sudo mysql_secure_installation
    Follow the prompts to configure. It is recommended to set the root user's password to a complex string and prohibit anonymous user access. After the configuration is complete, restart MySQL:
    sudo systemctl restart mariadb
  3. Install PHP
    Execute the following command in the terminal to install PHP and its related modules:
    sudo yum install php php-mysql php -gd php-pear
    After the installation is complete, restart Apache:
    sudo systemctl restart httpd
    3. Basic configuration
  4. Modify the Apache configuration file
    Open the main configuration of Apache file, open /etc/httpd/conf/httpd.conf using any text editor. Find the following line and modify it:

    ServerName www.example.com:80

    Change "www.example.com" to your domain name or server IP address. Save the file and exit.

  5. Firewall Settings
    If your CentOS system has enabled the firewall, you need to add rules to allow HTTP and HTTPS traffic to pass. Execute the following command in the terminal:
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    Reload the firewall rules:
    sudo firewall-cmd --reload
    4. Website deployment and management
  6. Create website directory
    Create a new directory under the /var/www/html directory as your website root Table of contents. For example, execute the following command to create a directory named mywebsite:
    sudo mkdir /var/www/html/mywebsite
  7. Set file permissions
    Execute the following command to set the owner of the website directory to User and group for the Apache process:
    sudo chown -R apache:apache /var/www/html/mywebsite
  8. Writing website code
    Write your website code through any text editor and then put It is saved to the mywebsite directory.
  9. Configuring virtual host
    Open Apache’s virtual host configuration file /etc/httpd/conf.d/virtualhost.conf and use the following code example as a reference:

    ServerAdmin webmaster@mywebsite.com
    DocumentRoot /var/www/html/mywebsite
    ServerName mywebsite.com
    ServerAlias ​​www.mywebsite.com
    ErrorLog /var/log/httpd/mywebsite_error.log
    CustomLog /var/log/httpd/mywebsite_access.log combined

    Save the file and exit. Then restart Apache:
    sudo systemctl restart httpd

5. Test website
Enter your server IP address or domain name in the browser. If you can see your website page, Indicates that the website is successfully built.

6. Conclusion
Through the guide in this article, you have learned how to build a web server on CentOS from scratch. These are the necessary steps to build a basic web server. I hope this article can help you. If you are deploying your website into a production environment, ensure proper security configuration and performance optimization.

The above is the detailed content of Starting from Scratch: A Complete Guide to Building a Web Server 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