Run a bash script in docker-compose - apache

I'm trying to bring up an OwnCloud container on my docker server via docker compose. I'm using the stock OwnCloud image from Docker Hub. Here is my yaml file:
version: '3.1'
services:
owncloud:
image: owncloud
restart: always
ports:
- 80:80
- 443:443
volumes:
- '/etc/docker/owncloud/data:/var/www/html'
- '/etc/docker/owncloud/ssl.sh:/etc/apache2/ssl.sh'
networks:
default:
ipv4_address: 192.168.226.28
mysql:
image: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: ********
volumes:
owncloud_data:
driver: local
networks:
default:
external:
name: pub_net
It works great. The container comes up and I can access it via http. Now of course I want to access via https so you can see bash script I've sent into my container. Here are the contents of that script:
#!/bin/bash
a2enmod ssl
a2ensite default-ssl
openssl req -x509 -nodes -days 99999 -newkey rsa:2048 -subj "/C=US/ST=Ohio/L=Cleveland/O=Data/CN=fake.domain.com" -keyout /etc/ssl/private/ssl-cert-snakeoil.key -out /etc/ssl/certs/ssl-cert-snakeoil.pem
service apache2 restart
Now, if I manually bash into the container and run that script it works great. I can access OwnCloud via https. Here's where it get's annoying. I want to automate the process of running that script so when the container updates its part of the rebuild process. So far I've tried adding a "command" string in the yaml file like this:
version: '3.1'
services:
owncloud:
image: owncloud
restart: always
ports:
- 80:80
- 443:443
volumes:
- '/etc/docker/owncloud/data:/var/www/html'
- '/etc/docker/owncloud/ssl.sh:/etc/apache2/ssl.sh'
networks:
default:
ipv4_address: 192.168.226.28
command: /etc/apache2/ssl.sh
mysql:
image: mariadb
restart: always
environment:
MYSQL_ROOT_PASSWORD: ********
volumes:
owncloud_data:
driver: local
networks:
default:
external:
name: pub_net
When I run docker-compose up it does appear to run the script, except it runs it over and over again, and the apache service keeps restarting. Here is a snippet of the logs:
Restarting Apache httpd web server: apache2.
Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Module socache_shmcb already enabled
dule ssl already enabled
Site default-ssl already enabled
Generating a RSA private key
................+++++
...............+++++
writing new private key to '/etc/ssl/private/ssl-cert-snakeoil.key'
-----
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.226.28. Set the 'ServerName' directive globally to suppress this message
[Wed Oct 07 21:09:05.504798 2020] [ssl:warn] [pid 33] AH01906: 192.168.226.28:443:0 server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
[Wed Oct 07 21:09:05.504883 2020] [ssl:warn] [pid 33] AH01909: 192.168.226.28:443:0 server certificate does NOT include an ID which matches the server name
Restarting Apache httpd web server: apache2.
Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Module socache_shmcb already enabled
dule ssl already enabled
Site default-ssl already enabled
Generating a RSA private key
....................................+++++
...............................+++++
writing new private key to '/etc/ssl/private/ssl-cert-snakeoil.key'
-----
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.226.28. Set the 'ServerName' directive globally to suppress this message
[Wed Oct 07 21:09:15.145928 2020] [ssl:warn] [pid 33] AH01906: 192.168.226.28:443:0 server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
[Wed Oct 07 21:09:15.146014 2020] [ssl:warn] [pid 33] AH01909: 192.168.226.28:443:0 server certificate does NOT include an ID which matches the server name
Restarting Apache httpd web server: apache2.
Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Module socache_shmcb already enabled
dule ssl already enabled
Site default-ssl already enabled
Generating a RSA private key
...........................+++++
..........................................+++++
writing new private key to '/etc/ssl/private/ssl-cert-snakeoil.key'
-----
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.226.28. Set the 'ServerName' directive globally to suppress this message
[Wed Oct 07 21:09:29.712422 2020] [ssl:warn] [pid 33] AH01906: 192.168.226.28:443:0 server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
[Wed Oct 07 21:09:29.712500 2020] [ssl:warn] [pid 33] AH01909: 192.168.226.28:443:0 server certificate does NOT include an ID which matches the server name
I'm not sure where to go from here. Any ideas?

