Skip to content

Instantly share code, notes, and snippets.

@Hikari9
Last active February 16, 2016 11:26
Show Gist options
  • Save Hikari9/8d0e42d147e9a456039a to your computer and use it in GitHub Desktop.
Save Hikari9/8d0e42d147e9a456039a to your computer and use it in GitHub Desktop.
Serve new folder for apache2.
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
const string SITES_AVAILABLE_DIR = "/etc/apache2/sites-available/";
const string PORTS_CONF = "/etc/apache2/ports.conf";
bool file_exists(string filename) {
return ifstream(filename.c_str()).good();
}
int to_int(string str) {
int x;
istringstream(str) >> x;
return x;
}
string get_site_name(int argc, char **args) {
string site_name = (argc > 1 ? args[1] : "");
while (true) {
if (site_name.empty()) {
cout << "Enter site name (no spaces): ";
cin >> site_name;
}
if (file_exists(SITES_AVAILABLE_DIR + site_name + ".conf")) {
break; // skip prompt
cout << "Warning! File \"" << SITES_AVAILABLE_DIR << site_name << ".conf\" exists!\nOverwrite? (y/n) ";
char cmd; cin >> cmd;
if (cmd == 'y' || cmd == 'Y')
break;
else
site_name = "";
} else break;
}
return site_name;
}
set<int> get_current_ports() {
ifstream fin(PORTS_CONF.c_str());
string buffer, listen = "Listen ";
set<int> current_ports;
while (getline(fin, buffer)) {
if (buffer.substr(0, listen.length()) == listen) {
int port = to_int(buffer.substr(listen.length()));
current_ports.insert(port);
}
}
fin.close();
return current_ports;
}
int get_port(int argc, char **args) {
set<int> current_ports = get_current_ports();
cout << "[Ports being used:";
for (set<int>::iterator it = current_ports.begin(); it != current_ports.end(); ++it)
cout << ' ' << *it;
cout << ']' << endl;
int port = (argc > 2 ? to_int(args[2]) : -1);
while (true) {
if (port == -1) {
cout << "Enter port (e.g. 8888): ";
cin >> port;
}
if (current_ports.count(port)) {
break; // skip prompt
cout << "Warning! Port \"" << port << "\" is already being used!\nContinue? (y/n) ";
char cmd; cin >> cmd;
if (cmd == 'y' || cmd == 'Y')
break;
else
port = -1;
} else break;
}
return port;
}
int main(int argc, char **args) {
{
ifstream test(PORTS_CONF.c_str());
if (!test.good()) {
cout << "You are not root. Try running again using sudo." << endl;
return 1;
}
}
string site_name = get_site_name(argc, args);
int port = get_port(argc, args);
cout << endl;
cout << "Preparing to set up..." << endl;
cout << "Site will be in /var/www/" << site_name << "/" << endl;
cout << "Configuration file will be saved in " << SITES_AVAILABLE_DIR << site_name << ".conf" << endl;
cout << "Setting up port at http://localhost:" << port << " ..." << endl;
cout << "Writing configuration to " << PORTS_CONF << " ..." << endl;
set<int> current_ports = get_current_ports();
system((string("a2dissite ") + site_name).c_str());
system("service apache2 stop");
if (!current_ports.count(port)) {
ifstream fin(PORTS_CONF.c_str());
string buffer, listen = "Listen ";
ostringstream stream;
bool done = false;
while (getline(fin, buffer)) {
stream << buffer << endl;
if (!done && buffer.substr(0, listen.length()) == listen) {
stream << endl;
stream << "# site: /var/www/" << site_name << endl;
stream << "Listen " << port << endl << endl;
done = true;
}
}
fin.close();
if (!done) {
stream << endl;
stream << "# site: /var/www/" << site_name << endl;
stream << "Listen " << port << endl << endl;
done = true;
}
ofstream out(PORTS_CONF.c_str());
stream << flush;
// cout << stream.str() << endl;
out << stream.str() << flush;
out.close();
}
cout << "Creating " << SITES_AVAILABLE_DIR << site_name << ".conf ..." << endl;
{
string path = SITES_AVAILABLE_DIR + site_name + ".conf";
ofstream out(path.c_str());
out << "<VirtualHost *:" << port << ">" << endl;
out << "ServerName " << site_name << ".localhost" << endl;
out << "ServerAdmin webmaster@localhost" << endl;
out << "DocumentRoot /var/www/" << site_name << endl;
out << endl;
out << "# Allow subdirectory access" << endl;
out << "<Directory /var/www/" << site_name << "/>" << endl;
out << "\tOptions Indexes FollowSymLinks MultiViews" << endl;
out << "\tAllowOverride All" << endl;
out << "\tOrder allow,deny" << endl;
out << "\tallow from all" << endl;
out << "</Directory>" << endl;
out << endl;
out << "LogLevel info debug" << endl;
out << "ErrorLog ${APACHE_LOG_DIR}/error.log" << endl;
out << "CustomLog ${APACHE_LOG_DIR}/access.log combined" << endl;
out << "</VirtualHost>" << endl;
out.close();
}
cout << "Starting apache server..." << endl;
system("service apache2 start");
cout << "Running 'a2ensite " << site_name << "' ..." << endl;
system((string("a2ensite ") + site_name).c_str());
cout << "Reloading apache server..." << endl;
system("service apache2 reload");
cout << "Done!" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment