How Do You Read in Keyboard Input from a User and Invoke a Unix Shell Command with a C++ Program?

Problem scenario
You want to see how C++ works.  You want to read in user input from the keyboard and have the C++ program run a bash command.  How do you do this?

Solution
Compile this code (e.g., as a file named "test.cpp" with "g++ test.cpp") and run it (e.g., ./a.out):

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main ()
{
  int i;
  string x;
  cout << "Please enter an integer value: ";
  cin >> i;
  cout << "The value you entered is " << i;
  cout << " and its double is " << i*2 << ".\n";
  x = system("/bin/date");
  return 0;
}

Where Are the apt-get Repo Configuration Files for a Debian/Ubuntu Linux Server?

Problem scenario
You run an apt-get command, and you get an error about a .deb file's source not being found.  For example you try to run an apt-get command to install a package, but you are blocked by this error:
"E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/p/postgresql-9.5/libpq-dev_9.5.6-0ubuntu0.16.04_amd64.deb  404  Not Found [IP:..."

An updated version for the deb file may have superseded the older version.  Or the repository server's hostname may have changed or its IP address has changed.  You want to change where your Ubuntu server retrieves files from, how do you do this?

Solution
Possible solution #1
As root or with a "sudo " in front of it, run this command:  "apt-get update"
Possible solution #2
Update /etc/apt/sources.list to point to new repositories.
Possible solution #3
Update your /etc/resolv.conf files or /etc/hosts file.  The IP address of the source of the repositories may have changed.

How Do You Fix the Problem “Unable to locate package php-pgsql”?

Problem scenario
You are trying to integrate PHP and Postgresql on you Ubuntu/Debian Linux OS.  You run this command:  sudo apt-get -y install ​php-pgsql

You receive this error:

Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package ​php-pgsql

What should you do?

Solution
#1  Find out what version of php you have.  Run this:
php --version

#2  If you have version 5, run this: sudo apt-get -y install php5-pgsql

If you have version 7, run this: sudo apt-get -y install php7.0-pgsql

#3  Install libapache2-mod-php.  Run this command:
sudo apt-get -y install libapache2-mod-php

How Do You Troubleshoot This Error “…docker.sock: read:: connection reset by peer”?

Problem scenario
You have Docker installed.  When running "docker ps" you get this error:  "An error occurred trying to connect: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json: read unix @->/var/run/docker.sock: read: connection reset by peer"

How do you fix this error so you can use Docker?

Solution
Just because Docker was installed does not mean that the kernel of your OS is supported.  Docker may not work despite being installed.  To find out if you kernel is supported, try this command:  docker daemon

You may also see this link for more information.  To reinstall Docker, see this posting.

How Do You Troubleshoot a PHP Page Showing the Raw Code in a Web Browser?

Problem scenario
You created a PHP web page, but it is not rendering correctly in a web browser.  You see the raw PHP code or text.  What do you do to get the PHP to display correctly as a web UI (and not raw code and uninterpreted text)?

Solution
Possible solution #1
Install PHP.  For assistance with installing it on a Debian/Ubuntu server, see this posting.  For assistance with installing it on CentOS/RHEL/Fedora, see this posting.

Possible solution #2
If you are using a Debian derivative of Linux, install libapache2-mod-php.  Here is the command that you would run (either with a sudo in front of it or as root):

apt-get -y install libapache2-mod-php

How Do You Set up Nginx to Be a Reverse Proxy That Conveys Inbound Connections to the Web Server with the Fewest Active Connections?

Problem scenario
You want your website to perform well.  By default Nginx's load balancing system uses the round-robin technique.  You want available yet under-utilized web servers to handle inbound connection requests (aka someone using a web browser to go to a web page).  Individual users can put a disparate load on a given Nginx instance.  Some website users will spend a significant amount of time on a web page.  Others will invoke various application features that will require more resources than others.  Ultimately you want to be sure that  servers with fewer active connections will receive more traffic to balance the load.  Like a router or networking hardware device, a physical server with a NIC running Nginx can relay traffic to specific servers to balance loads and enable fault-tolerance.  How do you avoid the simple round-robin distribution technique and configure Nginx to balance the HTTP traffic load by relaying it to the available web server with the fewest active connections?

Solution
Prerequisites
You need to have Nginx installed (for CentOS you can click here or for Debian/Ubuntu click here) or you must have configured Nginx as a an HTTP load balancer (or reverse proxy server).  To do the latter, see this article which will actually work for Nginx distributing traffic to regular Nginx websites, Apache websites, or Nginx websites running in Docker containers.  This solution below assumes that no web traffic can bypass the Nginx HTTP load balancer. In some environments, using a special DNS name or port in a web browser can bypass the load balancer.

