HSSF Apache POI Java Excel

Tengo un codigo que basicamente me recorre una lista y lo mete en celdas utilizando la libreria de Apacha POI, pero se ve todo muy estrecho y no consigo darle un “padding” o algo para ensanchar celdas , dejo el codigo que tengo aqui. Gracias!

HSSFWorkbook wk = new HSSFWorkbook();   		Sheet dades = wk.createSheet(EEEE);    		for (??? dades : ????) { 			Row header = dades.createRow(0); 			header.createCell(0).setCellValue(bla); 			header.createCell(1).setCellValue(nla); 			header.createCell(2).setCellValue(bla);  			Row rda = dades.createRow(rowNumDad);  	rda.createCell(0).setCellValue(dades.dades.getBla()); 	rda.createCell(1).setCellValue(dades.dades.getBla()); 			rda.createCell(2).setCellValue(dades.getBla()); 			rowNumDad++;  		}

Ambiente Azure – VM Linux Centos 7 com Virtual Host no Apache [pendente]

Boa tarde. Configurei o Virtual Host no Apache no SO Linux Centos 7, fiz testes incluindo os ips e os nomes no arquivo host do windows, o broswer acessa os dois sistemas sem problema, agora estou tentando utilizar o DNS da Azure para resolver o nome, fiz testes com o comando nslookup com sucesso, mas o browser não encontra do ip, como posso resolver este problema?

Apache alias do not show images with https

I have created an alias to store product images on a different drive as follow:

ServerName multiplace.net.br ServerAlias www.multiplace.net.br ServerAdmin contato@multiplace.net.br DocumentRoot /var/www/html/multiplace/public_html

    Alias /produtos "/mnt/bloco2/multiplace/produtos"     <Directory "/mnt/bloco2/multiplace/produtos">         Options FollowSymLinks          AllowOverride all          Order allow,deny         Allow from all         Require all granted     </Directory>       ErrorLog /var/www/html/multiplace/logs/error.log     CustomLog /var/www/html/multiplace/logs/access.log combined 

I can see images with http but it returns page not found for https. Please check the example: OK http://www.multiplace.net.br/produtos/imagem.jpg Not found https://www.multiplace.net.br/produtos/imagem.jpg

Any suggestions? Many thanks.

Problema con Apache Tomcat y Eclipse

Estoy intentando levantar en el eclipse con el apache tomcat una aplicacion web pero estoy teniendo problemas… levanto el apache desde el eclipse y me entra al localhost:8080 y al /manager, pero cuando entro a localhost:8080/aplicacion me da error 404.

La aplicacion ya estaba hecha y nos la han pasado, nosotros estamos intentando configurarla para levantarla en local y no hay manera..

Os pongo todo lo que tengo.

JDK 1.8 Apache Tomcat 9

Creo el server tomcat 9 y le añado la aplicacion web, supongamos que el proyecto se llama “aplicacion”.

introducir la descripción de la imagen aquí

Ahora, modifico las propiedades del servidor: * Elijo la opcion de “use tomcat installation” para que me coja la ruta de donde está el tomcat (ahora mismo está en mi escritorio) * Desde la configuracion del tomcat, me voy a la pestaña “Modules” y cambiamos el web-modules de “aplicacion” a “aplicacion-web” (por seguir un ejemplo), esto es debido a que en el archivo aplicacion.xml, el nombre de la aplicacion es “”, por eso en web-modules hay que poner el mismo nombre.

introducir la descripción de la imagen aquí

introducir la descripción de la imagen aquí

Bien, ahora, compilo todo mediante el ANT.build, me compila correctamente y me genera el .WAR en la ruta donde está el workspace, tal que así /workspace/{name_project}/target, aquí está generado el .WAR

Vale, ahora inicio la aplicacion desde el botón verde (play) de tomcat/eclipse para arrancar la app… y aqui es donde tengo problemas.

Me tarda solo 5 segundos y no me cuadra, es como si no pillara el .war… una vez iniciado el tomcat, puedo entrar a localhost:8080 y a localhost:8080/manager. Si entro al manager, veo mi app “desplegada”, y si entro a la direccion localhost:8080/aplicacion-web/ es cuando me da el error 404

jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server version name:   Apache Tomcat/9.0.21 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server built:          Jun 4 2019 20:19:36 UTC jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Server version number: 9.0.21.0 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Name:               Windows 7 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: OS Version:            6.1 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Architecture:          amd64 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Java Home:             C:\opt\Java\jdk1.8.0_40\jre jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Version:           1.8.0_40-b26 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Vendor:            Oracle Corporation jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_BASE:         C:\Users\usuario\Desktop\apache-tomcat-9.0.21 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_HOME:         C:\Users\usuario\Desktop\apache-tomcat-9.0.21 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.base=C:\Users\usuario\Desktop\apache-tomcat-9.0.21 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.home=C:\Users\usuario\Desktop\apache-tomcat-9.0.21 jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dwtp.deploy=C:\Users\usuario\Desktop\apache-tomcat-9.0.21\wtpwebapps jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\usuario\Desktop\apache-tomcat-9.0.21\endorsed jul 10, 2019 9:48:36 AM org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dfile.encoding=Cp1252 jul 10, 2019 9:48:36 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: Loaded APR based Apache Tomcat Native library [1.2.21] using APR version [1.6.5]. jul 10, 2019 9:48:36 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. jul 10, 2019 9:48:36 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] jul 10, 2019 9:48:36 AM org.apache.catalina.core.AprLifecycleListener initializeSSL INFO: OpenSSL successfully initialized [OpenSSL 1.1.1a  20 Nov 2018] jul 10, 2019 9:48:37 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-nio-8080"] jul 10, 2019 9:48:37 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-nio-8009"] jul 10, 2019 9:48:37 AM org.apache.catalina.startup.Catalina load INFO: Server initialization in [1,283] milliseconds jul 10, 2019 9:48:37 AM org.apache.catalina.core.StandardService startInternal INFO: Starting service [Catalina] jul 10, 2019 9:48:37 AM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet engine: [Apache Tomcat/9.0.21] jul 10, 2019 9:48:38 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored. jul 10, 2019 9:48:38 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "10000" for "maxWait" property, which is being ignored. jul 10, 2019 9:48:38 AM org.apache.jasper.servlet.TldScanner scanJars INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. jul 10, 2019 9:48:39 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [128] milliseconds. jul 10, 2019 9:48:39 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\docs] jul 10, 2019 9:48:39 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored. jul 10, 2019 9:48:39 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "10000" for "maxWait" property, which is being ignored. jul 10, 2019 9:48:39 AM org.apache.jasper.servlet.TldScanner scanJars INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. jul 10, 2019 9:48:39 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deployment of web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\docs] has finished in [611] ms jul 10, 2019 9:48:39 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\examples] jul 10, 2019 9:48:40 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored. jul 10, 2019 9:48:40 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "10000" for "maxWait" property, which is being ignored. jul 10, 2019 9:48:40 AM org.apache.jasper.servlet.TldScanner scanJars INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. jul 10, 2019 9:48:40 AM org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() jul 10, 2019 9:48:40 AM org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized() jul 10, 2019 9:48:40 AM org.apache.catalina.core.ApplicationContext log INFO: ContextListener: attributeAdded('StockTicker', 'async.Stockticker@492521c4') jul 10, 2019 9:48:40 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deployment of web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\examples] has finished in [1,111] ms jul 10, 2019 9:48:40 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\host-manager] jul 10, 2019 9:48:41 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored. jul 10, 2019 9:48:41 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "10000" for "maxWait" property, which is being ignored. jul 10, 2019 9:48:41 AM org.apache.jasper.servlet.TldScanner scanJars INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. jul 10, 2019 9:48:41 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deployment of web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\host-manager] has finished in [609] ms jul 10, 2019 9:48:41 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\manager] jul 10, 2019 9:48:41 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored. jul 10, 2019 9:48:41 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "10000" for "maxWait" property, which is being ignored. jul 10, 2019 9:48:41 AM org.apache.jasper.servlet.TldScanner scanJars INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. jul 10, 2019 9:48:41 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deployment of web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\manager] has finished in [583] ms jul 10, 2019 9:48:41 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\ROOT] jul 10, 2019 9:48:42 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored. jul 10, 2019 9:48:42 AM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance WARNING: Name = aplicacion3DS Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "10000" for "maxWait" property, which is being ignored. jul 10, 2019 9:48:42 AM org.apache.jasper.servlet.TldScanner scanJars INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. jul 10, 2019 9:48:42 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deployment of web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\ROOT] has finished in [556] ms jul 10, 2019 9:48:42 AM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-nio-8080"] jul 10, 2019 9:48:42 AM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["ajp-nio-8009"] jul 10, 2019 9:48:42 AM org.apache.catalina.startup.Catalina start INFO: Server startup in [5,222] milliseconds 

