Difference between revisions of "Apache 2"

 
(21 intermediate revisions by the same user not shown)
Line 23: Line 23:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install apache2 apache2-mpm-prefork apache2-utils ssl-cert
+
apt install apache2 apache2-utils  
</syntaxhighlight>
+
apt install ssl-cert
 
 
 
 
===Additional libraries===
 
 
 
<syntaxhighlight lang="bash">
 
apt-get install libapache2-mod-fcgid libruby
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
Since Ubuntu 16.04 <code>apache2-mpm-prefork</code> is not required
  
 
===Doc===  
 
===Doc===  
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install apache2-doc
+
apt install apache2-doc
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 63: Line 58:
  
  
 +
==PHP 8==
 +
2021-11: PHP 8 is not included in Ubuntu 20.04 LTS.
  
==PHP 5==
+
Source article: http://www.daxiongmao.eu/wiki/index.php?title=Apache_2&action=edit
 
 
  
===Core===
+
===Add PHP 8.0 repository===
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install libapache2-mod-php5 php5 php5-common
+
apt install software-properties-common
 +
add-apt-repository ppa:ondrej/php
 +
apt update
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
===Install core packages===
  
===Modules PHP5===
+
To install the latest version of PHP:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install php5-cli php5-cgi
+
# PHP core
apt-get install php5-curl php5-xmlrpc php5-xsl php5-dev php-pear
+
apt-get install php
apt-get install php5-mysql
+
apt-get install php-cli
apt-get install php5-memcache php5-xcache
+
# Apache2 support
apt-get install php5-mhash php-auth php5-mcrypt mcrypt
+
apt install libapache2-mod-php
apt-get install php5-imap
 
apt-get install php5-snmp
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
===Image Magick===
+
===Modules PHP===
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install php5-gd php5-imagick imagemagick
+
apt-get install php-cgi
 +
#apt-get install php-opcache
 +
apt-get install php-gd  
 +
apt-get install php-bz2
 +
apt-get install php-curl
 +
apt-get install php-xmlrpc
 +
apt-get install php-json
 +
apt-get install php-mysql
 +
apt-get install php-imap
 +
apt-get install php-mbstring
 +
# Performances
 +
apt install php-fpm libapache2-mod-fcgid
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
Enable modules
===Configuration===
 
 
 
Edit PHP config file:
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
vim /etc/php5/apache2/php.ini
+
sudo a2enmod proxy_fcgi setenvif
 +
sudo a2enconf php8.0-fpm
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Add / uncomment the following lines in Dynamic extensions area (~ line 865)
+
===Utility===
* extension=mysql.so
 
* extension=gd.so
 
 
 
 
 
!! Note this is NOT required on Ubuntu 14.04 because these modules are enabled by default !!
 
 
 
 
 
 
 
==Firewall==
 
 
 
You have to open the following ports:
 
* Port 80 = HTTP
 
* Port 443 = HTTPS
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$IPTABLES -A INPUT -p tcp -m state -i eth0 --dport 80 -j ACCEPT
+
apt install php-pear
$IPTABLES -A INPUT -p tcp -m state -i eth0 --dport 443 -j ACCEPT
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Restart the firewall
+
===Configuration===
 
 
<syntaxhighlight lang="bash">
 
/etc/init.d/firewall restart
 
</syntaxhighlight>
 
  
 
+
Edit '''PHP config''' file:
 
 
==Test your installation==
 
 
 
 
 
Restart the Apache2 server
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
service apache2 restart
+
vim /etc/php/8.0/cli/php.ini
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
* Let CGI behaves like before: set <code>cgi.fix_pathinfo=1</code>
 +
* Adjust file upload size <code>upload_max_filesize = 32M</code>
 +
* Adjust post size <code>post_max_size = 32M</code>
 +
* Adjust time zone <code>date.timezone = Europe/Paris</code>
 +
* Save path: <code>session.save_path = "/tmp"</code>
  
 +
===Check PHP version and configuration===
  
You can now test your installation by going to 'http://localhost' or 'http://myServer'. You should see the default page.
+
To ensure PHP 8.0 is well-installed just type:  
 
 
 
 
 
 
 
 
 
 
 
 
=HTTP Virtual host=
 
 
 
 
 
==Preparation==
 
 
 
Initialize configuration
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
cd /etc/apache2/sites-available/
+
php -v
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
Create target directory
+
===Image Magick===
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
mkdir -p /var/www/myServer
+
apt install php-gd php-imagick imagemagick
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
===Configuration===
  
Prepare the log files
+
Edit PHP config file:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
mkdir -p /var/log/apache2/myServer
+
vim /etc/php/8.0/apache2/php.ini
touch /var/log/apache2/myServer/access.log
 
touch /var/log/apache2/myServer/error.log
 
chmod -R 660 /var/log/apache2/myServer/*
 
chown -R www-data:www-data /var/log/apache2/myServer/*
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
Add / uncomment the following lines in Dynamic extensions area
Copy default index file
+
<syntaxhighlight lang="php">
 
+
// PHP 8  (~ line 904)
<syntaxhighlight lang="bash">
+
extension=bz2
cp /var/www/html/index.html /var/www/myServer
+
extension=curl
chown -R www-data:www-data /var/log/apache2/myServer/*
+
extension=gd
 +
extension=imap
 +
extension=mysqli
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
 +
!! Note this is NOT required on Ubuntu 20.04 because these modules are enabled by default !!
  
==Configuration==
+
==Firewall==
  
Init configuration
+
see [[Firewall INPUT filters#Web server]]
 
 
<syntaxhighlight lang="bash">
 
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myServer.conf
 
</syntaxhighlight>
 
  
 
+
Restart the firewall
'''Edit configuration'''
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
vim /etc/apache2/sites-available/myServer
+
/etc/init.d/firewall restart
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
To begin the virtual host, write the following lines:
 
* Adjust the settings to your own configuration
 
  
<syntaxhighlight lang="bash">
+
==Test your installation==
<VirtualHost 192.168.0.100:80>   → Choose the best options for your needs
 
<VirtualHost *:80>
 
  
#############################
 
        # Server main properties
 
#############################
 
  
ServerName myServer
+
Restart the Apache2 server
ServerAlias www.myServer *.myServer
 
ServerAdmin webmaster@domain
 
 
# Logs settings
 
LogLevel Warn
 
CustomLog ${APACHE_LOG_DIR}/myServer/access.log combined
 
ErrorLog ${APACHE_LOG_DIR}/myServer/error.log
 
 
 
 
 
#############################
 
        # Root folder properties
 
#############################
 
DocumentRoot /var/www/myServer
 
 
 
        # SECURITY: forbid access to .htaccess so no outsider can ever change it
 
        <Files ~ "^\.ht">
 
                ## Old Apache2 (before 2.4) syntax
 
                Order allow,deny
 
                deny from all
 
 
 
                ## Apache 2.4 syntax
 
                Require all denied
 
        </Files>
 
        # Restrict access to server root
 
        <Directory />
 
                Options FollowSymLinks
 
                AllowOverride None
 
                Require all denied
 
        </Directory>
 
 
 
 
 
        # Virtual host root directory
 
<Directory /var/www/myServer>
 
Options Indexes FollowSymLinks MultiViews
 
AllowOverride None
 
 
 
                ## Old Apache2 (before 2.4) syntax
 
Order allow,deny
 
allow from all
 
               
 
                ## Apache 2.4
 
                Require all granted 
 
</Directory>
 
 
 
 
 
#############################
 
        # Other configuration
 
        # Alias, proxy redirections, CGI scripts, Directory, etc.
 
#############################
 
 
 
 
 
 
 
</VirtualHost>
 
</syntaxhighlight>
 
 
 
 
 
 
 
==Enable / disable virtual host(s)==
 
 
 
 
 
'''Virtual Host desactivation'''
 
 
 
If you're listening on '''*:80''' then you should probably disable the default virtual host before enabling yours!
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
a2dissite 000-default
+
service apache2 restart
</syntaxhighlight>
 
 
 
 
 
 
 
 
 
'''Virtual Host activation'''
 
 
 
To activate a Virtual Host, just type
 
 
 
<syntaxhighlight lang="bash">
 
a2ensite  myServer
 
</syntaxhighlight>
 
 
 
Then, restart your web server
 
 
 
<syntaxhighlight lang="bash">
 
/etc/init.d/apache2 restart
 
</syntaxhighlight>
 
 
 
 
 
Check your server! You should see your "index.html" page.
 
 
 
 
 
 
 
=HTTPS (SSL) Virtual host=
 
 
 
 
 
==Create SSL certificate==
 
 
 
First of all, you need to create a server certificate.
 
Cf. SSL dedicated document → Create a new server certificate
 
 
 
>> see [[SSL server]]
 
 
 
 
 
 
 
==Enable SSL module==
 
 
 
You have to either copy or create symlinks for server certificate.
 
 
 
To avoid rights collision I'm using a ''copy'' operation. However I know from past experience that ''symLinks'' work very well if you set the correct rights.
 
 
 
 
 
-Note-
 
 
 
You MUST use the NON-ENCRYPTED private key if you want to start Apache2 automatically on each reboot.
 
 
 
 
 
 
 
'''Copy certificates'''
 
 
 
<syntaxhighlight lang="bash">
 
cp /srv/ssl/certs/myServer.cert.pem /etc/apache2/webServer.pem
 
cp /srv/ssl/private/myServer.nopass.key /etc/apache2/webServer.key
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
 
+
Create a simple PHP script
Alternative: '''Symlinks to /srv/ssl/'''
 
 
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
ln -s /srv/ssl/certs/myServer.cert.pem /etc/apache2/webServer.pem
+
vim /var/www/html/phpinfo.php
ln -s /srv/ssl/private/myServer.nopass.key /etc/apache2/webServer.key
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
Put the following:
 
+
<syntaxhighlight lang="php">
'''Activate the SSL module'''
+
<?php
 
+
phpinfo();
<syntaxhighlight lang="bash">
+
?>
a2enmod ssl
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
Adjust rights
 
 
==Prepare virtual host (optional)==
 
 
 
Create virtual host folder
 
 
 
<syntaxhighlight lang="bash">
 
mkdir -p /var/www/myServer-ssl
 
cp /var/www/index.html /var/www/myServer-ssl
 
chown -R www-data:www-data /var/www/myServer-ssl
 
</syntaxhighlight>
 
 
 
 
 
 
 
==Prepare the log files (optional)==
 
 
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
# That should already exists from before
+
chown www-data:www-data /var/www/html/phpinfo.php
mkdir -p /var/log/apache2/myServer
+
chmod 755 /var/www/html/phpinfo.php
 
 
# Create *-ssl.log
 
touch /var/log/apache2/myServer/error-ssl.log
 
touch /var/log/apache2/myServer/access-ssl.log
 
chmod -R 660 /var/log/apache2/myServer/*
 
chown -R www-data:www-data /var/log/apache2/myServer/*
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
 
+
You can now test your installation by going to 'http://localhost/phpinfo.php' or 'http://myServer/phpinfo.php'. You should see the default page.
Create a default "/var/www/myServer-ssl/index.html" to check your virtual host.
 
 
 
If you'd like you can use this ultra-simple file [http://daxiongmao.eu/wiki_upload_files/apache2/index.html]
 
 
 
<syntaxhighlight lang="bash">
 
cd /var/www/myServer-ssl/
 
wget http://daxiongmao.eu/wiki_upload_files/apache2/index.html
 
chown www-data:www-data index.html
 
</syntaxhighlight>
 
 
 
 
 
 
 
 
 
==Virtual host declaration==
 
 
 
You have 2 possibilities:
 
* Update your current virtual host (recommended)
 
* Create a new one, only for the SSL virtual host
 
 
 
 
 
'''Update non-ssl V.Host configuration'''
 
 
 
<syntaxhighlight lang="bash">
 
vim /etc/apache2/sites-available/myServer
 
</syntaxhighlight>
 
 
 
 
 
!! Adjust the settings to your own configuration !!
 
 
 
<syntaxhighlight lang="bash">
 
# Secure web server
 
<VirtualHost _default_:443>
 
<VirtualHost 192.168.0.100:443>   → Choose the best options for your needs
 
<VirtualHost *:443>
 
 
 
#############################
 
        # Server main properties
 
#############################
 
 
 
ServerName myServer
 
ServerAlias www.myServer *.myServer
 
ServerAdmin webmaster@domain
 
 
# Logs settings
 
LogLevel Warn
 
CustomLog ${APACHE_LOG_DIR}/myServer/access-ssl.log combined
 
ErrorLog ${APACHE_LOG_DIR}/myServer/error-ssl.log
 
 
 
        # Enable SSL
 
        SSLEngine              On
 
        SSLCertificateFile      /etc/apache2/webServer.pem
 
        SSLCertificateKeyFile  /etc/apache2/webServer.key
 
 
 
#############################
 
        # Root folder properties
 
#############################
 
DocumentRoot /var/www/myServer-ssl
 
 
 
 
 
        # SECURITY: forbid access to .htaccess so no outsider can ever change it
 
        <Files ~ "^\.ht">
 
                ## Old Apache2 (before 2.4) syntax
 
                Order allow,deny
 
                deny from all
 
 
 
                ## Apache 2.4 syntax
 
                Require all denied
 
        </Files>
 
 
 
        # Restrict access to server root
 
        <Directory />
 
                Options FollowSymLinks
 
                AllowOverride None
 
                Require all denied
 
        </Directory>
 
 
 
        # Virtual host root directory
 
<Directory /var/www/myServer-ssl>
 
                Require all granted
 
Options Indexes FollowSymLinks MultiViews
 
AllowOverride None
 
 
                ## Old Apache2 (before 2.4) syntax
 
Order allow,deny
 
allow from all
 
               
 
                ## Apache 2.4
 
                Require all granted 
 
</Directory>
 
 
 
 
 
#############################
 
        # Other configuration
 
        # Alias, proxy redirections, CGI scripts, Directory, etc.
 
#############################
 
 
 
Alias /phpsec  /var/somewhere/phpsecinfo
 
<Location /phpsec >
 
                ## Old apache 2 (before 2.4)
 
order deny,allow
 
allow from all
 
Allow from 127.0.0.1 192.168.1.0/24
 
 
 
                ## Apache 2.4
 
require local
 
require ip 192.168.1
 
                require host dev.daxiongmao.eu
 
        </Location>
 
</VirtualHost>
 
</syntaxhighlight>
 
 
 
 
 
Restart the web server
 
 
 
<syntaxhighlight lang="bash">
 
service apache2 restart
 
</syntaxhighlight>
 
 
 
 
 
Now you can test your server ''https://myServer''
 
 
 
 
 
If you've use a self-signed certificate you might see some alert. Just discarded it and process anyway!
 
 
 
 
 
 
 
 
 
 
 
 
 
=Related topics=
 
 
 
 
 
==Distribute and install the certificates==
 
 
 
Some guides to setup specific application and features:
 
 
 
* [[Apache 2 - Redirection / proxy|Apache 2- Redirection & rewrite]]
 
 
 
* [[Apache 2 - proxy]]
 
 
 
* [[Apache 2 - Security]]
 
 
 
* [[Apache 2 - Performances]]
 
 
 
* [[Apache 2 - SSL certificates page]]
 
 
 
* [[Apache 2 - LDAP access]]
 

Latest revision as of 16:38, 3 November 2021



Requirements

Before going through this tutorial, I recommend you to setup:



Installation

Apache 2

This will install web server + PHP + Perl + all required libraries.

Apache2 core

apt install apache2 apache2-utils 
apt install ssl-cert

Since Ubuntu 16.04 apache2-mpm-prefork is not required

Doc

apt install apache2-doc


Perl

apt-get install libapache2-mod-perl2 libapache2-mod-perl2-doc


SNMP

Sometimes you might encounter some SNMP errors on latest Debian based distributions.

In that case you have to install a new package and run it.

apt-get install snmp-mibs-downloader
download-mibs


source: http://www.podciborski.co.uk/miscellaneous/snmp-cannot-find-module/


PHP 8

2021-11: PHP 8 is not included in Ubuntu 20.04 LTS.

Source article: http://www.daxiongmao.eu/wiki/index.php?title=Apache_2&action=edit

Add PHP 8.0 repository

apt install software-properties-common
add-apt-repository ppa:ondrej/php
apt update

Install core packages

To install the latest version of PHP:

# PHP core
apt-get install php
apt-get install php-cli
# Apache2 support
apt install libapache2-mod-php


Modules PHP

apt-get install php-cgi 
#apt-get install php-opcache
apt-get install php-gd 
apt-get install php-bz2 
apt-get install php-curl 
apt-get install php-xmlrpc
apt-get install php-json 
apt-get install php-mysql 
apt-get install php-imap 
apt-get install php-mbstring
# Performances
apt install php-fpm libapache2-mod-fcgid

Enable modules

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.0-fpm

Utility

apt install php-pear

Configuration

Edit PHP config file:

vim /etc/php/8.0/cli/php.ini
  • Let CGI behaves like before: set cgi.fix_pathinfo=1
  • Adjust file upload size upload_max_filesize = 32M
  • Adjust post size post_max_size = 32M
  • Adjust time zone date.timezone = Europe/Paris
  • Save path: session.save_path = "/tmp"

Check PHP version and configuration

To ensure PHP 8.0 is well-installed just type:

php -v


Image Magick

apt install php-gd php-imagick imagemagick

Configuration

Edit PHP config file:

vim /etc/php/8.0/apache2/php.ini

Add / uncomment the following lines in Dynamic extensions area

// PHP 8  (~ line 904)
extension=bz2
extension=curl
extension=gd
extension=imap
extension=mysqli


!! Note this is NOT required on Ubuntu 20.04 because these modules are enabled by default !!

Firewall

see Firewall INPUT filters#Web server

Restart the firewall

/etc/init.d/firewall restart


Test your installation

Restart the Apache2 server

service apache2 restart


Create a simple PHP script

vim /var/www/html/phpinfo.php

Put the following:

<?php
phpinfo();
?>

Adjust rights

chown www-data:www-data /var/www/html/phpinfo.php
chmod 755 /var/www/html/phpinfo.php


You can now test your installation by going to 'http://localhost/phpinfo.php' or 'http://myServer/phpinfo.php'. You should see the default page.