One place for hosting & domains

      Linux

      How To Install Linux, Apache, MariaDB, PHP (LAMP) Stack on CentOS 8


      Introduction

      A “LAMP” stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps written in PHP. This term is an acronym which represents the Linux operating system, with the Apache web server. The backend data is stored in a MariaDB database and the dynamic processing is handled by PHP.

      The database layer in a LAMP stack is typically a MySQL database server, but prior to the release of CentOS 8, MySQL wasn’t available from the default CentOS repositories. Because of this, MariaDB, a community fork of MySQL, became a widely accepted alternative to MySQL as the default database system for LAMP stacks on CentOS machines. MariaDB works as a drop-in replacement for the original MySQL server, which in practice means you can switch to MariaDB without having to make any configuration or code changes in your application.

      In this guide, you’ll install a LAMP stack on a CentOS 8 server, using MariaDB as the database management system.

      Prerequisites

      To follow this guide, you’ll need access to a CentOS 8 server as a non-root user with sudo privileges, and an active firewall installed on your server. To set this up, you can follow our Initial Server Setup Guide for CentOS 8.

      Step 1 — Installing the Apache Web Server

      In order to display web pages to our site visitors, we are going to employ Apache, a popular open source web server that can be configured to serve PHP pages. We’ll use the dnf package manager, which is the new default package manager on CentOS 8, to install this software.

      Install the httpd package with:

      When prompted, enter y to confirm that you want to install Apache.

      After the installation is finished, run the following command to enable and start the server:

      • sudo systemctl start httpd

      In case you have enabled the firewalld firewall as per our initial server setup guide, you will need to allow connections to Apache. The following command will permanently enable HTTP connections, which run on port 80 by default:

      • sudo firewall-cmd --permanent --add-service=http

      To verify that the change was applied, you can run:

      • sudo firewall-cmd --permanent --list-all

      You’ll see output like this:

      Output

      public target: default icmp-block-inversion: no interfaces: sources: services: cockpit dhcpv6-client http ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:

      You’ll need to reload the firewall configuration so the changes take effect:

      • sudo firewall-cmd --reload

      With the new firewall rule added, you can test if the server is up and running by accessing your server’s public IP address or domain name from your web browser.

      Note: In case you are using DigitalOcean as DNS hosting provider, you can check our product docs for detailed instructions on how to set up a new domain name and point it to your server.

      If you do not have a domain name pointed at your server and you do not know your server’s public IP address, you can find it by running the following command:

      • ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'

      This will print out a few IP addresses. You can try each of them in turn in your web browser.

      As an alternative, you can check which IP address is accessible, as viewed from other locations on the internet:

      Type the address that you receive in your web browser and it will take you to Apache’s default landing page:

      Default Apache Page CentOS 8

      If you see this page, then your web server is now correctly installed.

      Step 2 — Installing MariaDB

      Now that you have a web server up and running, you need to install a database system to be able to store and manage data for your site. We’ll install MariaDB, a community-developed fork of the original MySQL server by Oracle.

      To install this software, run:

      • sudo dnf install mariadb-server

      When the installation is finished, you can enable and start the MariaDB server with:

      • sudo systemctl start mariadb

      To improve the security of your database server, it’s recommended that you run a security script that comes pre-installed with MariaDB. This script will remove some insecure default settings and lock down access to your database system. Start the interactive script by running:

      • sudo mysql_secure_installation

      This script will take you through a series of prompts where you can make some changes to your MariaDB setup. The first prompt will ask you to enter the current database root password. This is not to be confused with the system root user. The database root user is an administrative user with full privileges over the database system. Because you just installed MariaDB and haven’t made any configuration changes yet, this password will be blank, so just press ENTER at the prompt.

      The next prompt asks you whether you’d like to set up a database root password. Because MariaDB uses a special authentication method for the root user that is typically safer than using a password, you don’t need to set this now. Type N and then press ENTER.

      From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove anonymous users and the test database, disable remote root login, and load these new rules so that the server immediately respects the changes you have made.

      When you’re finished, log in to the MariaDB console by typing:

      This will connect to the MariaDB server as the administrative database user root, which is inferred by the use of sudo when running this command. You should see output like this:

      Output

      Welcome to the MariaDB monitor. Commands end with ; or g. Your MariaDB connection id is 9 Server version: 10.3.17-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MariaDB [(none)]>

      Notice that you didn’t need to provide a password to connect as the root user. That works because the default authentication method for the administrative MariaDB user is unix_socket instead of password. Even though this might look like a security concern at first, it makes the database server more secure because the only users allowed to log in as the root MariaDB user are the system users with sudo privileges connecting from the console or through an application running with the same privileges. In practical terms, that means you won’t be able to use the administrative database root user to connect from your PHP application.

      For increased security, it’s best to have dedicated user accounts with less expansive privileges set up for every database, especially if you plan on having multiple databases hosted on your server. To demonstrate such a setup, we’ll create a database named example_database and a user named example_user, but you can replace these names with different values.

      To create a new database, run the following command from your MariaDB console:

      • CREATE DATABASE example_database;

      Now you can create a new user and grant them full privileges on the custom database you’ve just created. The following command defines this user’s password as password, but you should replace this value with a secure password of your own choosing:

      • GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

      This will give the example_user user full privileges over the example_database database, while preventing this user from creating or modifying other databases on your server.

      Flush the privileges to ensure that they are saved and available in the current session:

      Following this, exit the MariaDB shell:

      You can test if the new user has the proper permissions by logging in to the MariaDB console again, this time using the custom user credentials:

      Note the -p flag in this command, which will prompt you for the password you chose when creating the example_user user. After logging in to the MariaDB console, confirm that you have access to the example_database database:

      This will give you the following output:

      Output

      +--------------------+ | Database | +--------------------+ | example_database | | information_schema | +--------------------+ 2 rows in set (0.000 sec)

      To exit the MariaDB shell, type:

      At this point, your database system is set up and you can move on to installing PHP, the final component of the LAMP stack.

      Step 3 — Installing PHP

      You have Apache installed to serve your content and MariaDB installed to store and manage your data. PHP is the component of our setup that will process code to display dynamic content to the final user. In addition to the php package, you’ll need php-mysqlnd, a PHP module that allows PHP to communicate with MySQL-based databases. Core PHP packages will automatically be installed as dependencies.

      To install the php and php-mysqlnd packages using the dnf package manager, run:

      sudo dnf install php php-mysqlnd
      

      After the installation is finished, you’ll need to restart the Apache web server in order to enable the PHP module:

      sudo systemctl restart httpd
      

      Your web server is now fully set up. In the next step, we’ll create a PHP testing script to make sure everything works as expected.

      Step 4 — Testing PHP with Apache

      The default Apache installation on CentOS 8 will create a document root located at /var/www/html. You don’t need to make any changes to Apache’s default settings in order for PHP to work correctly within your web server.

      The only adjustment we’ll make is to change the default permission settings on your Apache document root folder. This way, you’ll be able to create and modify files in that directory with your regular system user, without the need to prefix each command with sudo.

      The following command will change the ownership of the default Apache document root to a user and group called sammy, so be sure to replace the highlighted username and group in this command to reflect your system’s username and group.

      • sudo chown -R sammy.sammy /var/www/html/

      We’ll now create a test PHP page to make sure the web server works as expected.

      The default text editor that comes with CentOS 8 is vi. vi is an extremely powerful text editor, but it can be somewhat obtuse for users who lack experience with it. You might want to install a more user-friendly editor such as nano to facilitate editing files on your CentOS 8 server:

      Type y when prompted to confirm the installation.

      Now, create a new PHP file called info.php at the /var/www/html directory:

      • nano /var/www/html/info.php

      The following PHP code will display information about the current PHP environment running on the server:

      /var/www/html/info.php

      <?php
      
      phpinfo();
      

      When you are finished, save and close the file. If you are using nano, you can do that by typing CTRL+X, then Y and ENTER to confirm.

      Now we can test whether our web server can correctly display content generated by a PHP script. Go to your browser and access your server hostname or IP address, followed by /info.php:

      http://server_host_or_IP/info.php
      

      You’ll see a page similar to this:

      CentOS 8 default PHP info Apache

      After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment and your CentOS server. You can use rm to remove that file:

      • rm /var/www/html/info.php

      You can always regenerate this file if you need it later. Next, we’ll test the database connection from the PHP side.

      Step 5 — Testing Database Connection from PHP (Optional)

      If you want to test if PHP is able to connect to MariaDB and execute database queries, you can create a test table with dummy data and query for its contents from a PHP script.

      First, connect to the MariaDB console with the database user you created in Step 2 of this guide:

      Create a table named todo_list. From the MariaDB console, run the following statement:

      CREATE TABLE example_database.todo_list (
          item_id INT AUTO_INCREMENT,
          content VARCHAR(255),
          PRIMARY KEY(item_id)
      );
      

      Now, insert a few rows of content in the test table. You might want to repeat the next command a few times, using different values:

      • INSERT INTO example_database.todo_list (content) VALUES ("My first important item");

      To confirm that the data was successfully saved to your table, run:

      • SELECT * FROM example_database.todo_list;

      You will see the following output:

      Output

      +---------+--------------------------+ | item_id | content | +---------+--------------------------+ | 1 | My first important item | | 2 | My second important item | | 3 | My third important item | | 4 | and this one more thing | +---------+--------------------------+ 4 rows in set (0.000 sec)

      After confirming that you have valid data in your test table, you can exit the MariaDB console:

      Now you can create the PHP script that will connect to MariaDB and query for your content. Create a new PHP file in your custom web root directory using your preferred editor. We’ll use nano for that:

      • nano /var/www/html/todo_list.php

      Add the following content to your PHP script:

      /usr/share/nginx/html/todo_list.php

      <?php
      $user = "example_user";
      $password = "password";
      $database = "example_database";
      $table = "todo_list";
      
      try {
        $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
        echo "<h2>TODO</h2><ol>"; 
        foreach($db->query("SELECT content FROM $table") as $row) {
          echo "<li>" . $row['content'] . "</li>";
        }
        echo "</ol>";
      } catch (PDOException $e) {
          print "Error!: " . $e->getMessage() . "<br/>";
          die();
      }
      

      Save and close the file when you’re done editing.

      You can now access this page in your web browser by visiting your server’s host name or public IP address, followed by /todo_list.php:

      http://server_host_or_IP/todo_list.php
      

      You should see a page like this, showing the content you’ve inserted in your test table:

      Example PHP todo list

      That means your PHP environment is ready to connect and interact with your MariaDB server.

      Conclusion

      In this guide, you’ve built a flexible foundation for serving PHP websites and applications to your visitors, using Apache as web server. You’ve set up Apache to handle PHP requests, and you’ve also set up a MariaDB database to store your website’s data.



      Source link

      Cómo instalar la pila Linux, Nginx, MySQL, PHP (LEMP) en CentOS 8 [Guía de inicio rápido]


      Introducción

      A través de este tutorial, instalará una pila LEMP en un servidor de CentOS 8. Aunque MySQL está disponible en los repositorios predeterminados en CentOS 8, en esta guía se mostrará el proceso de configuración de una pila LEMP con MariaDB como sistema de administración de bases de datos.

      Para ver una versión más detallada de este tutorial, con más explicaciones de cada paso, consulte Cómo instalar la pila Linux, Nginx, MySQL, PHP (LEMP) en CentOS 8.

      Requisitos previos

      Para completar esta guía, necesitará acceso a un servidor de CentOS 8 como usuario sudo.

      Paso 1: Instalar Nginx

      Instale el paquete nginx con:

      Cuando finalice la instalación, ejecute el siguiente comando para habilitar e iniciar el servidor:

      • sudo systemctl start nginx

      Si firewalld está activo, deberá ejecutar el siguiente comando para permitir el acceso externo en el puerto 80 (HTTP):

      • sudo firewall-cmd --permanent --add-service=http

      Vuelva a cargar la configuración del firewall para que los cambios surtan efecto:

      • sudo firewall-cmd --reload

      Una vez añadida la nueva regla de firewall, puede verificar si su servidor está activo accediendo a la dirección IP pública o al nombre del dominio de este desde su navegador web. Verá una página como la siguiente:

      Página predeterminada de Nginx, CentOS 8

      Paso 2: Instalar MariaDB

      Ahora instalaremos MariaDB, una ramificación del servidor MySQL original de Oracle desarrollada por la comunidad. Para instalar este software, ejecute lo siguiente:

      • sudo dnf install mariadb-server

      Cuando termine la instalación, habilite e inicie el servidor MariaDB con lo siguiente:

      • sudo systemctl start mariadb

      Par mejorar la seguridad del servidor de su base de datos, se recomienda que ejecute una secuencia de comandos de seguridad que se incluye con MariaDB. Inicie la secuencia de comandos interactiva con lo siguiente:

      • sudo mysql_secure_installation

      En la primera solicitud se pedirá que introduzca la contraseña root de la base de datos actual. Debido a que acaba de instalar MariaDB y aún no realizó aún cambios en la configuración, el espacio de esta contraseña estará en blanco. Por ello, pulse ENTER en la solicitud.

      En la siguiente solicitud se pregunta si desea configurar una contraseña root de la base de datos. Debido a que MariaDB usa un método de autenticación especial para el usuario root que normalmente proporciona más seguridad que una contraseña, no es necesario que la configure ahora. Escriba N y pulse ENTER.

      Desde allí, puede pulsar Y y luego ENTER para aceptar los valores predeterminados para todas las preguntas siguientes.

      Cuando termine, inicie sesión en la consola de MariaDB escribiendo lo siguiente:

      Esto permitirá establecer conexión con el servidor de MariaDB como usuario root de la base de datos administrativa, lo que se infiere del uso de sudo cuando se ejecuta este comando. Debería ver el siguiente resultado:

      Output

      Welcome to the MariaDB monitor. Commands end with ; or g. Your MariaDB connection id is 9 Server version: 10.3.17-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MariaDB [(none)]>

      Para crear una nueva base de datos, ejecute el siguiente comando desde su consola de MariaDB:

      • CREATE DATABASE example_database;

      Ahora puede crear un nuevo usuario y concederle privilegios completos sobre la base de datos personalizada que acaba de crear:

      • GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

      Vacíe los privilegios para garantizar que se guarden y estén disponibles en la sesión actual:

      Después de esto, cierre el shell de MariaDB:

      Paso 3: Instalar PHP-FPM

      Para instalar los paquetes php-fpm y php-mysql, ejecute lo siguiente:

      • sudo dnf install php-fpm php-mysqlnd

      Cuando finalice la instalación, deberá editar el archivo /etc/php-fpm.d/www.conf para regular algunos ajustes. Instalaremos nano para facilitar la edición de estos archivos:

      Ahora, abra el archivo de configuración /etc/php-fpm.d/www.conf usando nano o el editor que prefiera:

      • sudo nano /etc/php-fpm.d/www.conf

      Busque las directivas user y group. Si usa nano, puede pulsar CTRL+W para buscar estos términos dentro del archivo abierto. Asegúrese de cambiar ambos valores de apache a nginx:

      /etc/php-fpm.d/www.conf

      …
      ; RPM: apache user chosen to provide access to the same directories as httpd
      user = nginx
      ; RPM: Keep a group allowed to write in log dir.
      group = nginx

      Guarde y cierre el archivo cuando finalice la edición.

      Para habilitar e iniciar el servicio php-fpm, ejecute lo siguiente:

      • sudo systemctl start php-fpm

      Finalmente, reinicie el servidor web Nginx de modo que cargue los archivos de configuración creados por la instalación de php-fmp:

      • sudo systemctl restart nginx

      Paso 4: Probar PHP con Nginx

      En CentOS 8, la instalación predeterminada de php-fpm crea automáticamente archivos de configuración que ahora permitirán que su servidor web Nginx gestione los archivos .php en la raíz de documento predeterminada ubicada en /usr/share/nginx/html. No necesitará realizar cambios en la configuración de Nginx para que PHP funcione correctamente en su servidor web.

      Solo tendremos que modificar el propietario y grupo predeterminados en la raíz del documento de Nginx, para que pueda crear y modificar archivos en esa ubicación empleando su usuario de sistema no root regular.

      • sudo chown -R sammy.sammy /usr/share/nginx/html/

      Cree un nuevo archivo PHP llamado info.php en el directorio /usr/share/nginx/html:

      • nano /usr/share/nginx/html/info.php

      El siguiente código PHP mostrará información sobre el entorno PHP actual que se ejecuta en el servidor:

      /usr/share/nginx/html/info.php

      <?php
      
      phpinfo();
      

      Copie este contenido a su archivo info.php y no olvide guardarlo cuando termine.

      Ahora, podemos probar si nuestro servidor web puede mostrar correctamente el contenido generado por una secuencia de comandos PHP. Vaya a su navegador y acceda al nombre de host o la dirección IP de su servidor; agregue /info.php al final:

      http://server_host_or_IP/info.php
      

      Verá una página similar a la siguiente:

      Información PHP predeterminada de CentOS 8

      Tutoriales relacionados

      A continuación, se ofrecen los enlaces a más guías detalladas relacionadas con este tutorial:



      Source link

      Установка стека Linux, Nginx, MySQL, PHP (LEMP) в CentOS 8 [Краткое руководство]


      Введение

      В этом руководстве вы установите стек LEMP на сервер с CentOS 8. Хотя MySQL доступен в используемых по умолчанию репозиториях в CentOS 8, в этом руководстве мы рассмотрим процесс настройки стека LEMP с MariaDB в качестве системы управления базами данных.

      Более подробную версию этого обучающего руководства с подробным описанием каждого шага см. в статье Установка стека Linux, Nginx, MySQL, PHP (LEMP) в CentOS 8.

      Предварительные требования

      Для выполнения этого руководства вам потребуется доступ к серверу CentOS 8 с пользователем с правами sudo.

      Шаг 1 — Установка Nginx

      Установите пакет nginx с помощью следующей команды:

      После завершения установки запустите следующую команду, чтобы активировать и запустить сервер:

      • sudo systemctl start nginx

      Если брандмауэр firewalld активен, вам потребуется запустить следующую команду, чтобы разрешить внешний доступ к порту 80 (HTTP):

      • sudo firewall-cmd --permanent --add-service=http

      Выполните перезапуск конфигурации брандмауэра, чтобы изменения вступили в силу:

      • sudo firewall-cmd --reload

      После добавления нового правила брандмауэра вы можете проверить, запущен ли сервер, запросив доступ к публичному IP-адресу или доменному имени сервера из вашего веб-браузера. Страница будет выглядеть следующим образом:

      Страница Nginx по умолчанию в CentOS 8

      Шаг 2 — Установка MariaDB

      Теперь мы установим MariaDB, разработанную энтузиастами версию оригинального сервера MySQL от Oracle. Для установки данного ПО запустите следующую команду:

      • sudo dnf install mariadb-server

      После завершения установки активируйте и запустите сервер MariaDB с помощью следующей команды:

      • sudo systemctl start mariadb

      Чтобы повысить уровень безопасности вашего сервера базы данных, рекомендуется запустить скрипт безопасности, который устанавливается в комплекте с MariaDB. Запустите интерактивный скрипт с помощью следующей команды:

      • sudo mysql_secure_installation

      В первом диалоге вам нужно будет ввести пароль пользователя root для текущей базы данных. Поскольку вы только что установили MariaDB и еще не меняли параметры конфигурации, пароль будет пустым, так что вам достаточно нажать ENTER в этом диалоге.

      В следующем диалоге вам будет предложено задать пароль для пользователя root базы данных. Поскольку MariaDB использует специальный метод аутентификации для пользователя root, который, как правило, отличается большей безопасностью по сравнению с аутентификацией по паролю, вам не нужно настраивать его в данный момент. Введите N и нажмите ENTER.

      Далее вы можете использовать клавиши Y и ENTER, чтобы принять ответы по умолчанию для всех последующих вопросов.

      После выполнения указанных выше действий выполните вход в консоль MariaDB с помощью следующей команды:

      В результате будет установлено подключение к серверу MariaDB с помощью пользователя root базы данных с правами администратора, который логически выводится в результате использования sudo при запуске данной команды. Результат должен выглядеть следующим образом:

      Output

      Welcome to the MariaDB monitor. Commands end with ; or g. Your MariaDB connection id is 9 Server version: 10.3.17-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. MariaDB [(none)]>

      Чтобы создать новую базу данных, запустите следующую команду в консоли MariaDB:

      • CREATE DATABASE example_database;

      Теперь вы можете создать нового пользователя и предоставить ему полный набор прав для только что созданной вами базы данных:

      • GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

      Очистите привилегии, чтобы они были сохранены и доступны в текущем сеансе:

      После этого закройте оболочку MariaDB:

      Шаг 3 — Установка PHP-FPM

      Для установки пакетов php-fpm и php-mysql воспользуйтесь следующей командой:

      • sudo dnf install php-fpm php-mysqlnd

      После завершения установки вам потребуется изменить файл /etc/php-fpm.d/www.conf, чтобы задать несколько параметров. Мы установим nano для упрощения процесса редактирования этих файлов:

      Теперь откройте файл конфигурации /etc/php-fpm.d/www.conf с помощью nano или другого выбранного вами редактора:

      • sudo nano /etc/php-fpm.d/www.conf

      Найдите директивы user и group. Если вы используете nano, вы можете нажать CTRL+W для поиска этих терминов в открытом файле. Обязательно измените оба значения с apache на nginx:

      /etc/php-fpm.d/www.conf

      …
      ; RPM: apache user chosen to provide access to the same directories as httpd
      user = nginx
      ; RPM: Keep a group allowed to write in log dir.
      group = nginx

      Сохраните и закройте файл после завершения редактирования.

      Чтобы активировать и запустить службу php-fpm, запустите следующую команду:

      • sudo systemctl start php-fpm

      Перезапустите веб-сервер Nginx, чтобы он смог использовать файлы конфигурации, созданные при установке php-fpm:

      • sudo systemctl restart nginx

      Шаг 4 — Тестирование PHP с помощью Nginx

      В CentOS 8 при установке php-fpm по умолчанию автоматически создаются файлы конфигурации, которые позволят вашему веб-серверу Nginx обрабатывать файлы .php в корневой директории документов по умолчанию, расположенной в /usr/share/nginx/html. Вам не потребуется вносить какие-либо изменения в конфигурацию Nginx для обеспечения корректной работы PHP на вашем веб-сервере.

      Нам нужно будет только изменить владельца и группу по умолчанию в корневой директории документов Nginx, чтобы вы могли создавать и изменять файлы в этом месте с помощью обычного пользователя без прав root:

      • sudo chown -R sammy.sammy /usr/share/nginx/html/

      Создайте новый файл PHP с именем info.php в директории /usr/share/nginx/html:

      • nano /usr/share/nginx/html/info.php

      Следующий код PHP будет отображать информацию о текущей среде PHP, которая запущена на сервере:

      /usr/share/nginx/html/info.php

      <?php
      
      phpinfo();
      

      Скопируйте это содержимое в ваш файл info.php и не забудьте сохранить изменения после завершения редактирования.

      Теперь мы можем проверить, может ли наш веб-сервер корректно отображать содержимое, созданное скриптом PHP. Перейдите в браузер и вставьте в адресную строку имя хоста вашего сервера или его IP-адрес, добавив /info.php:

      http://server_host_or_IP/info.php
      

      Вы увидите приблизительно следующую страницу:

      Отображаемая по умолчанию страница с данными PHP в CentOS 8

      Другие обучающие руководства

      Ниже представлены ссылки на более подробные материалы, связанные с настоящим обучающим руководством:



      Source link