Last active
August 8, 2018 11:58
-
-
Save emgee3/0852cccc1a1fd1c418d3 to your computer and use it in GitHub Desktop.
Compile and install MongoDB with SSL support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Compile and install MongoDB with SSL support | |
# tested an works on Ubuntu 12.04 LTS x64 and Ubuntu 14.04 LTS x64 | |
# | |
set -e | |
set -u | |
set -o pipefail | |
# set -x #debugging | |
# | |
# CONFIGURATION SECTION | |
# | |
export MONGO_VERSION="r2.4.12" # Which version of MongoDB, which corresponds to a git tag | |
# You can list available versions with `git tag | grep -v "rc"` | |
# inside the mongo git respository | |
# Self-signed SSL Certificate settings | |
export SSL_HOSTNAME="${HOSTNAME}" # Should be FQDN of your MongoDB server | |
export SSL_C="US" # Country | |
export SSL_ST="California" # State | |
export SSL_O="Radiant" # Organization | |
export SSL_DAYS="3650" # Valid for in days == 10 years | |
# | |
# SANITY CHECKS | |
# | |
# See http://blog.mongodb.org/post/137788967/32-bit-limitations | |
if [ "${HOSTTYPE}" != "x86_64" ]; then | |
echo "MongoDB should be installed on a 64-bit OS" >&2 | |
exit 1 | |
fi | |
# A non-scientific test compiling MongoDB showed it used around 11 GB storage at its peak | |
cd /usr/src | |
export FREE_SPACE=$(($(stat -f --format="%a*%S" .))) | |
export FREE_SPACE_NEEDED=14000000000 # ~14 GB | |
if [ "${FREE_SPACE}" -le "${FREE_SPACE_NEEDED}" ]; then | |
echo "Not enough free space in /usr/src" >&2 | |
exit 1 | |
fi | |
# Compilation will fail without 2+ GB of RAM and/or SWAP | |
export TOTAL_RAM=`free -mt | grep Total | awk '{print $2}'` | |
if [ ${MONGO_VERSION} = "r2.4.12" ]; then | |
export RAM_NEEDED=3000 | |
else | |
export RAM_NEEDED=5000 | |
fi | |
if [ ${TOTAL_RAM} -lt ${RAM_NEEDED} ]; then | |
echo "Compiliation needs more than ${TOTAL_RAM} ram/swap space" >&2 | |
exit 1 | |
fi | |
# | |
# PREREQUSITIES | |
# | |
# install all the prerequsite packages needed for compilation | |
export DEBIAN_FRONTEND=noninteractive | |
aptitude install build-essential scons git-core libssl-dev libboost-filesystem-dev \ | |
libboost-program-options-dev libboost-system-dev libboost-thread-dev \ | |
-q -y | |
# clone the source from github, and check out the proper release | |
git clone git://github.com/mongodb/mongo.git | |
cd /usr/src/mongo | |
git checkout "${MONGO_VERSION}" | |
# | |
# PATCHING | |
# | |
if [ "${MONGO_VERSION}" = "r2.4.12" ]; then | |
# fix for BOOST bug https://svn.boost.org/trac/boost/ticket/7242 | |
patch SConstruct <<EOF | |
709a710,711 | |
> "-Wno-unused-function", | |
> "-Wno-unused-local-typedefs", | |
EOF | |
# MongoDB will compile and install properly but will error on the stacktrace test | |
# if this patch isn't applied, which will cause this script to return a failed exit code. | |
# snatched from http://git.alpinelinux.org/cgit/aports/plain/testing/mongodb/mongodb-2.4.4-fix-sharedclient.patch | |
patch src/mongo/SConscript <<EOF | |
459c459 | |
< LIBDEPS=['stacktrace', | |
--- | |
> LIBDEPS=['stacktrace', 'foundation', 'mongocommon', 'alltools', | |
EOF | |
fi | |
# | |
# COMPILATION and INSTALLATION | |
# | |
# this takes ~40 min on a Digital Ocean 2 core/2GB RAM droplet | |
scons core install --64 --ssl -j4 --no-glibc-check --prefix=/usr | |
# run the post-install script which makes the mongo user, creates the data folder | |
# and sets various permissions | |
cd /usr/src/mongo/debian | |
chmod +x postinst | |
./postinst configure | |
# configure upstart and create base config file | |
cp mongodb.upstart /etc/init/mongodb.conf | |
cp mongodb.conf /etc | |
# create a logrotate script so our log files don't overflow | |
cat > /etc/logrotate.d/mongodb-server <<EOF | |
/var/log/mongodb/*.log { | |
weekly | |
rotate 10 | |
copytruncate | |
delaycompress | |
compress | |
notifempty | |
missingok | |
} | |
EOF | |
# | |
# CONFIGURE SSL | |
# | |
# generate a self-signed SSL key/cert | |
cd /etc/ssl | |
openssl req \ | |
-new \ | |
-newkey rsa:4096 \ | |
-x509 \ | |
-days ${SSL_DAYS} \ | |
-nodes \ | |
-subj "/C=${SSL_C}/ST=${SSL_ST}/O=${SSL_O}/CN=${SSL_HOSTNAME}" \ | |
-keyout mongodb.key \ | |
-out mongodb.crt | |
# configure mongo to use the key/cert | |
cat mongodb.key mongodb.crt > mongodb.pem | |
cat >> /etc/mongodb.conf <<EOF | |
sslOnNormalPorts = prefer | |
sslPEMKeyFile = /etc/ssl/mongodb.pem | |
EOF | |
# | |
# CLEANUP | |
# | |
cd ~ | |
rm -rf /usr/src/mongo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment