How to install LAMP server on CentOS 7

Personally I didn't know how to do it until I found a website that explained it all. I didn't even dare figuring it out because the task just seemed too big.

Before starting the whole process make sure you're root.

sudo -i

I'd also like to point out that I was specifically installing a LAMP stack with MariaDB, not Mysql. So I'm going to show how to install MariaDB only. Although when installed there is probably not much difference syntax wise anyway. And the webpage I used for this is https://www.linuxbabe.com/redhat/install-lamp-stack-centos-8-rhel-8

One more thing before starting is we probably want all the latest updates for CentOS

yum update

Now that we're fully prepared let's install the LAMP stack



Installing, starting Apache, and making sure it runs properly

Install:

yum install httpd httpd-tools

Start:

systemctl start httpd

Make sure it starts on computer/server startup:

systemctl enable httpd

Check that it's running alright:

systemctl status httpd

The last command should output something like this:

● httpd.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
    Active: active (running) since Sat 2019-10-12 06:43:15 UTC; 14s ago
      Docs: man:httpd.service(8)
  Main PID: 14515 (httpd)
    Status: "Running, listening on: port 80"
     Tasks: 213 (limit: 5092)
    Memory: 24.8M
    CGroup: /system.slice/httpd.service
            ├─14515 /usr/sbin/httpd -DFOREGROUND
            ├─14516 /usr/sbin/httpd -DFOREGROUND
            ├─14517 /usr/sbin/httpd -DFOREGROUND
            ├─14518 /usr/sbin/httpd -DFOREGROUND
            └─14519 /usr/sbin/httpd -DFOREGROUND

The output above is taken straight from the website

enabled means Apache will startup itself on boot, you also need to hit q to exit the output

We can check the Apache version:

httpd -v

which should give us something like this:

Server version: Apache/2.4.6 (CentOS)
Server built:   Aug  8 2019 11:41:18

We can really test that it's running properly by adding a file called "index.html" into where Apache is looking to get the files needed to run the website "/var/www/html/" like this:

echo "Welcome to this site" > /var/www/html/index.html

If you look at the website I referenced they also have an exclamation mark at the end of the sentence, I removed it because it was causing me errors.

To test this page out, open your favorite browser and type in the appropriate ip address for your server (if you're doing this on the server you can just type in "127.0.0.1" or "localhost")

A page with a welcome message should pop up. If you're opening the page from a different computer than your server you may want to do this prior to opening the page:

firewall-cmd --permanent --zone=public --add-service=http

This opens up port 80 in firewalld and therefore allows other computers to access the page. (thanks to "--permanent" the port will remain open even if you restart the server)
Now we need to reload firewalld, and then you can look at the page from different computers:

systemctl reload firewalld

As a last step here, let's make sure Apache is the owner of the directory it's gonna be looking in to get files for the website:

chown apache:apache /var/www/html -R

If you add any folders or directories, which will happen if you install wordpress, just run the command again so you don't get any pages telling you something along the lines of "you don't have access to the page"



Installing MariaDB

Install:

yum install mariadb-server mariadb -y

Start:

systemctl start mariadb

Make sure it starts on computer/server startup:

systemctl enable mariadb

Check that it's running alright:

systemctl status mariadb

Above command output (again taken from referenced website):

● mariadb.service - MariaDB 10.3 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-10-12 09:02:53 UTC; 33s ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 18608 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 30 (limit: 5092)
   Memory: 77.0M
   CGroup: /system.slice/mariadb.service
           └─18608 /usr/libexec/mysqld --basedir=/usr

Now let's run its security script:

mysql_secure_installation

In the script just press enter everywhere except when it says New password:, there you want to enter a MariaDB's root password you come up with and save somewhere safe and it will probably ask you to repeat it.

Now if you want to test out MariaDB, you can enter its command line by typing mysql -u root -p. (this enters you into the database as the root user) To exit the database type exit; or press CTRL+C



Installing and testing PHP

Install:

yum install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring -y

Start:

systemctl start php-fpm

Make sure it starts on computer/server startup:

systemctl enable php-fpm

Check that it's running alright:

systemctl status php-fpm

Above command output (again taken from referenced website):

● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-10-12 09:54:37 UTC; 3s ago
 Main PID: 19755 (php-fpm)
   Status: "Ready to handle connections"
    Tasks: 6 (limit: 5092)
   Memory: 24.5M
   CGroup: /system.slice/php-fpm.service
           ├─19755 php-fpm: master process (/etc/php-fpm.conf)
           ├─19757 php-fpm: pool www
           ├─19758 php-fpm: pool www
           ├─19759 php-fpm: pool www
           ├─19760 php-fpm: pool www
           └─19761 php-fpm: pool www

Restart PHP for some changes that happened during installation to take effect:

systemctl restart httpd

Make sure SELinux allows Apache to run PHP code:

setsebool -P httpd_execmem 1

Now if you want to test PHP create and open info.php like this:

nano /var/www/html/info.php

And put this in there:

<?php phpinfo(); ?>

Then go put in the server ip address into the browser and "/info.php" so altogether it will look something like this "localhost/info.php"

A page should open up that tells you all kinds of information about your currently installed PHP

Now I haven't had it happen but the website tells this:

"If the browser fails to display the PHP info but prompt you to download the info.php file, simply restart Apache and PHP-FPM.

sudo systemctl restart httpd php-fpm

Then you should be able to see the PHP info in the web browser."



Last steps

You probably don't want unwanted people looking at your php info page so you can remove it like this:

rm /var/www/html/info.php

You can also remove "index.html" if you'll be installing WordPress so it doesn't interfere with it:

rm /var/www/html/index.html



Post scriptum

Wow, this was the longest post I've ever written anywhere. It was kind of getting on my stamina 😄

Hopefully I managed to explain the installation pretty well. You'll notice I pretty much paraphrased the webpage and added some of my own edits along the way that worked for me. Feel free to write in the comments any questions you have, and have a good one! 🙂

Photo by Pixabay from Pexels

Leave a Reply

Your email address will not be published.