Ubuntu php mongodb install

How to Install MongoDB with Nginx and PHP on Ubuntu Linux

This post shows students and new users steps to install MongoDB on Ubuntu Linux with Nginx and PHP support. MongoDB, a free open source, NoSQL, High-performance, and ema-free document-oriented database, can be a robust website and application.

For developers using Nginx and PHP with MongoDB database in the backend, MongoDB PHP drivers will be required to enable PHP support. PHP-based applications and MongoDB websites can interact with the database without the drivers.

MongoDB packages are included in Ubuntu default repositories. However, the versions in Ubuntu repositories aren’t the latest. To install the latest, you must install the MongoDB package repository on Ubuntu Linux, and this tutorial will show you how.

Also, for students and new users learning Linux, Ubuntu Linux is the most accessible place to start learning. Ubuntu is the modern, open-source Linux operating system for desktops, servers, and other devices.

Читайте также:  Python print save to file

When you’re ready to get MongoDB setup on Ubuntu with Nginx and PHP support, continue with the steps below:

How to install Nginx on Ubuntu Linux

To install Nginx HTTP on the Ubuntu server, run the commands below…

sudo apt update sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start and enable the Nginx service to always start up with the server boots.

sudo systemctl stop nginx.service sudo systemctl start nginx.service sudo systemctl enable nginx.service

To test the Nginx setup, open your browser and browse to the server hostname or IP address, and you should see the Nginx default test page as shown below.

http://localhost

How to install MongoDB on Ubuntu Linux

We have written a great tutorial on installing and configuring MongoDB on Ubuntu Linux. To read that post, click on the link below:

After reading the post above and installing MongoDB on Ubuntu, continue below with the setup.

How to install PHP on Ubuntu Linux

PHP 7.4 isn’t available on Ubuntu’s default repositories. So to install it, you must get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.4

sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.4

Next, run the commands below to install PHP 7.2 and related modules.

sudo apt install php7.4-fpm php7.4-common php7.4-mongodb php-pear php7.4-dev

After installing PHP 7.4-FPM, run the commands below to open the PHP default config file for Nginx…

How to install MongoDB PHP drivers

MongoDB PHP drivers provide exceptionally thin glue between MongoDB and PHP. Run the commands below to install it.

sudo pecl install mongodb

After that, run the commands below to enable the drivers

sudo bash sudo echo "extension=mongodb.so" >> /etc/php/7.4/fpm/php.ini sudo echo "extension=mongodb.so" >> /etc/php/7.4/cli/php.ini

Configure Nginx to enable PHP support

If you have a custom site file, edit it to enable Nginx PHP support. Or run the commands below to open Nginx’s default site configuration file.

sudo nano /etc/nginx/sites-available/default

Then uncomment or remove the # symbol on the highlighted lines below to enable Nginx PHP support.

Also, add the highlighted index.php to the index line.

server < listen 80; listen [::]:80; root /var/www/html; index index.php index.html index.htm; server_name example.com www.example.com; location / < try_files $uri $uri/ =404; ># pass PHP scripts to FastCGI server # location ~ \.php$ < include snippets/fastcgi-php.conf;# # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; > >

Restart Nginx and PHP-FPM services

sudo systemctl restart nginx.service sudo systemctl restart php7.2-fpm.service

To verify the MongoDB PHP driver is installed and enabled, continue below.

Create a phpinfo.php file in the Nginx root directory by running the commands below.

sudo nano /var/www/html/phpinfo.php

Then type the content below and save the file.

Save the file. Then browse to your server hostname followed by /phpinfo.php

You should see the PHP default test page…

This post showed you how to use MongoDB with Nginx and PHP support. Please use the comment form below if you find any errors above or have something to add.

Richard W

I love computers; maybe way too much. What I learned I try to share at geekrewind.com.

Источник

Install MongoDB with Apache and PHP on Ubuntu Linux

This post shows students and new users how to install MongoDB on Ubuntu with Apache and PHP support. MongoDB, a free open source, NoSQL, High-performance, and document-oriented database, can be a robust website and application.

If you’re developing an application or website that requires MongoDB as its backend database server, the steps below should be a great place to start getting MongoDB working with Apache and PHP.

MongoDB packages are included in Ubuntu default repositories. However, the versions in Ubuntu repositories aren’t the latest. To install the latest, you must install the MongoDB package repository on Ubuntu Linux, and this tutorial will show you how.

Also, for students and new users learning Linux, Ubuntu Linux is the most accessible place to start learning. Ubuntu is the modern, open-source Linux operating system for desktops, servers, and other devices.

When you’re ready to get MongoDB setup on Ubuntu with Apache2 and PHP support, continue with the steps below:

How to install Apache on Ubuntu Linux

To install the Apache2 HTTP server on Ubuntu, run the commands below.

sudo apt update sudo apt install apache2

After installing Apache2, the commands below can be used to stop, start and enable the Apache2 service to always start up with the server boots.

sudo systemctl stop apache2.service sudo systemctl start apache2.service sudo systemctl enable apache2.service