introducir la descripción de la imagen aquí

introducir la descripción de la imagen aquí

Es como si el tomcat no me cogiera el .WAR… esta es la configuracion del servidor:

introducir la descripción de la imagen aquí

Lo que he visto es que en la propiedad ” -Dwtp.deploy” está puesta la carpeta “wtpwebapps” del tomcat y digo quizas está deployando lo que está en esa ruta.. pues he puesto ahí el .war (tanto el .war como el .war descomprimido) y me sigue sin ir.

Otra curiosidad es que si en la carpeta del apache tomcat, en la ruta ../apache-tomcat-9.0.21\webapps, descomprimo el .war pero con otro nombre (imaginemos aplicacion-web_2), cuando inicio el servidor desde el eclipse, si que coge ese war, ya que tarda 100 segundos en levantar y sale un comentario que pone “INFO: Deploying web application directory [C:\Users\usuario\Desktop\apache-tomcat-9.0.21\webapps\aplicacion-web-2]” y aqui si entro al manager del tomcat, si que me aparecen aplicacion-web y aplicacion-web-2, pero ninguna de las 2 entra (error 404 en las 2).

introducir la descripción de la imagen aquí

Es como si el tomcat del eclipse no me cogiera bien el war (ruta mal definida) pero si lo pongo en webapps si me coge ese war..

Indicar que cuando pongo el .war descomprimido en webapps de la carpeta del tomcat y tarda 100 segundos en levantar el server en eclipse, da un error de BBDD ya que la BBDD que tenemos en el .context no funciona, por tanto aunque la BBDD no funcione ¿no debería de entrar a la aplicacion pero no conectar con nada?, no debería dar el error 404 ¿no?

¿alguna idea?, no tiene sentido estar generando el .war y descomprimiendolo a mano en la carpeta del tomcat, ¿no? deberia cogerlo solo el servidor

Gracias!!

Apache mod_rewrite

Necesito ayuda con la reescritura de URLs de Apache, tengo dos apps con django en dos dominios. El primero es www.backendluis.com y el segundo www.luis.com. Quisiera poder mostrar el login del www.backendluis.com/login en el dominio www.luis.com. Tengo las siguiente reglas: RewriteEngine On RewriteCond %{HTTP_HOST} ^www.luis.com$ [NC] RewriteRule ^/(.+)$ http://www.backendluis.com/login/$ 1 [L] Sin embargo al entrar a www.luis.com me muestra los contenidos de http://www.backendluis.com y no los de http://www.backendluis.com/login Que hago mal? Luis

How To Set Up Apache Virtual Hosts on Ubuntu 16.04

In this tutorial, we shall start with an empty VPS screen of a freshly installed Ubuntu 16.04 and will finish up with three different sites sharing the same IP address.

Apache web server analyzes input from the browser and connects it to the directory structure inside VPS. The technical term for “sites” inside VPS boxes is “virtual host” — the server is the “host” many DNS addresses at the same time, hence, they are not real but only “virtual”.

Let’s see how it’s done.

What We Are Going To Cover

  • Creating a Non Root User
  • Installing Apache
  • Defining File Locations For the Default Apache Page
  • Creating File Structure For Our Demo Virtual Hosts
  • Granting Permissions
  • Creating index.html Pages For Our Demo Sites
  • Creating Virtual Hosts Files
  • Turning On the New Virtual Host Files
  • Testing the Virtual Hosts
  • Securing Your Domains With Let’s Encrypt SSL

Prerequisites

We use Ubuntu 16.04:

  • Starting with a clean VPS with
  • At least 512Mb of RAM and
  • 15Gb of free disk space.
  • You will need root user access via SSH
  • Two domain names pointed to your server’s IP address (it can also be a subdomain) using A records at your DNS service provider.

Step 1: Creating a Non Root User

Once you are logged in as root, you can create a new user account that you’ll use to access the app once it is installed. We’ll call the new user simpleuser. To create it, run:

adduser simpleuser 

Enter the password twice and press Enter five times to create a new user.

Then, add it to the sudo group, so that you can run commands as sudo:

usermod -aG sudo simpleuser 

Now you will be able to use the simpleuser as a root using the command sudo.

Step 2: Install Apache

First, update your package manager’s cache:

sudo apt update -y 

Install the Apache web server:

sudo apt install apache2 -y 

Enable its service to make it run on every system boot:

sudo systemctl enable apache2 

Finally, start it:

sudo systemctl start apache2 

To verify that Apache was installed successfully, access it from your local browser by navigating to http://YOUR_DOMAIN/. If that does not work, try adding :80 in the end, like this:

http://YOUR_DOMAIN:80 

You should see a welcome page for Apache, which means that you now have Apache running.

Apache is running

Step 3: File Locations For the Default Apache Page

What did just happen? How did Apache know to serve the page that you see above? Turns out that under Ubuntu, root document page is in directory /var/www/html and it is called index.html. You can see it after these commands:

cd /var/www/html ls -la 

Note that the directories and the index.html files belong to root user.

Execute this command

sudo nano index.html 

to see the contents of the index.html:

It is a very long page so we can scroll through it, change it and so on.

Under Ubuntu, all Apache files are in folder /etc/apache2:

cd /etc/apache2 ls -la 

Directory sites-available holds information on sites that exist, while sites-enabled holds information which of the available sites can be exposed to the Internet. File 000-default.conf in the image above is light blue in color, which tells us that it is a symbolic link to the file of same name in folder sites-available. The existence of one such link enables Apache to read the contents of file

/etc/apache2/sites-available/000-default.conf 

and serve it to the browser. By adding more config files into /etc/apache2/sites-available and creating the corresponding symbolic links in /etc/apache2/sites-enabled, we can serve different pages to the different site domains and that is how we can host multiple independent sites off of one and the same IP address.

Each site in Apache parlance is called “virtual host” and will have to reside in its own subdirectory. The plan for adding new virtual hosts boils down to

  • creating file structure for each site,
  • populating HTML and other files in the site,
  • creating a new .conf file in /etc/apache2/sites-available, and
  • creating a new symbolic link in /etc/apache2/sites-enabled.

Apache will then do the rest, automatically.

Step 4: Creating File Structure For Our Demo Virtual Hosts

We shall create two virtual hosts, let us call them demo1.com and demo2.com and they will correspond to DNS entries demo1.duskosavic.com and demo2.duskosavic.com that we have previously made using A records at our DNS service provider. (Instead of these, you should enter your own site domains and subdomains.) If we now entered http://demo1.duskosavic.com/ in the browser, the default index.html page with basic Apache information would be served, again.

Let us now create a directory within VPS to hold our demo1.com site. Ubuntu will hold its HTML files under /var/www/html folder but we are free to install our files elsewhere. We can, for example, install it into the root folder, like this:

cd ~ sudo mkdir /demo1.com 

and put all our data into it. The address of the directory would be /root/demo1.com. As long as we entered that folder address into the rest of the configuration files, everything would be OK.

However, we could also use /var/www/html/ folder to create our demo sites there. With commands such as

cd /var/www/html sudo mkdir demo1.com sudo mkdir demo2.com ls -la 

we would have the following directory structure:

Or, we can go one level up the directory tree and create the demo sites in /var/www. Inside folders demo1.com and demo2.com we can install whatever directory structure we need, for example:

sudo mkdir -p /var/www/demo1.com/{public_html,private,log,cgi-bin,backup} sudo mkdir -p /var/www/demo2.com/{public_html,private,log,cgi-bin,backup} ls demo1.com -la 

Step 5: Granting Permissions

From the image above we see that root user is still the owner of public_html folder from which our public files will be served to the Internet. Let us now change the ownership so that our simpleuser can access the public_html files. The commands are:

sudo chown -vR simpleuser:simpleuser /var/www/demo1.com/public_html sudo chown -vR simpleuser:simpleuser /var/www/demo2.com/public_html 

Also make sure that files in /var/www and its subfolders can be read correctly:

sudo chmod -R 755 /var/www 

Step 6: Create index.html Pages For Our Demo Sites

Our public files will be in /var/www/demo1.com/publichtml_ and /var/www/demo2.com/publichtml. Let us create an _index.html in each of these two folders so that we have something to see while browsing. Use nano or editor of your choice to create index.html for demo1.com:

sudo nano /var/www/demo1.com/public_html/index.html 

We need only one line of text, preferably in H1 format for better readability. Insert the following text into nano, save and close the file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>demo1.com Title</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> <!-- body,td,th {     color: #FF3333; } --> </style></head>  <body> <h1>This is demo1.com  </h1> </body> </html> 

Create index.html for demo2.com

sudo nano /var/www/demo2.com/public_html/index.html 

and paste this in:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>demo2.com Title</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> <!-- body,td,th {     color: #FF3333; } .style1 {color: #3333FF} --> </style></head> <body> <h1 class="style1">This is demo2.com</h1> </body> </html> 

Step 7: Create Virtual Hosts Files

Let us now inform Apache that there are two new sites to be served. We shall copy the default virtual host file, 000-default.conf twice and then alter these new files to reflect the positions of demo1.com and demo2.com sites on disk.

Step 7A: Create the First Virtual Hosts File

Copy the original and rename it demo1.com.conf:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/demo1.com.conf 

Through nano see what is in it:

sudo nano /etc/apache2/sites-available/demo1.com.conf 

For clarity, let us remove comments and this is what you should return back into nano:

<VirtualHost *:80>     ServerAdmin admin@demo1.com     ServerName demo1.com     ServerAlias demo1.duskosavic.com     ServerAlias www.demo1.com     DocumentRoot /var/www/demo1.com/public_html     ErrorLog $  {APACHE_LOG_DIR}/error.log     CustomLog $  {APACHE_LOG_DIR}/access.log combined </VirtualHost> 

Let us explain in detail:

  • 80 This virtual host will listen on port 80. You can change port number through file ports.conf with this command:
sudo nano /etc/apache2/ports.conf 

The contents of the ports.conf file are here in case you want to actually change them:

# If you just change the port or add more ports here, you will likely also # have to change the VirtualHost statement in # /etc/apache2/sites-enabled/000-default.conf  Listen 80  <IfModule ssl_module>         Listen 443 </IfModule>  <IfModule mod_gnutls.c>         Listen 443 </IfModule>  # vim: syntax=apache ts=4 sw=4 sts=4 sr noet 
  • ServerAdmin is the email address to which Apache will send messages for administrator in case of an error in the system.
  • ServerName Server name, obviously, it should coincide with the domain name.
  • ServerAlias Another name for the same server as above. You can have as many of these aliases as you like.
  • DocumentRoot Points to the absolute address of the site on disk.
  • ErrorLog The address of the error log.
  • CustomLog The same for access logs — for example, have there been too many unauthorized accesses from the same IP address?

Step 7B: Create the Second Virtual Hosts File

Do the same for the other site / domain. Here are the commands:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/demo2.com.conf 

Again use nano see what is in it:

sudo nano /etc/apache2/sites-available/demo2.com.conf 

This is what you should return back into nano:

<VirtualHost *:80>     ServerAdmin admin@demo2.com     ServerName demo2.duskosavic.com     ServerAlias www.demo2.com     DocumentRoot /var/www/demo2.com/public_html     ErrorLog $  {APACHE_LOG_DIR}/error.log     CustomLog $  {APACHE_LOG_DIR}/access.log combined </VirtualHost> 

Save and close both files.

Step 8: Turn On the New Virtual Host Files

Command a2ensite will create symbolic links from sites-enabled directory to sites-available and so Apache will routinely start serving the virtual hosts. The command are:

sudo a2ensite demo1.com.conf sudo a2ensite demo2.com.conf 

If you want to disable the default site, the command would be:

sudo a2dissite 000-default.conf 

You may want to leave it as it is in case that someone enters the IP address directly into the browser — then the default html code will be activated and the visitor would see what you have prepared for that case.

Once you use the a2ensite and a2dissite commands, you may see something like this in the terminal window:

If you try to activate a link that is already activated, you will be notified as already enabled. And then you will see two lines that say:

To activate new configuration, you need to run:   service apache2 reload 

That is, however, an incorrect advice to follow. The proper command is:

sudo service apache2 reload 

that is, run it as a sudo.

Also be sure to clear cache in your browser or it may start fooling you with old values instead of the properly refreshed ones.

Step 9: Testing the Virtual Hosts

If you have left the default IP address activated, it will currently show the welcome to Apache screen. If you enter address demo1.duskosavic.com you will see this:

And entering demo2.duskosavic.com into the browser changes the image to:

Step 10: Securing Your Domains With Let’s Encrypt SSL

We can use free certificates to turn our HTTP traffic into HTTPS traffic, which will make access to the site more secure. Use this link to obtain free Let’s Encrypt TLS certificates, from the official Cerbot site.

Execute the following set of commands:

sudo apt-get update sudo apt-get install software-properties-common sudo add-apt-repository universe sudo add-apt-repository ppa:certbot/certbot sudo apt-get update 

Now let us install Certbot:

sudo apt-get install certbot python-certbot-apache  

Run this command to get a certificate and have Certbot edit your Apache configuration automatically to serve it, turning on HTTPS access in a single step.

sudo certbot --apache 

It will ask for your email address in case of emergency, then another two questions that you may answer however you like and then the most important question — which names would you like to activate HTTPS for?

I chose 4 and 5 as these are the only real domains and subdomains that I have DNS addresses for:

  • demo1.duskosavic.com
  • demo2.duskosavic.com

The last question will be whether do you want HTTPS access or not. You, of course, do so choose 2. This finishes the installation of CertBot so let us now try it out:

We have entered the HTTP address and the site automatically turns it into a HTTPS address:

What Can You Do Next

We have shown how to share one IP address to one, two, three or dozens or hundreds of independent sites. You can now use this knowledge to host all your sites on one low cost VPS box.

Dusko Savic is a technical writer and programmer.

duskosavic.com

The post How To Set Up Apache Virtual Hosts on Ubuntu 16.04 appeared first on Low End Box.

Environment variable in whm managed apache virtual host

I have an EC2 with CentOS, cpanel and whm installed.

I have multiple accounts, linked with different subdomains. I have same php application deployed in the virtual hosts.

I need to use environment variables, with different values in for different virtual hosts.

For testing, I created a php file with

<?php     echo getenv("MYVAR"); ?> 

So far, I have tried setting the environment variables in

  1. /etc/apache2/conf.d/userdata/std/2_4/user/sub.domain.com/env.conf
  2. httpd.conf even though I’m not supposed to
  3. .htaccess because I was desperate

I enabled mod_env from EasyApache from whm.
apachectl -M shows env_module (shared) though I’m not sure if its the same thing.

My userdata file just contained SetEnv MYVAR myvalue, I could not find a single example whether this was right or wrong.

After that, I did /scripts/ensure_vhost_includes --all-users and it added the include statement in httpd.conf. I restarted httpd service, it didnt help.

I even tried /scripts/rebuildhttpdconf, it didnt help.