Home lamp
Post
Cancel

lamp

LAMP CentOS

Ensure that the Linode’s hostname is set.

Check your Linode’s hostname.

1
2
hostname
hostname -f

Update your system:

1
sudo yum update

Apache

  • Install Apache
    1
    
    sudo yum install httpd
    
  • Enable Apache to start at boot and start the Apache service:
    1
    2
    
    sudo systemctl enable httpd.service
    sudo systemctl start httpd.service
    
  • Create a httpd-mpm.conf file and add the code in the example to turn off KeepAlive and adjust the resource use settings. The settings shown below are a good starting point for a Linode 2GB:

  • As a best practice, you should create a backup of your Apache configuration file, before making any configuration changes to your Apache installation. To make a backup in your home directory:

    1
    
     cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup
    
    1
    
     File: /etc/httpd/conf.modules.d/httpd-mpm.conf
    
    1
    2
    3
    4
    5
    6
    7
    8
    
     KeepAlive Off
     <IfModule prefork.c
        StartServers        4
        MinSpareServers     20
        MaxSpareServers     40
        MaxClients          200
        MaxRequestsPerChild 4500
     </IfModule
    

Configure Name-based Virtual Hosts

  • Create the directories to store your site files and logs. Replace example.com with your own site’s domain name.
    1
    
    sudo mkdir -p /var/www/html/example.com/{public_html,logs}
    
  • Create the directories to store your site’s virtual hosts files:
    1
    
    sudo mkdir -p /etc/httpd/sites-available /etc/httpd/sites-enabled
    
  • Edit Apache’s configuration file to let it know to look for virtual host files in the /etc/httpd/sites-enabled directory. Add the example line to the bottom of your httpd.conf file:

    /etc/httpd/conf/httpd.conf

    IncludeOptional sites-enabled/*.conf

  • Navigate to your /var/www/html/example.com directory if you are not already there:
    1
    
    cd /var/www/html/example.com
    
  • Using your preferred text editor create a virtual hosts file. Copy the basic settings in the example below and paste them into the file. Replace all instances of example.com with your domain name.

    /etc/httpd/sites-available/example.com.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
     <Directory /var/www/html/example.com/public_html
       Require all granted
     </Directory
     <VirtualHost *:80
       ServerName example.com
       ServerAdmin webmaster@localhost
       DocumentRoot /var/www/html/example.com/public_html
       ErrorLog /var/www/html/example.com/logs/error.log
       CustomLog /var/www/html/example.com/logs/access.log combined
     </VirtualHost
    
  • Create a symbolic link from your virtual hosts file in the sites-available directory to the sites-enabled directory. Replace example.com.conf with the name of your own virtual hosts file.
    1
    
    sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
    
  • Reload to apply your new configuration:
    1
    
    sudo systemctl reload httpd.service
    
  • Configure SELinux to Allow HTTP
    1. Use chown to make apache the owner of the web directory:
      1
      
      sudo chown apache:apache -R /var/www/html/example.com/
      
    2. Modify the permissions for files and directories:
      1
      2
      3
      
      cd /var/www/html/example.com/
      find . -type f -exec sudo chmod 0644 {} \;
      find . -type d -exec sudo chmod 0755 {} \;
      
    3. Use SELinux’s chcon to change the file security context for web content:
      1
      2
      
      sudo chcon -t httpd_sys_content_t /var/www/html/example.com -R
      sudo chcon -t httpd_sys_rw_content_t /var/www/html/example.com -R
      
    4. Enable Apache to start at boot, and restart the service for the above changes to take place:
      1
      2
      
      sudo systemctl enable httpd.service
      sudo systemctl restart httpd.service
      
  • Configure FirewallD to Allow HTTP and HTTPS Connections
    1. View the default set of services:
      1
      
      sudo firewall-cmd --zone=public --list-services
      
    2. To allow connections to Apache, add HTTP and HTTPS as a service:
      1
      2
      3
      4
      
      sudo firewall-cmd --zone=public --add-service=http --permanent
      sudo firewall-cmd --zone=public --add-service=https --permanent
      sudo firewall-cmd --zone=public --add-service=http
      sudo firewall-cmd --zone=public --add-service=https
      

MariaDB

  1. Install the MariaDB-server package:
    1
    
    sudo yum install mariadb-server
    
  2. Set MariaDB to start at boot and start the daemon for the first time:
    1
    2
    
    sudo systemctl enable mariadb.service
    sudo systemctl start mariadb.service
    
  3. Run mysql_secure_installation to secure MariaDB. You will be given the option to change the MariaDB root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases and reload privileges. It is recommended that you answer yes to these options:
    1
    
    sudo mysql_secure_installation
    
  4. Create a MariaDB Database
    • Log in to MariaDB:
      1
      
      mysql -u root -p
      
    • Create a new database and user with permissions to use it:
      1
      2
      
      create database webdata;
      grant all on webdata.* to 'webuser' identified by 'password';
      

PHP

  1. Install PHP:

    1
    
    sudo yum install php php-pear php-mysqlnd
    
  2. Edit /etc/php.ini for better error messages and logs, and upgraded performance. These modifications provide a good starting point for a Linode 2GB:

    1
    
    File: /etc/php.ini
    
    1
    2
    3
    
    error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
    error_log = /var/log/php/error.log
    max_input_time = 30
    
  3. Create the log directory for PHP and give the Apache user ownership:
    1
    2
    
    sudo mkdir /var/log/php
    sudo chown apache:apache /var/log/php
    
  4. You may need to enable and start the php-fpm.service. This service provides an alternative PHP FastCGI implementation.
    1
    2
    
    sudo systemctl enable php-fpm.service
    sudo systemctl start php-fpm.service
    
  5. Reload Apache:
    1
    
    sudo systemctl reload httpd.service
    

Test and TroubleShoot the LAMP Stack

  • Paste the following code into a new file, phptest.php, in the public_html directory. Modify webuser and password to match the information entered in the Create a MariaDB Database section above: File: /var/www/html/example.com/public_html/phptest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html
<head
<titlePHP Test</title
</head
    <body
    <?php echo '<pHello World</p';

    // In the variables section below, replace user and password with your own MariaDB credentials as created on your server
    $servername = "localhost";
    $username = "webuser";
    $password = "password";

    // Create MariaDB connection
    $conn = mysqli_connect($servername, $username, $password);

    // Check connection - if it fails, output will include the error message
    if (!$conn) {
    die('<pConnection failed: </p' . mysqli_connect_error());
    }
    echo '<pConnected successfully</p';
    ?
    </body
</html
  • Navigate to example.com/phptest.php from your local machine. If the components of your LAMP stack are working correctly, the browser will display a “Connected successfully” message. If not, the output will be an error message.

  • Remove the test file:

This post is licensed under CC BY 4.0 by the author.