Docker container command should run for as long as you expect for container to run.
When your script starts, there is completely nothing else running in container. There is no apache2 or anything, just your script. And it ends with service apache2 restart and quits right after. It doesn't care about any background processes that you just started. It only cares that your foreground process - your bash script - has finished already.
As you can see in your image (using latest version at time of writing this): https://hub.docker.com/layers/owncloud/library/owncloud/latest/images/sha256-57e690e039c947e4de6bdae767b57b402d3ed9b9ed9f12ba5d31d3cf92def4b8?context=explore it is using CMD ["apache2-foreground"] to run. And that's how you should end your bash script so it also runs apache2 in foreground:
#!/bin/bash
a2enmod ssl
a2ensite default-ssl
openssl req -x509 -nodes -days 99999 -newkey rsa:2048 -subj "/C=US/ST=Ohio/L=Cleveland/O=Data/CN=fake.domain.com" -keyout /etc/ssl/private/ssl-cert-snakeoil.key -out /etc/ssl/certs/ssl-cert-snakeoil.pem
apache2-foreground

Related

Getting an error after renewing the SSL certificate. apache webserver

I have replaced the certificate and private key to renew my SSL certificate on my Linux server. This is for APACHE by the way. I am positive I am using the right private key, and in the ssl.config file, I have directed the path to the correct places.
i.e - SSLCertificateFile & SSLCertificateKeyFile.
But I still get this error below:
" AH01909: RSA certificate configured for hostname:443 does NOT include an ID which matches the server name
[Wed May 20 21:17:33.432341 2020] [ssl:emerg] [pid 2607] AH02238: Unable to configure RSA server private key
[Wed May 20 21:17:33.432366 2020] [ssl:emerg] [pid 2607] SSL Library Error: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch"
what could I be missing?
#Oluwatobi Elugbadebo. It sounds like you have used the wrong hostname to generate the certificates. Not knowing your environment, I cannot comment further. However I would recommend using Let's Encrypt / Certbot for free ssl and very minimal setup to turnkey add SSL to any apache2 hosted domain. It will handle everything related to the cert and modification of apache files.
https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-centos-7

SSL not working on ubuntu server with multiple vhosts

I run an Ubuntu (17.04) server that is hosting multiple sites. One of the sites I'm hosting now is an eCommerce site and needs an SSL certificate. I've gone through many different tutorials and I've followed every step but when I go to the site with HTTPS I'm just getting an error page.
The domain name in the screenshots below is fake.
First I bought the SSL certificate from Comodo. They requested the CSR from the server so I generated it using this command:
openssl req -new -newkey rsa:2048 -nodes -keyout domain.key -out domain.csr
I pasted the CSR and generated the CRT without issue. Now I have the CRT from Comodo and Updated my vhost for a secure connection:
<VirtualHost *:443>
ServerAdmin test#test.com
ServerName www.domain.com
ServerAlias domain.com
DirectoryIndex index.php
DocumentRoot /var/www/html/domain
SSLEngine on
SSLCertificateFile /etc/ssl/certs/www_domain_com.crt
SSLCertificateKeyFile /etc/ssl/private/domain_com.key
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I then ran:
a2enmod ssl
And finally restarted apache. Now when I go to the site I'm only seeing this error page:
Looking at the apache error log the only hint to what might be wrong is the below message:
[Thu Aug 17 16:42:42.746221 2017] [mpm_prefork:notice] [pid 19871] AH00169: caught SIGTERM, shutting down
[Thu Aug 17 16:42:42.836087 2017] [ssl:warn] [pid 22306] AH01909: 2001:4802:7801:103:be76:4eff:fe20:7c04:443:0 server certificate does NOT include an ID which matches the server name
[Thu Aug 17 16:42:42.874200 2017] [ssl:warn] [pid 22316] AH01909: 2001:4802:7801:103:be76:4eff:fe20:7c04:443:0 server certificate does NOT include an ID which matches the server name
[Thu Aug 17 16:42:42.878354 2017] [mpm_prefork:notice] [pid 22316] AH00163: Apache/2.4.25 (Ubuntu) OpenSSL/1.0.2g configured -- resuming normal operations
[Thu Aug 17 16:42:42.878374 2017] [core:notice] [pid 22316] AH00094: Command line: '/usr/sbin/apache2'
Any help would really be appreciated. This is the first time this server has had an SSL website so maybe I am missing a step with openSSL or something.
Your first mistake is buying a certificate from Namecheap. I just recently had similar problems getting their crt to work. It wasn't until I decided to with Let's Encrypt and used their certbot to generate the SSL certificate that I was able to resolve the problem. I cannot prove this, but I suspect there is a problem with Namecheap's bundling tool which builds the signed certificate.
Let's Encrypt is a completely free open source project to solve your HTTPS needs. I also have multiple virtual hosts and got up and running with Let's Encrypt in minutes.
https://letsencrypt.org/

Configuring Two Way Client Auth SSL Certificate on Apache

