jeremygo

jeremygo

我是把下一颗珍珠串在绳子上的人

Setting up a Web server on Centos7

Recently, I installed Centos7 on a virtual machine to configure a lamp server, and encountered many challenges...o(╥﹏╥)o. I must make a record of it.

Update: Installation of nginx and node

Centos7 Installation#

  • Here, you just need to download the ISO image from the internet and install it step by step. It is recommended for beginners to choose the GNONE desktop version for easier operation.

Installation of Apache, Mysql, PHP#

  • Apache
    The package name for Apache is httpd.
    yum install httpd
    When prompted, press y+Enter to continue.

    Start Apache and set it to start on boot.
    systemctl start httpd.service
    systemctl enable httpd.service
    Check the status of the httpd service:
    systemctl status httpd.service
    If you see active(running) in green, it means that the httpd service is running, and enabled means that the httpd service is set to start on boot.

    Now, the HTTP protocol is enabled. Since it uses port 80, the firewall needs to allow access to port 80. Here, we will simply disable the firewall.
    systemctl stop firewalld.service Stop the firewall
    systemctl disable firewalld.service Disable it from starting on boot

    You can now use ip addr to check the current system IP address, and access the Apache page in a browser.
    Tips: Here, you can configure the internal IP of the virtual machine as static, and then modify the hosts file on the host to correspond to an alias such as "myserver". After that, you can directly access it using http://myserver.

  • PHP
    yum install php
    If needed, you can make some modifications in /etc/php.ini.
    To test if Apache can call PHP correctly, create a file in the /var/www/html directory that outputs phpinfo.
    vim /var/www/html/phpinfo.php
    If you can see the PHP information page when you open it in a browser, it means that it is working correctly.
    Then, install commonly used extensions.
    yum -y install php-mysql php-gd php-imap php-ldap php-mbstring php-odbc php-pear php-xml php-xmlrpc

  • Mysql
    Here, it is recommended to install MariaDB (starting from RHEL 7, Red Hat recommends using it as a replacement for Mysql).
    yum install mariadb-server mariadb
    systemctl start mariadb Start mariadb
    systemctl enable mariadb Set it to start on boot

    Here, you can start the database daemon.
    mysql_secure_installation
    This is used to set the root password, allow remote root login, and more.

    mysql -u root -p Use the root account to log in to mariadb
    show mysql Switch to the mysql database
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; Modify the login permissions to allow remote login

Configuring Apache Virtual Directories#

The default project directory for Apache is located at /var/www, and editing this directory requires root user access. Therefore, it is necessary to customize a directory that Apache can recognize.
Open the Apache configuration file by running vim /etc/httpd/conf/httpd.conf.
Find <dir alias_module> and add the following inside:

Example:
Alias /myweb "/home/daybreak/www"
<Directory "/home/daybreak/www">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Here, my custom path is /home/daybreak/www, and the corresponding alias is myweb. You can access the files in the actual directory /home/daybreak/www by entering http://myserver/myweb in a browser.

Note: When accessing directly, you may encounter a 403 forbidden error. Here are the solutions:

  1. Make sure that the directory access permissions are set when configuring the virtual directory, i.e., Require all granted.
  2. It may be a permission issue with the website directory. Apache requires the directories to have execute permissions (x), so make sure that the directories you are accessing have these permissions. For example, if my directory is /home/daybreak/www, you need to run:
chmod 755 /home
chmod 755 /home/daybreak
chmod 755 /home/daybreak/www
or simply chmod 755 -R /home
  1. If you still encounter a 403 error, it may be an issue with selinux. Set the selinux permissions for the directory using the following command:
    chcon -R -t httpd_sys_content_t /home

After successfully accessing, you can access the web documents through the custom directory.

Extension: Installation of nginx and node#

Installation of nginx#

  • Add the centos7 nginx yum repository:

    sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    
  • Install using yum:

    sudo yum install -y nginx
    
  • Start nginx and enable it to start on boot:

    sudo systemctl start nginx && sudo systemctl enable nginx
    
  • Test the nginx configuration file (to check the nginx configuration location):

    nginx -t
    

Installation of node#

  • Install using the official compiled binary package:

    • Go to the official website download link, choose the version link and version (*-linux-x64.tar.gz), and use the wget command in the user's home directory to download:

      wget https://nodejs.org/download/release/v8.9.4/node-v8.9.4-linux-x64.tar.gz
      
    • After the download is complete, extract it to the /usr/local directory and install:

      sudo tar --strip-components 1 -xzvf node-v* -C /usr/local
      
    • After the installation is complete, verify the installation:

      node -v
      v8.9.4
      
  • Installation from source:

    • The difference between installing from source and installing from a binary package is that installing from source requires compiling the source code before installation.

    • Go to the official website download link, and choose the version (node-v*.tar.gz):

      wget https://nodejs.org/download/release/v8.9.4/node-v8.9.4.tar.gz
      
    • After the download is complete, extract it and go to the extracted directory:

      tar xzvf node-v8.9.4.tar.gz && cd node-v8.9.4
      
    • To compile the source code, you need to install gcc and gcc-c++ (you can use yum info package_name to check if they are already installed):

      sudo yum install gcc gcc-c++
      
    • After the installation is complete, run the configure file and compile it. After the compilation is complete, install it:

      ./configure && make && make install
      
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.