Added Apache config classes
This commit is contained in:
parent
2d9aefd079
commit
ba3c548232
5 changed files with 168 additions and 75 deletions
115
src/wa/server/plugin/apache/ApacheAbstractConfig.java
Executable file
115
src/wa/server/plugin/apache/ApacheAbstractConfig.java
Executable file
|
|
@ -0,0 +1,115 @@
|
|||
package wa.server.plugin.apache;
|
||||
|
||||
import wa.server.WAConstants;
|
||||
import wa.server.plugin.WAConfiguration;
|
||||
import zutil.db.bean.DBBean.DBTable;
|
||||
import zutil.ui.Configurator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
|
||||
public abstract class ApacheAbstractConfig extends WAConfiguration {
|
||||
private static final String CONFIG_NAME = "vhost";
|
||||
private static final String NAVIGATION_NAME = "Apache Virtual Host";
|
||||
|
||||
private static final String SITE_AVAILABLE_DIR = "/apache2/site-available/";
|
||||
private static final String SITE_ENABLED_DIR = "/apache2/site-enabled/";
|
||||
|
||||
|
||||
@Configurator.Configurable("Domain")
|
||||
protected String domain;
|
||||
protected transient String domain_old;
|
||||
@Configurator.Configurable("WWW Redirect")
|
||||
protected boolean wwwRedirect;
|
||||
@Configurator.Configurable("SSL")
|
||||
protected boolean ssl;
|
||||
|
||||
|
||||
|
||||
public String getDomain() {
|
||||
return domain;
|
||||
}
|
||||
public void setDomain(String domain) {
|
||||
this.domain = domain;
|
||||
}
|
||||
public boolean wwwRedirectEnabled() {
|
||||
return wwwRedirect;
|
||||
}
|
||||
public void setWWWRedirect(boolean redirect) {
|
||||
this.wwwRedirect = redirect;
|
||||
}
|
||||
public boolean sslEnabk() {
|
||||
return ssl;
|
||||
}
|
||||
public void setSSL(boolean ssl) {
|
||||
this.ssl = ssl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void saveConfiguration() throws IOException {
|
||||
if(domain != domain_old){ // Configuration file has changed name
|
||||
WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain_old).delete();
|
||||
domain_old = domain;
|
||||
}
|
||||
PrintStream out = new PrintStream(
|
||||
WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain));
|
||||
|
||||
saveConfiguration(out);
|
||||
out.close();
|
||||
}
|
||||
|
||||
private void saveConfiguration(PrintStream out){
|
||||
if (wwwRedirect){
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName www."+ domain);
|
||||
out.println();
|
||||
out.println(" RewriteEngine on");
|
||||
out.println(" RewriteCond %{HTTP_HOST} ^www\\..*$ [NC]");
|
||||
out.println(" RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]"); // 301: permanent, 302: temporary
|
||||
out.println("</VirtualHost>");
|
||||
out.println();
|
||||
}
|
||||
|
||||
if (ssl){
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName "+ domain);
|
||||
out.println();
|
||||
out.println(" RewriteEngine On");
|
||||
out.println(" RewriteCond %{SERVER_PORT} !^443$");
|
||||
out.println(" RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]");
|
||||
out.println("</VirtualHost>");
|
||||
out.println("<VirtualHost *:443>");
|
||||
out.println(" ServerName "+ domain +":443");
|
||||
out.println();
|
||||
apacheConfiguration(out);
|
||||
out.println();
|
||||
out.println(" ServerAlias "+domain);
|
||||
out.println(" Include /etc/letsencrypt/options-ssl-apache.conf");
|
||||
out.println(" SSLCertificateFile /etc/letsencrypt/live/"+domain+"/cert.pem");
|
||||
out.println(" SSLCertificateKeyFile /etc/letsencrypt/live/"+domain+"/privkey.pem");
|
||||
out.println(" SSLCertificateChainFile /etc/letsencrypt/live/"+domain+"/chain.pem");
|
||||
out.println("</VirtualHost>");
|
||||
out.println("");
|
||||
}
|
||||
else {
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName "+ domain);
|
||||
out.println();
|
||||
apacheConfiguration(out);
|
||||
out.println("</VirtualHost>");
|
||||
out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void apacheConfiguration(PrintStream out);
|
||||
|
||||
|
||||
@Override
|
||||
public void deleteConfiguration() throws Exception {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue