How to install LAMP (Linux Apache, MariaDB & PHP) on CentOS 7
In this article, we are going to show you how to install LAMP (Linux Apache, MariaDB and PHP) on CentOS 7…
What is LAMP?
LAMP is actually an acronym for the Web Services Stack, consisting of Linux, Apache HTTP Server , MySQL or MariaDB DBMSand PHP, Perl or Python. All components are software with free and open sourcewhich are suitable for creating dynamic web pages.
Update the system
As always, make sure your CentOS 7 completely updated using the following commands:
screen -U -S lamp-centos7 yum update
Install MariaDB
MariaDB is the default database server in CentOS 7so go ahead and install it with the following command:
yum install mariadb mariadb-server mysql
After installation, add bind-address = 127.0.0.1
in /etc/my.cnf.d/server.cnf
,
to link MariaDB locally:
vim /etc/my.cnf.d/server.cnf [mysqld] #log-bin=mysql-bin #binlog_format=mixed bind-address = 127.0.0.1
Restart the MariaDB database server and enable it to autostart the system with:
systemctl restart mariadb systemctl status mariadb systemctl enable mariadb
Finally, you can run the script mysql_secure_installation
after installation to finish configuring MariaDB. For instance:
mysql_secure_installation Enter current password for root (enter for none): ENTER Set root password? [Y/n] Y Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Y/n] Y Reload privilege tables now? [Y/n] Y
Install Apache
Then proceed to install Apache, OpenSSL and mod_ssl using yum, так
:
yum install httpd openssl mod_ssl
Restart Apache and add it to start automatically on your system with:
systemctl restart httpd systemctl status httpd systemctl enable httpd
Test the setting by opening, for example, http: // your_server_ip_address / in your browser. You should see the default Apache page, for example:
Excellent. Now let’s configure some additional Apache parameterseg hide your version number, its banner, etc … by adding the following to /etc/httpd/conf.d/options.conf.
vim /etc/httpd/conf.d/options.conf TraceEnable off ## Disable Signature ServerSignature Off ## Disable Banner ServerTokens Prod
Testing the Apache configuration file and restarting the web server for the changes to take effect:
apachectl configtest systemctl restart httpd systemctl status httpd
Install PHP
In order to be able to run PHP based web applications, we must add PHP support for Apache web serverso let’s install some required packages with yum
:
yum install php php-mysql
In addition, you can install some other PHP extensions required for your applications. Here’s a list:
php-bcmath : модуль для PHP-приложений для использования библиотеки BCMath php-cli : Интерфейс командной строки для PHP php-common : Общие файлы для PHP php-dba : Модуль абстракции базы данных для PHP-приложений php-devel : Файлы, необходимые для построения расширений PHP php-embedded : PHP библиотека для встраивания в приложения php-enchant : расширение enchant правописания для PHP-приложений php-fpm : PHP FastCGI Process Manager php-gd : модуль для PHP-приложений для использования библиотеки графики gd php-intl : расширение для PHP интернационализации приложений php-ldap : модуль для PHP-приложений, которые используют LDAP php-mbstring : модуль для PHP-приложений, которые требуют обработки многобайтовых строк php-mysql : модуль для PHP-приложений, использующих базы данных MySQL php-mysqlnd : модуль для PHP-приложений, которые используют базы данных MySQL php-odbc : Модуль для PHP приложений, использующих базы данных ODBC php-pdo : Модуль абстракции доступа к базам данных для PHP-приложений php-pear.noarch : PHP Extension и Application Repository framework php-pecl-memcache : Расширение для работы с кэширования демона Memcached php-pgsql : Модуль базы данных PostgreSQL для PHP php-process : Модули для PHP скрипт с использованием интерфейсов системного процесса php-pspell : модуль для PHP-приложений для использования pspell интерфейсов php-recode : модуль для PHP-приложений для использования библиотеки перекодирования php-snmp : Модуль для PHP приложений, запрашивающих SNMP-управляемых устройств php-soap : Модуль для PHP-приложений, использующих протокол SOAP php-xml : модуль для PHP-приложений, использующих XML php-xmlrpc : модуль для PHP-приложений, использующих протокол XML-RPC
Ok, let’s continue with the PHP configuration. Edit the file /etc/php.ini
and set the following parameters:
vim /etc/php.ini
date.timezone = Europe/Moscow memory_limit = 64M expose_php = Off
restart Apache using systemctl
for the changes to take effect:
systemctl restart httpd systemctl status httpd
Testing the loading of the PHP module in Apache using:
httpd -M | grep php also ## php -v
You can create a test script info.php
by using the following command:
echo -e "<?phpntphpinfo();" > /var/www/html/info.php
and access it with the following command:
curl -I $(curl -s icanhazip.com)/info.php
Install APACHE VHOST
Now you have to configure Apache Virtual Host directives, to be able to host multiple domainsEven SSL using powered one one IP address. So create a file /etc/httpd/conf.d/vhosts.conf
and add the following:
cat /etc/httpd/conf.d/vhosts.conf # Загрузка моих vhosts IncludeOptional vhosts.d/*.conf
This tells Apache to load configuration files ending in .conf
who are in /etc/httpd/vhosts.d
… As you might have guessed, this is where we list our virtual hosts, so let’s configure one for my_domain.com
and another for my_domain2.ru
VHOST for my_domain.com
## cat /etc/httpd/vhosts.d/my_domain.com.conf <VirtualHost YOUR_SERVER_IP:80> ServerAdmin [email protected]_domain.com DocumentRoot "/var/www/html/my_domain.com" ServerName my_domain.com ServerAlias www.my_domain.com ErrorLog "/var/log/httpd/my_domain.com-error_log" CustomLog "/var/log/httpd/my_domain.com-access_log" combined <Directory "/var/www/html/my_domain.com/"> DirectoryIndex index.html index.php Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
VHOST for my_domain.net
## cat /etc/httpd/vhosts.d/my_domain.net.conf <VirtualHost YOUR_SERVER_IP:80> ServerAdmin [email protected]_domain.net DocumentRoot "/var/www/html/my_domain.net" ServerName my_domain.net ServerAlias www.my_domain.net ErrorLog "/var/log/httpd/my_domain.net-error_log" CustomLog "/var/log/httpd/my_domain.net-access_log" combined <Directory "/var/www/html/my_domain.net/"> DirectoryIndex index.html index.php Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
Finally, restart Apache for the changes to take effect:
apachectl configtest systemctl restart httpd systemctl status httpd