One place for hosting & domains

      Archives hassan latif

      How To Install the Ampache Music Streaming Server on Ubuntu 18.04


      The author selected Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      Ampache is an open-source music streaming server that allows you to host and manage your digital music collection on your own server. Ampache can stream your music to your computer, smartphone, tablet, or smart TV. This means that you don’t have to maintain multiple copies of your music on the device you want to use to listen to it. With Ampache you can manage your collection on your server using Ampache’s web interface and listen to it anywhere.

      In this tutorial, you will install and configure the Apache webserver and PHP that will serve your Ampache instance. You will then create a MySQL database that Ampache will use to store all of its operational information. Finally you will upload your music collection so you can start streaming your music.

      Prerequisites

      Before you begin this guide you’ll need the following:

      Step 1 — Installing Ampache

      In this step, you will download the Ampache ZIP archive to your server, unpack it to its installation home directory, and make some necessary file system changes.

      First, log in to your server as the non-root user. Then create Ampache’s home directory with the following command:

      • sudo mkdir /var/www/ampache

      Next, install the zip utility that you will need to unpack the Ampache archive:

      Next, download the ZIP archive of the latest release. You can find the link to the latest release on Ampache’s GitHub page. In these examples, version 4.1.1 is used:

      • wget https://github.com/ampache/ampache/releases/download/4.1.1/ampache-4.1.1_all.zip

      Next, unpack the ZIP archive into the /var/www/ampache/ directory using the -d option:

      • sudo unzip ampache-4.1.1_all.zip -d /var/www/ampache/

      Next, set the user and group identity of Ampache’s files so that Apache is able to read, write, and execute the Ampache instance files:

      • sudo chown --recursive www-data:www-data /var/www/ampache/

      The --recursive option makes chown change the ownership and group identity of all the files and subdirectory under /var/www/ampache/ to Apache’s user and group www-data.

      Next, rename the .htaccess files that are included in the ZIP archive. .htaccess files contain security and other operation information for Apache but they will only work with the file extension name .htaccess. Rename the supplied .htaccess.dist files to .htaccess with the following commands:

      • sudo mv /var/www/ampache/rest/.htaccess.dist /var/www/ampache/rest/.htaccess
      • sudo mv /var/www/ampache/play/.htaccess.dist /var/www/ampache/play/.htaccess
      • sudo mv /var/www/ampache/channel/.htaccess.dist /var/www/ampache/channel/.htaccess

      Now you’ll create the directory that will contain your music files. For security reasons, this directory is best created outside of Ampache’s installation directory. That way, it will not be readable or changeable by malicious web requests as it resides outside of Ampache’s DocumentRoot.

      Create the directory, /data/Music, with the following command:

      • sudo mkdir -p /data/Music

      Then change its owner and group identity so that Apache can read and write to it:

      • sudo chown www-data:www-data /data/Music

      To finish setting up, you’ll install FFmpeg, which is a utility that converts audio and video from one format to another. For example, you could use it to convert an MP3 music file to an OPUS music file. Ampache uses FFmpeg to convert audio on the fly from the format in which it was uploaded to a format that the listening device can play. This is a process known as transcoding.

      This is useful because not all devices are able to play all formats of music. Ampache can detect what formats are supported on the playing device and automatically supply your music in the supported format.

      Install FFmpeg with the following command:

      You’ve now unpacked and prepared your Ampache instance for the web installer and installed the FFmpeg utility. Next you’ll configure Apache and PHP to serve your Ampache instance.

      Step 2 — Configuring Apache and PHP

      In this section, you will configure Apache by creating a new VirtualHost file, which will provide the configuration that Apache needs to serve your Ampache server domain. You will also install some additional PHP modules that extend the abilities of PHP to cover the requirements of Ampache

      First, install some additional PHP modules that were not included with the default PHP installation:

      • sudo apt install php-mysql php-curl php-json php-gd php7.2-xml

      These modules provide the following additional functionality:

      • php-mysql — Enables PHP to communicate with a MySQL database.
      • php-curl — Enables PHP to use the curl utility to download files, such as album cover art, from remote servers.
      • php-json — Enables PHP to read and manipulate JSON formatted text files.
      • php-gd — Enables PHP to manipulate and create image files.
      • php7.2-xml — Enables PHP to read and manipulate XML formatted text files.

      Next, enable a couple of Apache modules using the a2enmod utility:

      • sudo a2enmod rewrite expires

      These Apache modules allow Apache to do the following:

      • rewrite — Modify or rewrite URLs following rules supplied by Ampache.
      • expires — Set the cache expiry times for objects such as images so that they are more efficiently stored by browsers.

      Now, you will create the VirtualHost file that tells Apache how and where to load the Ampache instance.

      Note: If you created a test VirtualHost file that uses your Ampache domain name when you followed the “How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 18.04” guide you must disable it. Use the a2dissite utility to disable it:

      • sudo a2dissite test_virtalhost_file

      Now, create and open the VirtualHost file with your text editor at /etc/apache2/sites-available/ampache.conf:

      • sudo nano /etc/apache2/sites-available/ampache.conf

      Add the following VirtualHost template into your file:

      /etc/apache2/sites-available/ampache.conf

      <VirtualHost *:80>
      
          ServerName your_domain
          DocumentRoot /var/www/ampache
      
          <Directory /var/www/ampache/>
              AllowOverride All
              Require all granted
          </Directory>
      
          RewriteEngine on
          CustomLog /var/log/apache2/ampache.access.log common
          ErrorLog  /var/log/apache2/ampache.error.log
      
      </VirtualHost>
      

      You must change your_domain to the domain name that you redirected to your server. When you have finished editing this file save and exit the editor.

      The directives in this VirtualHost file are:

      • ServerName — The domain name that Apache will use to serve your Ampache instance.
      • DocumentRoot — The file system location on your server where the Ampache instance is located. This is the same place that you unpacked the ZIP archive to in Step 1.
      • Directory — This section passes configuration to Apache that applies to the files and directories contained in the path.
      • RewriteEngine — Enables the rewrite Apache module.
      • CustomLog — Creates a log file that Apache will use to record all access logs for your Ampache server.
      • ErrorLog — Creates a log file that Apache will use to record all error logs generated by your Ampache server.

      Next, check that the VirtualHost file you created does not have any errors with the apachectl utility:

      • sudo apachectl configtest

      If your configuration does not contain any errors, you will see the following output after the command:

      Output

      Syntax OK

      If your configuration does contain errors, the output will print the file name and line number where the error was encountered.

      Note: If you see the error:

      AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
      

      Then you will need to edit Apache’s main configuration file at /etc/apache2/apache2.conf and add the following line:

      ServerName your_domain
      

      Next, enable the new VirtualHost configuration using the a2ensite utility:

      The final configuration is optional, but recommended. The default setting for file uploads in PHP is that no file greater than 2MB can be uploaded. Music files tend to be larger than this so increasing this size will allow you to use the Ampache interface to upload larger files in your music collection.

      Open /etc/php/7.2/apache2/php.ini with the following command:

      • sudo nano /etc/php/7.2/apache2/php.ini

      And change the following lines:

      /etc/php/7.2/apache2/php.ini

      . . .
      upload_max_filesize = 2M
      . . .
      post_max_size = 8M
      . . .
      

      To:

      /etc/php/7.2/apache2/php.ini

      . . .
      upload_max_filesize = 100M
      . . .
      post_max_size = 110M
      . . .
      

      You will now be able to upload music files up to 100MB. Use a larger value if you intend to upload files larger than this size. Save and exit the file.

      Finally, reload your updated Apache configuration:

      • sudo systemctl reload apache2.service

      You’ve now configured Apache to serve Ampache over HTTP. Next you’ll obtain a TLS certificate and configure Apache to use it so you can securely access Ampache over HTTPS.

      Step 3 — Enabling HTTPS

      In this step, you will get a free Let’s Encrypt TLS certificate using the Certbot utility, which enables HTTPS browsing. Certbot will create the certificate, automatically generate the required Apache configuration, and manage the automatic renewal of the certificate.

      This is important because every time you log in to Ampache you will send your username and password across the internet. If you are not using HTTPS then your password will be sent in plain text that can be read as it travels across the internet.

      Ubuntu’s LTS releases tend not to have the latest packages and this is true of the Certbot program. The Certbot developers maintain a dedicated Ubuntu repository, known as a PPA, so that Ubuntu users can keep an up-to-date copy of the Certbot.

      Install the Certbot repository with the following command:

      • sudo add-apt-repository ppa:certbot/certbot

      Now, install the certbot utility:

      • sudo apt install python-certbot-apache

      Next, use certbot to get the TLS certificate:

      • sudo certbot --apache -d your_domain

      The --apache option uses the Apache plugin that enables Certbot to read and configure Apache automatically. -d your_domain specifies the domain name for which you want Certbot to create the certificate.

      When you run the certbot command you will be asked a series of questions. You’ll be prompted to enter an email address and agree to the terms of service.

      If certbot successfully confirms that you control your domain, it will ask you to configure your HTTPS settings:

      Output

      Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. ------------------------------------------------------------------------------- 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. ------------------------------------------------------------------------------- Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
      • No redirect: Apache will serve Ampache via HTTP and HTTPS.
      • Redirect: Apache will redirect any HTTP connections to HTTPS automatically. This means that your Ampache server will only be available via HTTPS. This option is the more secure and will not affect how your Ampache instance behaves. This is the recommended choice.

      Finally, test that the auto-renewal of the certificate will take place successfully by running the following command:

      • sudo certbot renew --dry-run

      The --dry-run option means that certbot will test a renewal attempt without making any permanent changes to your server. If the test was successful the output will include the following line:

      Output

      Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/your_domain/fullchain.pem (success)

      Apache and PHP are now ready to serve your Ampache instance. In the next step, you will create and configure Ampache’s database.

      Step 4 — Creating a MySQL Database

      Ampache uses a MySQL database to store information such as playlists, user preferences, and so on. In this step, you will create a database and MySQL user that Ampache will use to access it.

      You will need to choose three pieces of information to complete the following instructions to create Ampache’s database:

      1. ampache_database: The name of the Ampache database.
      2. database_user: The MySQL user that Ampache will use to access the database. This is not a system user and can only access the database.
      3. database_password: The database user’s password. Be sure to choose a secure password.

      Make a note of these details as you will need them later.

      First, open the interactive MySQL shell with the mysql command:

      • mysql --user=root --password

      --user=root opens the MySQL shell as the MySQL root user and --password prompts for the root user’s password.

      The following command will create an empty database:

      • CREATE DATABASE ampache_database;

      Next, create the MySQL user:

      • CREATE USER 'database_user'@'localhost' IDENTIFIED BY 'database_password';

      Now, give the new user full access to the database:

      • GRANT ALL PRIVILEGES ON ampache_database.* TO 'database_user'@'localhost';

      Finally, check that the new database exists by running the following command:

      You will see the following output:

      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | ampache_database  |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+
      5 rows in set (0.00 sec)
      

      Exit the MySQL shell by entering exit;.

      Finally, test the database, username, and password by trying to log in to the MySQL shell with the database_user.

      Enter the following command to log in to the MySQL shell as the new user:

      • mysql --user=database_user --password ampache_database

      You’ve now created the database that Ampache will use. You’ve finished your server configuration and are ready to complete the installation with the web installer.

      Step 5 — Using the Web Installer

      In this step, you will use Ampache’s web installer to finish the installation by giving Ampache the information it will need to run, such as a web interface admin user, the database details, and other settings.

      Start the web installation by entering https://your_domain into your browser.

      Choose Installation Language

      Select Ampache’s interface language and click the Start Configuration button to continue.

      Requirements

      This page is where Ampache checks that the server meets its requirements. Each line on this page represents a test that the installer performs to ensure that, for example, all the required PHP modules are present and working. You’ll see every test has a green checkmark to indicate that your server is ready for Ampache.

      Click the Continue button to move on to the next page.

      Insert Ampache Database

      This page creates Ampache’s database if it does not exist and formats it. Fill in the fields as follows:

      • Desired Database Name: ampache_database
      • MySQL Hostname: localhost
      • MySQL Port (optional): <EMPTY>
      • MySQL Administrative Username: database_user
      • MySQL Administrative Password: database_password
      • Create Database: <UNCHECKED>
      • Create Tables (ampache.sql): <CHECKED>
      • Create Database User: <UNCHECKED>

      Image showing the completed form

      Click the Insert Database button to continue.

      Generate Configuration File

      This page creates the configuration file that Ampache will use to run. Fill in the fields as follows:

      • Web Path: <EMPTY>

      • Database Name: ampache_database

      • MySQL Hostname: localhost

      • MySQL Port (optional): <EMPTY>

      • MySQL Username: database_user

      • MySQL Password: database_password

      Image showing the completed form

      • Installation Type
        Leave this at the default setting.

      • Allow Transcoding
        Select ffmpeg from the drop-down list.

      • Players
        Leave these at the default settings.

      Click the Create Config button to continue.

      Create Admin Account

      This page creates the first web interface user. This user is created with full administrative, privileges and is the one that you will use to log in and configure Ampache for the first time.

      Choose a username and a secure password and enter it into the Password and Confirm Password fields.

      Click the Create Account button to continue.

      Ampache Update

      This page will perform any administrative changes to Ampache’s database that need making. These changes are made during version upgrades but as this is a new installation the installer will not make any changes.

      Click the Update Now! button to continue.

      Ampache Update

      This page prints and explains any updates that the installer made in the previous step. You should not see any updates listed.

      Click the [Return to main page] link to continue to the login page. Enter your username and password you set to log in to your Ampache server.

      Ampache is not quite fully set up and ready for use. You’ll now complete the setup by adding your music so you can start using your new Ampache server.

      Step 6 — Adding Your Music to Ampache

      A music server is no use without some music to play. In this step, you will configure a music catalog and upload some music. “Catalog” is the name that Ampache gives to a collection of music. Ampache is able to read music from many sources both on and off the server but in this tutorial, you will upload and store your music on the server in a local catalog, as Ampache refers to it.

      First, click the add a Catalog link in the following line on the first page that you see when you log in to Ampache:

      No Catalog configured yet. To start streaming your media, you now need to add a Catalog.

      This will take you to the Add Catalog page. Fill in the fields as follows:

      • Catalog Name: Give this catalog a short, memorable name.
      • Catalog Type: local
      • Filename Pattern: <EMPTY>
      • Folder Pattern: Leave this with the defaults.
      • Gather Art: <CHECKED>
      • Build Playlists from Playlist Files. (m3u, m3u8, asx, pls, xspf): <UNCHECKED>
      • Path: /data/Music

      Image showing the completed add catalog form

      Click the Add Catalog button to complete this page.

      On the page that follows click the Continue button. This will take you to the Show Catalogs page that will print the details for the catalog you created. You can change the catalog settings at any time in the future.

      Now, enable Ampache’s web upload facility by clicking on the third navigation icon to open the extended settings:

      Image showing the third settings icon

      Scroll down to the Server Config section and click the System link to open the system settings page.

      Find the Allow user uploads line and select Enable from the drop-down menu in the Value column.

      You can also choose what level of user is able to upload music files. The default level is Catalog Manager, this allows the Catalog Manager and all users with greater privileges to upload music. In this case, that is the administrator.

      You also need to set the catalog to which the music will get added. Set this using the Destination catalog line. Select the catalog you created in the Value drop-down.

      Image showing the allow upload and destination catalog lines

      Click the Update Preferences button at the bottom of the page to complete the configuration. You are now ready to upload some music.

      First, click the first settings icon:

      Icon showing the first settings icon

      Then click the Upload link in the Music section.

      Image showing upload link

      On the Upload page, click the Browse button and locate your music files on your computer and upload them.

      If you leave the Artist and Album labels blank then Ampache will read the ID3 tags of the music files to discover the artist and album automatically. After you have uploaded some music you will now be able to find it by clicking on the Songs, Albums, or Artists links in the Music section in the navigation panel on the left.

      Your Ampache music server is now ready to start streaming your music.

      Conclusion

      In this article, you installed and configured an Ampache music streaming server and uploaded some of your music. You can now listen to your music wherever you go on any of your devices. Ampache’s Documentation will help you use and extend your streaming server. These Android apps and this iOS app will stream your music to your phone. Ampache organizes your music on the server using the ID3 tags in the music files. The MusicMrainz program will help you manage the ID3 tags of your music files.



      Source link

      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

      How To Install MariaDB on CentOS 8


      Introduction

      MariaDB is an open-source database management system, commonly used as an alternative for the MySQL portion of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It is intended to be a drop-in replacement for MySQL.

      In this tutorial, we will explain how to install the latest version of MariaDB on a CentOS 8 server. If you’re wondering about MySQL vs. MariaDB, MariaDB is the preferred package and should work seamlessly in place of MySQL. If you specifically need MySQL, see the How to Install MySQL on CentOS 8 guide.

      Prerequisites

      To follow this tutorial, you will need a CentOS 8 server with a non-root sudo-enabled user. You can learn more about how to set up a user with these privileges in the Initial Server Setup with CentOS 8 guide.

      Step 1 — Installing MariaDB

      First, use dnf to install the MariaDB package:

      • sudo dnf install mariadb-server

      You will be asked to confirm the action. Press y then ENTER to proceed.

      Once the installation is complete, start the service with systemctl:

      • sudo systemctl start mariadb

      Then check the status of the service:

      • sudo systemctl status mariadb

      Output

      ● mariadb.service - MariaDB 10.3 database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2020-04-03 17:32:46 UTC; 52min ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 4567 (mysqld) Status: "Taking your SQL requests now..." Tasks: 30 (limit: 5059) Memory: 77.1M CGroup: /system.slice/mariadb.service └─4567 /usr/libexec/mysqld --basedir=/usr . . . Apr 03 17:32:46 centos8-mariadb systemd[1]: Started MariaDB 10.3 database server.

      If MariaDB has successfully started, the output should show active (running) and the final line should look something like:

      Output

      Apr 03 17:32:46 centos8-mariadb systemd[1]: Started MariaDB 10.3 database server..

      Next, let’s take a moment to ensure that MariaDB starts at boot, using the systemctl enable command:

      • sudo systemctl enable mariadb

      Output

      Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.

      We now have MariaDB running and configured to run at startup. Next, we’ll turn our attention to securing our installation.

      Step 2 — Securing the MariaDB Server

      MariaDB includes a security script to change some of the less secure default options for things like remote root logins and sample users. Use this command to run the security script:

      • sudo mysql_secure_installation

      The script provides a detailed explanation for every step. The first step asks for the root password, which hasn’t been set so we’ll press ENTER as it recommends. Next, we’ll be prompted to set that root password. Keep in mind that this is for the root database user, not the root user for your CentOS server itself.

      Type Y then ENTER to enter a password for the root database user, then follow the prompts.

      After updating the password, we will accept all the security suggestions that follow by pressing y and then ENTER. This will remove anonymous users, disallow remote root login, remove the test database, and reload the privilege tables.

      Now that we’ve secured the installation, we’ll verify it’s working by connecting to the database.

      Step 3 — Testing the Installation

      We can verify our installation and get information about it by connecting with the mysqladmin tool, a client that lets you run administrative commands. Use the following command to connect to MariaDB as root (-u root), prompt for a password (-p), and return the version.

      • mysqladmin -u root -p version

      You should see output similar to this:

      Output

      mysqladmin Ver 9.1 Distrib 10.3.17-MariaDB, for Linux on x86_64 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Server version 10.3.17-MariaDB Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/lib/mysql/mysql.sock Uptime: 6 min 5 sec Threads: 7 Questions: 16 Slow queries: 0 Opens: 17 Flush tables: 1 Open tables: 11 Queries per second avg: 0.043

      This indicates the installation has been successful.

      Conclusion

      In this guide you installed MariaDB to act as an SQL server. During the installation process you also secured the server. Optionally, you also created a separate password-authenticated administrative user.

      Now that you have a running and secure MariaDB server, here some examples of next steps that you can take to work with the server:



      Source link