To test the Apache2 setup, open your browser and browse to the server hostname or IP address, and you should see the Apache2 default test page as shown below.

http://localhost

How to install MongoDB on Ubuntu Linux

We have written a great tutorial on installing and configuring MongoDB on Ubuntu Linux. To read that post, click on the link below:

After reading the post above and installing MongoDB on Ubuntu, continue below with the setup.

How to install PHP on Ubuntu Linux

PHP 7.4 may not be available on Ubuntu default repositories. Therefore, to install it, you must get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.4

sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.4

Next, run the commands below to install PHP 7.4 and related modules.

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-common php7.4-cli php7.4-mongodb php-pear php7.4-dev

After installing PHP 7.2, run the commands below to open the PHP default config file for Apache2.

How to install MongoDB PHP drivers

MongoDB PHP drivers provide exceptionally thin glue between MongoDB and PHP. Run the commands below to install it.

sudo pecl install mongodb

After that, run the commands below to enable the drivers

sudo bash sudo echo "extension=mongodb.so" >> /etc/php/7.4/apache2/php.ini
sudo systemctl restart apache2.service

To verify the MongoDB PHP driver is installed and allowed, continue below.

Create a phpinfo.php file in the Apache2 root directory by running the commands below.

sudo nano /var/www/html/phpinfo.php

Then type the content below and save the file.

Save the file. Then browse to your server hostname followed by /phpinfo.php

http://localhost/phpinfo.php

You should see the PHP default test page.

This post showed you how to install MongoDB on Ubuntu Linux with Apache and PHP support. Please use the comment form below if you find any errors above or have something to add.

Richard W

I love computers; maybe way too much. What I learned I try to share at geekrewind.com.

Источник

MongoDB PHP Driver

Welcome to the documentation site for the official MongoDB PHP driver. You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the two following components:

While it is possible to use the extension alone, MongoDB recommends using both the extension and the library together. Download the components you need or set up a runnable project by following our tutorials.

Installation

First, make sure you have a recent version of PHP installed on your system. See the official PHP manual

for download and installation instructions.

Install the PHP MongoDB Extension before installing the PHP Library for MongoDB. You can install the extension using PECL

$ sudo pecl install mongodb

Finally, add the following line to your php.ini file:

Note

On some systems, there may be multiple INI files for individual SAPIs (e.g. CLI, FPM). Make sure to enable the extension in all SAPIs that you need.

The preferred method of installing the PHP library is with Composer

by running the following from your project root:

$ composer require mongodb/mongodb

Once you have installed the library, ensure that your application includes Composer’s autoloader as in the following example:

require_once __DIR__ . '/vendor/autoload.php';

Additional installation instructions may be found in the library documentation .

Connect to MongoDB Atlas

You can use the following connection snippet to test your connection to your MongoDB deployment on Atlas:

use Exception;
use MongoDB\Client;
use MongoDB\Driver\ServerApi;
// Replace the placeholder with your Atlas connection string
$uri = '';
// Specify Stable API version 1
$apiVersion = new ServerApi(ServerApi::V1);
// Create a new client and connect to the server
$client = new MongoDB\Client($uri, [], ['serverApi' => $apiVersion]) ;
try
// Send a ping to confirm a successful connection
$client->selectDatabase('admin') ->command(['ping' => 1]) ;
echo "Pinged your deployment. You successfully connected to MongoDB!\n";
> catch (Exception $e)
printf($e->getMessage() ) ;
>

This connection snippet uses the Stable API feature, which you can enable when using the PHP driver v1.9 and later to connect to MongoDB Server v5.0 and later. When you use this feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.

To learn more about the Stable API feature, see Stable API in the Server manual.

Note

Starting from Feburary 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.

Connect to MongoDB Atlas Without the Stable API

If you are using a version of MongoDB or the driver that doesn’t support the Stable API feature, you can use the following code snippet to test your connection to your MongoDB deployment on Atlas:

use Exception;
use MongoDB\Client;
// Replace the placeholder with your Atlas connection string
$uri = '';
// Create a new client and connect to the server
$client = new MongoDB\Client($uri) ;
try
// Send a ping to confirm a successful connection
$client->selectDatabase('admin') ->command(['ping' => 1]) ;
echo "Pinged your deployment. You successfully connected to MongoDB!\n";
> catch (Exception $e)
printf($e->getMessage() ) ;
>

Connect to a MongoDB Server on Your Local Machine

If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:

Important

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.

After you successfully start your MongoDB server, specify your connection string in your driver connection code.

If your MongoDB Server is running locally, you can use the connection string «mongodb://localhost:» where is the port number you configured your server to listen for incoming connections.

If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings .

To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

Compatibility

Due to potential problems representing 64-bit integers on 32-bit platforms, users are advised to use 64-bit environments. When using a 32-bit platform, be aware that any 64-bit integer read from the database will be returned as a MongoDB\BSON\Int64

instance instead of a PHP integer type.

MongoDB Compatibility

The following compatibility table specifies the recommended version(s) of the PHP driver for use with a specific version of MongoDB.

The first column lists the driver version(s).

Источник

Оцените статью