Ubuntu: 安装MongoDB, Install MongoDB With Apache2, PHP 7.2 Support On Ubuntu 16.04 / 17.10 / 18.04

Step 1: Install Apache2 HTTP Server  – 安装Apache2,如果已经安装,请略过

命令行安装

sudo apt update
sudo apt install apache2

开启和关闭apache2的命令

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

http://localhost 打开后,如果可以显示下图,说明安装成功

Ubuntu: 安装MongoDB, Install MongoDB With Apache2, PHP 7.2 Support On Ubuntu 16.04 / 17.10 / 18.04
Ubuntu: 安装MongoDB, Install MongoDB With Apache2, PHP 7.2 Support On Ubuntu 16.04 / 17.10 / 18.04

Step 2: Add MongoDB Repository 添加 MongoDB 源

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

STEP 3: INSTALL MONGODB 安装 MongoDB

sudo apt update
sudo apt install mongodb-org mongodb-org-server

By default, (默认监听端口是27017)MongoDB listens on port 27017…

开启和关闭的命令

sudo systemctl start mongod
sudo systemctl status mongod

看到以下内容,说明已经开启mongodb

ikeepstudying@ubuntu1604:~$ sudo systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-01-27 08:53:42 CST; 13min ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 2383 (mongod)
    Tasks: 23
   Memory: 60.7M
      CPU: 2.613s
   CGroup: /system.slice/mongod.service
           └─2383 /usr/bin/mongod --config /etc/mongod.conf

To connect to MongoDB shell, run the commands below:

Step 4: Install PHP 7.2 On Ubuntu 安装php7.2 ,如果已经安装,请略过

安装更新源

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

安装php7.2及插件

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

Step 5: Install MongoDB PHP Drivers 安装MongoDB PHP驱动

sudo pecl install mongodb

需要把 mongodb.so 拓展加到 php.ini中

sudo bash
sudo echo "extension=mongodb.so" >> /etc/php/7.2/apache2/php.ini

重启Apache2

sudo systemctl restart apache2.service

为了确认是否成功,创建一个php文件 phpinfo.php

<?php phpinfo(); ?>

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

http://localhost/phpinfo.php

查看如下:

Ubuntu: 安装MongoDB, Install MongoDB With Apache2, PHP 7.2 Support On Ubuntu 16.04 / 17.10 / 18.04
Ubuntu: 安装MongoDB, Install MongoDB With Apache2, PHP 7.2 Support On Ubuntu 16.04 / 17.10 / 18.04

That’s it!

 

下一步可以进入:PHP操作远程mongodb数据库, MongoDB PHP Library, php connects with remote MongoDB

 

本文:Ubuntu: 安装MongoDB, Install MongoDB With Apache2, PHP 7.2 Support On Ubuntu 16.04 / 17.10 / 18.04