Menu

Установка LAMP под Centos 8

LAMP это связка таких категорий как Linux, Apache, MariaDB/MySQL и PHP. Все компоненты - с открытым исходным кодом.

 

Эта статья как-то не задалась, слишком много вопросов. К прочтению не рекомендуется. Вторая часть вставлена на английском.
Всё равно надо отказываться от MariaDB и устанавливать старый добрый MySQL. А лучше вообще переходить с LAMP на LEMP. Когда-нибудь про это появится самостоятельная статья.
su -

Step 1: Update

Сначала обновляем все пакеты.

yum update

Step 2: Установка Apache Web Server

Давайте установим Apache Web server. Пакет httpd-tools установит полезные утилиты, например  benchmarking tool (ab).

yum install httpd httpd-tools

Теперь  запустим Apache:

systemctl start httpd

Добавим в автозагрузку.

systemctl enable httpd

Проверим статус.

systemctl status httpd

Видим вывод:

 httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-02-01 09:40:00 UTC; 9s ago
     Docs: man:httpd.service(8)
 Main PID: 655 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 5092)
   Memory: 20.1M
   CGroup: /system.slice/httpd.service
           ├─1517 /usr/sbin/httpd -DFOREGROUND
           ├─1518 /usr/sbin/httpd -DFOREGROUND
           └─1519 /usr/sbin/httpd -DFOREGROUND

 

Проверим версию.

httpd -v

Вывод:

Server version: Apache/2.4.37 (centos)
Server built: Oct 7 2019 21:42:02

Создадим index.html в корневой директории (/var/www/html/)

echo "Сайт работает!" > /var/www/html/index.html

If you are installing LAMP on your local CentOS 8/RHEL 8 computer, then type 127.0.0.1 or localhost in the browser address bar. You should see the welcome message, which means Apache Web server is running properly.

 

By default, CentOS 8/RHEL 8 forbids public access to port 80. To allow other computers to access the web page, we need to open port 80 in firewalld, the dynamic firewall manager on RHEL/CentOS. Run the following command to open port 80.

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

If you want to enable HTTPS on Apache later, then you also need to open port 443.

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

The --permanent option will make this firewall rule persistent across system reboots. Next, reload the firewall daemon for the change to take effect.

systemctl reload firewalld

Now the Apache web page is accessible publicly.

Finally, we need to make user apache as the owner of web directory. By default it’s owned by the root user.

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

Step 3: Install MariaDB Database Server on CentOS 8/RHEL 8

MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who are concerned that Oracle might turn MySQL into a closed-source product. Enter the following command to install MariaDB on CentOS 8/RHEL 8.

yum install mariadb-server mariadb -y

After it’s installed, we need to start it.

systemctl start mariadb

Enable auto start at system boot time.

systemctl enable mariadb

Check status:

systemctl status mariadb

output:

 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

Enabled” indicates that auto start at boot time is enabled and we can see that MariaDB server is running. Now we need to run the security script.

mysql_secure_installation

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.

Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security. (Note that the letter Y is capitalized, which means it’s the default answer.)

Now you can run the following command and enter MariaDB root password to log into MariaDB shell.

mysql -u root -p

To exit, run

exit;

Step 4: Install PHP on CentOS 8/RHEL 8

Install PHP and some common modules using the following command.

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

Apache web server on CentOS 8/RHEL 8 by default uses PHP-FPM instead of mod_php to run PHP code, so in the above command we also installed php-fpm. After it’s installed, we need to start it.

systemctl start php-fpm

Enable auto start at system boot time.

systemctl enable php-fpm

Check status:

systemctl status php-fpm

output:

 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)
           ├─19759 php-fpm: pool www
           ├─19760 php-fpm: pool www
           └─19761 php-fpm: pool www

Enabled” indicates that auto start at boot time is enabled and we can see that PHP-FPM is running. The php-fpm package installs a php.conf file in /etc/httpd/conf.d/ directory, so we need to restart Apache web server, in order to run PHP code.

systemctl restart httpd

We also need to run the following command to tell SELinux to allow Apache to execute PHP code via PHP-FPM.

setsebool -P httpd_execmem 1

Step 5: Test PHP

To test PHP-FPM with Apache Web server, we need to create a info.php file in the document root directory.

nano /var/www/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. If you installed LAMP stack on a local CentOS 8/RHEL 8 server, type in 127.0.0.1/info.php or localhost/info.php in the browser address bar. You should see your server’s PHP information. This means PHP scripts can run properly with Apache web server.

If RHEL 8/CentOS is running on a remote server, then enter server-ip-address/info.php in browser address bar. Replace sever-ip-address with your actual IP address.


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.

Wrapping Up

You have successfully installed Apache, MariaDB and PHP7.2 on Red Hat 8 or CentOS 8. For your server’s security, you should delete info.php file now to prevent hacker seeing it.

rm /var/www/html/info.php
Авторизуйтесь, чтобы получить возможность оставлять комментарии
Go to top