I am currently developing an API to communicate with an external service and they require that we use a two way auth with the certificate they sent.
I have received two files: .pem and .cer, both files have the -----BEGIN CERTIFICATE-----
I've tried several different ways to configure this on Apache 2.4, using SSLCACertificateFile, but all I get is errors saying it could not start server:
[Mon Dec 05 10:29:06.434853 2016] [ssl:emerg] [pid 6112] AH02572: Failed to configure at least one certificate and key for my-server.com:443
[Mon Dec 05 10:29:06.434872 2016] [ssl:emerg] [pid 6112] SSL Library Error: error:140A80B1:SSL routines:SSL_CTX_check_private_key:no certificate assigned
Would it be possible to configure this auth with just this files, or should I request the .key in order to configure everything?
Best
You can configure two way client Auth SSL certificate by getting third party certificate. Apache works on OpenSSL command so you have to create CSR, private key and install CA bundle and certificate.
Install Root certificate in Apache: https://www.alphassl.com/support/install-root/apache.html
For SSL installation in Apache: https://www.alphassl.com/support/install-ssl/apache.html
I suggest you to read this article which will help you to understand more deeply: http://www.stefanocapitanio.com/configuring-two-way-authentication-ssl-with-apache/

Apache - Init: Can't open server private key file

I am enabling ssl on my apache running on localhost. I generated a self signed certificate and put it in a usual place:
[david#david web]$ ll /etc/ssl/certs/
...
-rwxrwxrwx. 1 david david 977 Mar 7 13:18 localhost.crt
-rwxrwxrwx. 1 david david 712 Mar 7 13:16 localhost.csr
-rwxrwxrwx. 1 david david 963 Mar 7 13:12 localhost.key
But when I restart Apache, the server fails and I receive the following in the error log:
[Fri Mar 07 13:29:17 2014] [notice] SELinux policy enabled; httpd running as context unconfined_u:system_r:httpd_t:s0
[Fri Mar 07 13:29:17 2014] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Fri Mar 07 13:29:17 2014] [error] (13)Permission denied: Init: Can't open server private key file /etc/ssl/certs/localhost.key
The permissions look ok to me, what am I doing wrong?
This is because you probably uploaded the file(s) first to one of your home directories and then moved them somewhere in the /etc directory.
To correct the context of this file, execute the following command:
restorecon -RvF /etc/ssl/certs/
and restar Apache service httpd restart.
Hope it helps.
David,
I would suggest first to change the permissions and ownership on the key file
Change ownership:sudo chown root:root localhost.key
Change permissions:sudo chmod 600 localhost.key
To fix the problem, I needed to remove the passphrase from the key
Remove passphrase: openssl rsa -in localhost.key -out localhost_nopp.key
After that, just change the SSLCertificateKeyFile parameter to point to localhost_nopp.key and restart Apache.
I am guessing this produces a "clean" file that Apache is now happy to use. Since this appears to a development/test environment, the assumption is that removing the passphrase will not cause a major security issue.
Thanks,
John

apache service can't be start

Things weird:
1:I compiled this LAMP environment with
./configure --enable-layout=RedHat --enable-so --enable-mods-shared=all --with-ldap --enable-ldap -enable-authnz-ldap --with-ssl=/usr/local/ssl --enable-ssl && make && make install
2: This configured for name based virtualhost with SSL(not for a
single virtualhost)
3: I can start the apache service with default website, but can't
start when i config the virtualhost
4: with testing too many times, I found i can start the apache
service with default website first, then modify the http.conf file to
uncomment virtualhost config line, and exec command "apachectl -k
restart", finally the virtualhost will be up.
5: I can't find any error messages from apache error log, It just
show:
[notice] Digest: generating secret for digest authentication ...
[crit] (4)Interrupted system call: Digest: error generating secret: Interrupted system call Configuration Failed
[warn] RSA server certificate CommonName (CN) `*.example.com' does NOT match server name!?
[warn] RSA server certificate CommonName (CN) `*.example.com' does NOT match server name!?
[warn] RSA server certificate CommonName (CN) `*.example.com' does NOT match server name!?
[warn] Init: SSL server IP/port conflict: china.example.com:443 (/etc/httpd/conf.d/china.conf:49) vs. hk.example.com:443 (/etc/httpd/conf.d/hk.conf:45)
[warn] Init: SSL server IP/port conflict: korea.tkeexample.com:443 (/etc/httpd/conf.d/korea.conf:45) vs. hk.example.com:443 (/etc/httpd/conf.d/hk.conf:45)
[warn] Init: You should not use name-based virtual hosts in conjunction with SSL!!
6: I know there are config mistake, but how can I find it, cause i
know there is not place to find out error from virtualhost
configuration.