FROM ubuntu:latest

#################################################### Adding new user and group ####################################################
ENV myuser=jenkins
ENV myuserid=1002
ENV mygroup=jenkins
ENV mygroupid=1002

RUN groupadd $mygroup -g $mygroupid
RUN useradd -u $myuserid -g $mygroupid $myuser

###################################################### Update APT #######################################################
RUN apt-get update -y

###################################################### Utilities #######################################################

#install git curl wget zip unzip nano
RUN apt-get install -y git curl wget zip unzip nano && \
apt-get -y clean all

######################################################### Apache #########################################################

RUN apt-get install -y apache2 && \
apt-get -y clean all

######################################################### PHP #########################################################

RUN apt-get install -y php7.0-fpm php7.0-mysql php7.0-curl php7.0-json php7.0-cgi php7.0-xml php7.0 libapache2-mod-fastcgi && \
apt-get -y clean all

######################################################### Apache Configs #########################################################
## Enabling Mod_Rewrite
RUN a2enmod actions fastcgi
RUN a2enmod rewrite
RUN a2enmod headers
RUN a2enmod expires
RUN a2enmod deflate

## Fixing localhost error.
RUN echo "ServerName localhost" >> /etc/apache2/conf-available/servername.conf
RUN a2enconf servername

RUN rm /etc/apache2/sites-enabled/000-default.conf
RUN echo "<VirtualHost *:80> \n\
        ServerAdmin webmaster@localhost \n\
        DocumentRoot /var/www/html \n\
 \n\
        ErrorLog ${APACHE_LOG_DIR}/error.log \n\
        CustomLog ${APACHE_LOG_DIR}/access.log combined \n\
 \n\
        <Directory /var/www/html> \n\
                Options Indexes FollowSymLinks MultiViews \n\
                AllowOverride All \n\
                Order allow,deny \n\
                allow from all \n\
        </Directory> \n\
        <IfModule mod_fastcgi.c> \n\
            AddHandler php7-fcgi .php \n\
            Action php7-fcgi /php7-fcgi \n\
            Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi \n\
            FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /run/php/php7.0-fpm.sock -pass-header Authorization \n\
            <FilesMatch '.+\.ph(p[345]?|t|tml)$'> \n\
                    SetHandler php7-fcgi \n\
            </FilesMatch> \n\
            <Directory '/usr/lib/cgi-bin'> \n\
            Require all granted \n\
            </Directory> \n\
        </IfModule> \n\
</VirtualHost>" >> /etc/apache2/sites-enabled/000-default.conf

RUN x=";clear_env = no" && y="clear_env = no" && sed -i -e "s/$x/$y/g" /etc/php/7.0/fpm/pool.d/www.conf
#################################################### php should run as my user ####################################################
RUN x="user = www-data" && y="user = $myuser" && sed -i -e "s/$x/$y/g" /etc/php/7.0/fpm/pool.d/www.conf
RUN x="group = www-data" && y="group = $mygroup" && sed -i -e "s/$x/$y/g" /etc/php/7.0/fpm/pool.d/www.conf
#################################################### allow 1gb upload size ####################################################
RUN x="upload_max_filesize = 2M" && y="upload_max_filesize = 1024M" && sed -i -e "s/$x/$y/g" /etc/php/7.0/fpm/php.ini
RUN x="post_max_size = 8M" && y="post_max_size = 1024M" && sed -i -e "s/$x/$y/g" /etc/php/7.0/fpm/php.ini
#################################################### Setting up apache ####################################################
ADD start.sh /start.sh
RUN chmod u+x /start.sh

EXPOSE 80

ENTRYPOINT ["/start.sh"]
