Ubuntu 18 Enable Php Mysql Nginx


Ubuntu 18 Enable Php Mysql Nginx

Introduction: What Is Ubuntu?

Ubuntu is an open source operating system based on the Debian GNU/Linux distribution. Built around the Linux kernel and released in October 2004, Ubuntu is designed to be user friendly, easy to use and freely available. It is free to download from the Ubuntu website and access to its underlying source code is available. The operating system is maintained and supported by Canonical Ltd., a company founded by South African entrepreneur Mark Shuttleworth in 2004.

Ubuntu offers hundreds of software packages available for installation, ranging from messaging software, software development environments, and web browsers to compilers, educational software and even games. Its features include a graphical user interface, multi-user support, and a plethora of different tools to make software development and system maintenance easier for both the experienced users and novices. This makes Ubuntu a great choice for users who are looking for an easy to use, stable, and cost-effective operating system.

Installing MySQL, PHP & Nginx on Ubuntu 18

In order to enable MySQL, PHP and Nginx on Ubuntu, you must first install it. Fortunately, Ubuntu has an easy way to do this – the apt-get command. With apt-get, you can search and install packages from the official repositories. To install MySQL, PHP and Nginx on your Ubuntu machine, first open the Terminal app and then type the following command:

sudo apt-get install mysql php nginx

Apt-get will now list the packages available for installation. Enter “y” when prompted to confirm the installation and wait for the installation to complete.

Enabling MySQL & PHP with Nginx on Ubuntu

Now that you have installed MySQL, PHP and Nginx, you must enable it so that it can serve your webpages. To do this, open the Nginx configuration file and add the following lines of code in it:

location / {
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
}

location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

These lines will tell Nginx to serve any requests for “.php” files as well as the main index page with PHP. Next, you must enable the MySQL server. To do this, you must open the MySQL configuration file and add the following line in it:

sql_mode =STRICT_ALL_TABLES

Once you have added the line, save the file and restart the MySQL server. This will enable MySQL and PHP with Nginx on your Ubuntu machine.

Testing Your Nginx Webserver with PHP & MySQL Support

Now that you have enabled MySQL, PHP and Nginx to serve PHP files and web pages, it’s time to test it out. Create a simple PHP page with the following code:


PHP/MySQL test page


echo "This is a test page";
?>

Save the file as index.php in the Nginx web root directory – it’s usually called /var/www/html on Ubuntu – and then open your web browser and go to the following address:

http://localhost/index.php

If everything is working correctly, you should see the message “This is a test page” on the page. If you don’t, check that MySQL and PHP are both installed correctly and that your Nginx config file is correct.

Connecting PHP with MySQL Database

Once you have confirmed that PHP is working correctly with Nginx, you can now connect your PHP scripts with your MySQL database. To do this, you must open the php.ini file and uncomment the following line of code in it:

extension=mysql.so

This will enable the MySQL extension which is necessary for connecting PHP scripts to MySQL databases. Once you have done this, you can use the following PHP code to connect to the database:

$host = 'localhost';
$username = 'username';
$password = 'PASSWORD';
$dbname = 'DATABASE_NAME';

$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

Once the connection is established, you can use the various MySQL functions in your PHP scripts to query the database and read or write data to it.

Conclusion

Setting up PHP, MySQL and Nginx on Ubuntu 18 can be an easy and straightforward process with the help of the apt-get command. By following the steps outlined above, you can quickly enable the necessary software and begin developing web applications for your Ubuntu machine.

FAQs

What Is Ubuntu?

Ubuntu is an open source operating system based on the Debian GNU/Linux distribution and built around the Linux kernel. It is designed to be user friendly, easy to use and provide access to hundreds of software packages. It is free to download from the Ubuntu website and access the underlying source code.

How Do I Enable MySQL & PHP with Nginx on Ubuntu 18?

In order to enable MySQL, PHP and Nginx on Ubuntu 18, you must first install them using the apt-get command. Once the package is installed, open the Nginx configuration file and add the necessary lines of code to enable MySQL and PHP with Nginx. Once done, you can test it using a simple PHP script.

How Do I Connect PHP with MySQL Database?

In order to connect PHP with a MySQL database, you must first open the php.ini file and uncomment the MySQL extension. Once you have done this, you can use the following PHP code to connect to the database:

$host = 'localhost';
$username = 'username';
$password = 'PASSWORD';
$dbname = 'DATABASE_NAME';

$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

Once the connection is established, you can use the various MySQL functions in your PHP scripts to query the database.

Thank you for reading this article. For further reading, have a look at our other articles about MySQL, PHP and Nginx.

Leave a Reply

Your email address will not be published. Required fields are marked *