Procedures
1.  To use Nginx as a reverse proxy or load balancer with the guideline that traffic goes to the web server with the fewest active connections, you need one Nginx web server to be a distributor.  This means modifying the /etc/nginx/conf.d/default.conf file (or /etc/nginx/conf.d/nginx.conf) in two sections or what Nginx people refer to as "blocks."  (If you want to know the difference between these two files, see this posting.) The changes that need to take place are these:

a) Comment out in the server {} section these lines under the "location / {" section (but there may only be one of these stanzas that you find):

# root /usr/share/nginx/html;
# index index.html index.htm;

b)  Do not comment out the "location / {" stanza itself.  Do add a line under this stanza near the two commented-out lines above with this as the content indented the same way as the original lines above:

proxy_pass http://backend;

c) At the very bottom of the file, add these lines (but replace the IP addresses with web servers, e.g., regular Nginx instances):

upstream backend {
   least_conn;
   server 10.10.0.1;
   server 10.10.0.2;
   server 10.10.0.3;
}

2.  The Nginx services then need to be brought down and brought back up (cycled).

FFR
If you want to need to troubleshoot problems with your Nginx load balancer, see this link.

The default web page page for Nginx installations can be located on a Linux server /var/www/html/index.nginx-debian.html

How Do You Use the C++ Connector for Libpqxx?

Problem scenario
You want to integrate C++ programs with Postgresql on an Ubuntu instance of AWS.  You want to use pqxx (http://pqxx.org/development/libpqxx/) as it is an API connector for C++ to Postgres.  How do you use libqxx?

Solution
#1  Install Postgresql.  To install Postgresql, run this as root or with a "sudo " in front of it: apt-get install postgresql

#2  To execute SQL commands, use this the following Linux commands:

sudo -i -u postgres

psql

#3  Create a ROLE with a user that can log in.  For example, run this (but substitute contuser with the username and integ3727 with the password of your choice):

CREATE ROLE contuser WITH LOGIN PASSWORD 'integ3727';

#4  Exit the Postgres database by running these two commands:

\q

exit

#5  Install libpqxx.  See this link if necessary.

#6  Create a test program (e.g., contint.cpp).  The content of this program (e.g., contint.cpp) should be the following (and please note that every line except the "connection" line was taken from this link).

#include <iostream>
#include <pqxx/pqxx>

using namespace std;
using namespace pqxx;

int main(int argc, char* argv[])
{
   try{
      connection C("dbname=postgres user=contuser password=integ3727 \
      hostaddr=127.0.0.1 port=5432");
      if (C.is_open()) {
         cout << "Opened database successfully: " << C.dbname() << endl;
      } else {
         cout << "Can't open database" << endl;
         return 1;
      }
      C.disconnect ();
   }catch (const std::exception &e){
      cerr << e.what() << std::endl;
      return 1;
   }
}

#7  Compile the program using a special command (substitute contint.cpp with the name of your C++ program):
# g++ -I /usr/local/include/pqxx/ -L /usr/local/lib/ -I /usr/local/pgsql/include/ -L /usr/local/pgsql/lib/ contint.cpp -lpqxx -lpq

Citation: This command was largely taken from this link.

#8  Run the byte code to prove it all works:

./a.out

How Do You Install pqxx on an AWS Instance of Ubuntu?

Problem scenario
You try to compile a C++ program (i.e., a .cpp file).  You get this message:  "fatal error:  pqxx/pqxx: No such file or directory."  You want to install pqxx on your Ubuntu server in AWS.  What do you do?

Solution
#1  Log into the server.  Run this command as root or with a sudo in front it:

add-apt-repository ppa:jtv/ppa 
#Press enter in response to the prompt from the command above.

#2  Run this command as root or with a sudo in front it:
apt-get update

#3  Update this file: /etc/apt/sources.list
Append these stanzas to the sources.list file:

deb http://ppa.launchpad.net/jtv/ppa/ubuntu xenial main
deb-src http://ppa.launchpad.net/jtv/ppa/ubuntu xenial main

#4  Run this command as root or with a sudo in front it:
apt-get -y install libpqxx-dev

How Do You Find the Version of Postgres without Running a SQL Command?

Problem scenario
To find the version of Postgres when you can enter the SQL database, you would run this SQL command with no quotes: "SELECT version();"

In this situation you cannot enter the Postgres database (and therefore can run no SQL commands).  You want to know more about the installation of Postgres to troubleshoot why you cannot enter it.  How do you find the version of Postgresql that is installed?

Solution
For Debian/Ubuntu distributions of Linux, run:
dpkg -l | grep postgres

For CentOS/RedHat/Fedora distributions of Linux, run:
rpm -qa | grep postgres