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.
Related Posts:
- Log Nginx Ubuntu 14.04 Log Nginx Ubuntu 14.04 Introduction Nginx is an open-source web server that is considered fast and reliable for online traffic. It has been around for many years and is one…
- Remove Nginx Full Complete On Ubuntu 18 Remove Nginx Full Complete On Ubuntu 18 Uninstallating Nginx From Ubuntu Ubuntu is one of the most popular Linux-based operating systems, and it has a wide variety of programs available…
- How To Setup Nginx On Ubuntu How To Setup Nginx On Ubuntu Introduction Nginx is a powerful web server that is very popular among Linux users. It is open-source and comes with great features such as…
- Wordpress Nginx Ubuntu 18.04 Wordpress Nginx Ubuntu 18.04 Overview WordPress is a popular web software used by millions of people around the world. It allows users to easily create and manage websites without the…
- Create Wordpress With Nginx Mariadb Php-Fpm 16.04 Create WordPress With Nginx Mariadb Php-Fpm 16.04 What Is WordPress? WordPress is a content management system (CMS) and a blogging platform. It allows you to easily create and manage websites…
- Install Phpmyadmin On Nginx Ubuntu Install Phpmyadmin On Nginx Ubuntu What Is Phpmyadmin? Phpmyadmin is a free, open source web application written in PHP specifically designed to help manage MySQL databases. It is a powerful…
- Docker Install Nginx Mysql Php Docker Install Nginx Mysql Php What is Docker? Docker is an open-source platform for automating the deployment of applications as lightweight, portable, and self-sufficient containers. It bundles applications and all…
- How To Login Phpmyadmin In Nginx Server Ubuntu How To Login Phpmyadmin In Nginx Server Ubuntu What is Nginx & phpMyAdmin Nginx is a web server software that is commonly used on Linux-based systems, though it works on…
- Install Nginx Php Oracle Ubuntu Install Nginx, Php, Oracle, Ubuntu Installing Nginx on Ubuntu Nginx is a popular web server software based on the LEMP stack that is widely used to host web applications, especially…
- Install Phpmyadmin Nginx Ubuntu 16.04 Install Phpmyadmin Nginx Ubuntu 16.04 What is PhpMyAdmin? PhpMyAdmin is an open-source software for managing MySQL databases. It can be used to add, modify, and delete databases, tables, and columns.…
- How To Install Nginx On Ubuntu 18.04 How To Install Nginx on Ubuntu 18.04 Introduction Nginx is a popular open-source web server and reverse proxy software that’s used by millions of websites. It’s the most widely used…
- Install Nginx Phpmyadmin Ubuntu 18.04 Install Nginx Phpmyadmin Ubuntu 18.04 Introduction to Nginx, PHP, and Ubuntu Nginx is an open-source, high-performance web server written in C and used to serve static and dynamic webpages. It…
- Nginx Ssl Ubuntu 16.04 Nginx SSL Ubuntu 16.04 What is SSL and Nginx? SSL stands for Secure Sockets Layer. It is a protocol used to encrypt communications over the internet. It is a secure…
- Install Wordpress On Ubuntu Vps On Nginx Install Wordpress On Ubuntu VPS On Nginx What is WordPress? WordPress is an open-source, content management system (CMS) top-tier overall that is used to create powerful online presence. It powers…
- Deploy Laravel Nginx Ubuntu 18 Deploy Laravel Nginx Ubuntu 18 Intro to Laravel Laravel is a free, open-source, Model-View-Controller (MVC) web framework written in PHP. It has become one of the most popular web development…
- Install Phpmyadmin On Nginx Ubuntu 18.04 Install Phpmyadmin On Nginx Ubuntu 18.04 Introduction PhpMyAdmin is one of the most popular and widely used web-based database management tools available. It is used for administering, managing and maintaining…
- Ubuntu 16.04 Nginx Ipv6 Ubuntu 16.04 Nginx Ipv6 What is Ubuntu 16.04? Ubuntu 16.04 is a version of the Ubuntu Linux operating system for computers and servers. Ubuntu has been around since 2004, and…
- Hhvm Nginx Ubuntu 16.4 HHVM Nginx Ubuntu 16.4 What is HHVM? HHVM, also known as HipHop Virtual Machine, is a virtual machine developed by Facebook to speed up the execution of PHP code. It…
- Stop Nginx Ubuntu 16.04 Stop Nginx Ubuntu 16.04 What is Nginx? Nginx is a web server and reverse proxy software. It is open source and widely used on the web. It is used to…
- Setting Php Nginx Ubuntu Vps Setting Up a PHP, Nginx, and Ubuntu VPS What is a VPS? A Virtual Private Server (VPS) is a type of virtualized hosting. It works in the same way as…
- Apt-Get Install Nginx Php Mysql Apt-Get Install Nginx Php Mysql What is Apt-Get? Apt-Get is a powerful and innovative command line tool used by Linux-based operating systems such as Debian, Ubuntu, Linux Mint and others.…
- Google Cloud Ubuntu 16 Install Nginx Php Google Cloud Ubuntu 16 Install Nginx Php 1. What is Nginx? Nginx is a powerful open source web server. It is used to serve web pages to the internet and…
- How To Check Whether 2 Nginx Is Installed In Ubuntu How To Check Whether 2 Nginx Is Installed In Ubuntu Overview Nginx is an open-source web server that is used to host web sites and applications. It is very popular…
- Install Nginx Mysql Phpmyadmin Ubuntu Install Nginx Mysql Phpmyadmin Ubuntu 1. Introduction In today's tutorial, we will show you how to install Nginx, MySQL, and phpMyAdmin on Ubuntu 20.04. Nginx is an open source web…
- Linux Nginx Mysql Php Lemp Linux Nginx Mysql Php Lemp What is Linux, Nginx, MySQL, PHP (LEMP)? Linux, Nginx, MySQL, PHP (LEMP) is a software stack combination of Linux, Nginx, MySQL, and PHP, commonly referred…
- Completely Uninstall Phpmyadmin On Nginx Ubuntu 18.04 Completely Uninstall PhpMyAdmin On NGINX Ubuntu 18.04 Introduction PhpMyAdmin is a very popular web-based graphical tool for managing MySQL databases in the Ubuntu operating system. It is used by web…
- How To Deactive Nginx Ubuntu How To Deactivate Nginx Ubuntu Introduction Nginx is a web server that’s popular in the Linux world because of its simple configuration, scalability, and performance. The Apache httpd web server…
- 403 Forbidden Nginx Ubuntu 14.04 403 Forbidden Nginx Ubuntu 14.04 What Is a 403 Forbidden Error? A 403 Forbidden error is an HTTP status code that denotes that a server, upon receiving a request from…
- Install Php Nginx Ubuntu 12.04 Install Php Nginx Ubuntu 12.04 Introduction Welcome to our guide on How to install PHP, Nginx & MySQL on Ubuntu 12.04 LTS. This guide should work on other Linux VPS…
- Install Nginx Php Mysql Ssl & Wordpress On Ubuntu 18.04 Install Nginx Php Mysql Ssl & Wordpress On Ubuntu 18.04 What is Nginx, Php, Mysql, SSL and Wordpress? Nginx is a high-performance web server that is widely used to serve…