Skip to content

Instantly share code, notes, and snippets.

@fschutt
Last active June 19, 2016 20:32
Show Gist options
  • Save fschutt/a2e0dadfcaf3d9fd5549bd59a67b0fce to your computer and use it in GitHub Desktop.
Save fschutt/a2e0dadfcaf3d9fd5549bd59a67b0fce to your computer and use it in GitHub Desktop.
INSTALLATION INSTRUCTIONS FOR NGINX / PGSQL / C++ DEV STACK
1. Add user to sudoers file
sudo su -
visudo //(opens /etc/sudoers), add the following line to the file
felix ALL=(ALL) ALL
(1.1) If you can't connect to a network during installation, do this next:
sudo nano /etc/apt/sources.list
deb http://ftp.de.debian.org stable main
2. PostgreSQL
//https://www.linux.com/blog/postgresql-c-tutorial
sudo apt-get install postgresql-9.1 postgresql-client postgresql-client-9.1 postgresql-client-common postgresql-common postgresql-contrib postgresql-contrib-9.1
sudo apt-get install pgadmin3
//TODO: Figure out how to connect to a server
//Probably do this via PGAdmin, not via commandline
//Add test database and test user
3. KDevelop Full + cmake + gcc (don't take the light version of KDevelop!)
sudo apt-get install build-essential kdevelop-full
4. NGINX:
sudo apt-get install libfcgi-dev spawn-fcgi nginx
sudo apt-get install curl // optional
2. Open nginx.conf or /etc/nginx/fastcgi_params:
location / {
fastcgi_pass 127.0.0.1:8000;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS $https;
}
3. Restart nginx
sudo service nginx start
OR
sudo nginx reload
4. Building the project:
sudo apt-get install autoconf automake1.10 libtool
make
5. Spawn the fcgi app on port 8000 with no fork
spawn-fcgi -p 8000 -n hello_world
//Important links:
//
set(PostgreSQL_ADDITIONAL_VERSIONS "9.4" "9.4.4")
find_package(PostgreSQL REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(test_libpqxx ${SOURCE_FILES} main.cpp)
target_link_libraries(test_libpqxx pq)
target_link_libraries(test_libpqxx pqxx)
#include <iostream>
#include "fcgio.h"
using namespace std;
int main(void) {
// Backup the stdio streambufs
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();
FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0, 0);
while (FCGX_Accept_r(&request) == 0) {
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);
cout << "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
<< " <head>\n"
<< " <title>Hello, World!</title>\n"
<< " </head>\n"
<< " <body>\n"
<< " <h1>Hello, World!</h1>\n"
<< " </body>\n"
<< "</html>\n";
// Note: the fcgi_streambuf destructor will auto flush
}
// restore stdio streambufs
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
return 0;
}
all:
g++ fastcgi.cpp -Wall -lfcgi++ -lfcgi -o hello_world
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char** argv)
{
connection C("dbname=testpgdb user=testuser password=testpass hostaddr=127.0.0.1 port=5432");
string tableName("tabletwo");
if (C.is_open()) {
cout << "We are connected to" << C.dbname() << endl;
}
else {
cout << "We are not connected!" << endl;
return 0;
}
work Q(C);
try {
Q.exec("DROP TABLE " + tableName);
Q.commit();
}
catch (const sql_error&) {
}
work T(C);
T.exec("CREATE TABLE " + tableName + " (id integer NOT NULL, name character varying(32) NOT NULL, salary integer DEFAULT 0);");
tablewriter W(T, tableName);
string load[][3] = {
{ "1", "John", "0" },
{ "2", "Jane", "1" },
{ "3", "Rosa", "2" },
{ "4", "Danica", "3" }
};
for (int i = 0; i < 4; ++i){
W.insert(&load[i][0], &load[i][3]);
}
W.complete();
T.exec("ALTER TABLE ONLY " + tableName + " ADD CONSTRAINT \"PK_IDT\" PRIMARY KEY (id);");
T.commit();
nontransaction N(C);
result R(N.exec("select * from " + tableName));
if (!R.empty()) {
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
cout << '\t' << c[0].as(string()) << '\t' << c[1].as(string()) << '\t' << c[2].as(string()) << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment