This is an old revision of the document!
Table of Contents
Ubuntu 24.04 LTS + Apache2 + PHP installarion and setting
| OS | Ubuntu 24.04.2 LTS |
|---|---|
| apache2 | 2.4.58-1ubuntu8.5 |
| PHP | PHP 8.3.6 |
The PHP and the apache2 version may have changed since this article was written.
Installing Apache and Updating the Firewall
$ sudo apt update $ sudo apt install apache2
$ sudo ufw app list $ sudo ufw allow in "Apache" $ sudo ufw status
Ufw setting
OpenSSH
$ cat /etc/ufw/applications.d/openssh-server [OpenSSH] title=Secure shell server, an rshd replacement description=OpenSSH is a free implementation of the Secure Shell protocol. ports=22/tcp
$ sudo ufw allow 'OpenSSH'
$ sudo ufw enable Firewall is active and enabled on system startup $ sudo ufw status Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6)
If you do not want the sshd to be accessible from outside of your local network, you can do the following:
$ sudo ufw delete allow 'OpenSSH' Rule deleted Rule deleted (v6) $ sudo ufw status Status: active
$ sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp comment 'Allow ssh from local' Rule added $ sudo ufw status Status: active To Action From -- ------ ---- 22/tcp ALLOW 192.168.1.0/24 # Allow ssh from local
Apache2
$ cat /etc/ufw/applications.d/apache2-utils.ufw.profile [Apache] title=Web Server description=Apache v2 is the next generation of the omnipresent Apache web server. ports=80/tcp [Apache Secure] title=Web Server (HTTPS) description=Apache v2 is the next generation of the omnipresent Apache web server. ports=443/tcp [Apache Full] title=Web Server (HTTP,HTTPS) description=Apache v2 is the next generation of the omnipresent Apache web server. ports=80,443/tcp
So, you should do one of these three commands.
$ sudo ufw app info 'Apache' $ sudo ufw app info 'Apache Secure' $ sudo ufw app info 'Apache Full'
Same as the sshd, if you do not want the apache2 to be accessible from outside of your local network, you can do the following:
$ sudo ufw delete allow 'Apache' $ sudo ufw delete allow 'Apache Secure' $ sudo ufw delete allow 'Apache Full'
$ sudo ufw delete allow 'Apache' $ sudo ufw delete allow 'Apache Secure' $ sudo ufw delete allow 'Apache Full'
$ sudo ufw allow from 192.168.1.0/24 to any port 80 proto tcp comment 'Allow http from local' $ sudo ufw allow from 192.168.1.0/24 to any port 443 proto tcp comment 'Allow https from local'
enable ufw
$ sudo ufw enable Firewall is active and enabled on system startup
Set to start ufw on boot
$ git diff /etc/ufw/ufw.conf diff --git a/etc/ufw/ufw.conf b/etc/ufw/ufw.conf index 8336b91..28fe534 100644 --- a/etc/ufw/ufw.conf +++ b/etc/ufw/ufw.conf @@ -3,7 +3,7 @@ # Set to yes to start on boot. If setting this remotely, be sure to add a rule # to allow your remote connection before starting ufw. Eg: 'ufw allow 22/tcp' -ENABLED=no +ENABLED=yes # Please use the 'ufw' command to set the loglevel. Eg: 'ufw logging medium'. # See 'man ufw' for details.
http://localhost
The default Ubuntu 24.04 Apache web page is there for informational and testing purposes.
Installing PHP
$ sudo apt-get install php libapache2-mod-php
$ php --version PHP 8.3.6 (cli) (built: Dec 2 2024 12:36:18) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.6, Copyright (c) Zend Technologies with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
$ apt list 2>/dev/null |grep -e "^php.*installed" php-common/noble,now 2:93ubuntu2 all [installed,automatic] php8.3-cli/noble-updates,noble-security,now 8.3.6-0ubuntu0.24.04.3 amd64 [installed,automatic] php8.3-common/noble-updates,noble-security,now 8.3.6-0ubuntu0.24.04.3 amd64 [installed,automatic] php8.3-opcache/noble-updates,noble-security,now 8.3.6-0ubuntu0.24.04.3 amd64 [installed,automatic] php8.3-readline/noble-updates,noble-security,now 8.3.6-0ubuntu0.24.04.3 amd64 [installed,automatic] php8.3/noble-updates,noble-security,now 8.3.6-0ubuntu0.24.04.3 all [installed,automatic] php/noble,now 2:8.3+93ubuntu2 all [installed]
$ sudo apt-get install php-xml # (for Dokuwiki)
Create user wweb
You can change the user name whatever you want.
$ sudo adduser wweb $ sudo usermod -aG sudo wweb $ sudo usermod -aG wweb pctr # replace pctr with your main account's group name
Creating a Virtual Host for your Website
$ sudo -p mkdir /home/wweb/www/html # instead of (sudo mkdir -p /var/www/your_domain)
$ sudo chown -R wweb:wweb /home/wweb/www/html # instead of (sudo chown -R $USER:$USER /var/www/your_domain)
$ sudo vim /etc/apache2/sites-available/wweb.conf # instead of (sudo vim /etc/apache2/sites-available/your_domain.conf)
/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80> ServerName your_domain ServerAlias www.your_domain ServerAdmin webmaster@localhost DocumentRoot /var/www/your_domain ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
ServerAdmin Directive in Apache2 is deprecated.
ServerName Directive
Syntax: ServerName [scheme://]domain-name|ip-address[:port] ex) ServerName www.example.com
Or try this.
$ cat /etc/hostname ubuntupctr
Then
ServerName ubuntupctr
is enough.
/etc/apache2/sites-available/wweb.conf
<VirtualHost *:80> ServerName ubuntupctr ServerAdmin webmaster@localhost #DocumentRoot /var/www/html DocumentRoot /home/wweb/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
To set the 'ServerName' directive globally in Apache, edit the /etc/apache2/apache2.conf file and add the line ServerName your.server.ip.address or ServerName your.domain.name under the global configuration section. After saving the changes, restart Apache using sudo systemctl restart apache2 to apply the configuration.
So, Add a line containing "ex) ServerName 127.0.0.1" to the end of the file:
/etc/apache2/apache2.conf
End of file:
ServerName ubuntupctr
/etc/apache2/envvars
$ cgit diff etc/apache2/envvars diff --git a/etc/apache2/envvars b/etc/apache2/envvars index 708d170..a863276 100644 --- a/etc/apache2/envvars +++ b/etc/apache2/envvars @@ -13,8 +13,8 @@ fi # Since there is no sane way to get the parsed apache2 config in scripts, some # settings are defined via environment variables and then used in apache2ctl, # /etc/init.d/apache2, /etc/logrotate.d/apache2, etc. -export APACHE_RUN_USER=www-data -export APACHE_RUN_GROUP=www-data +export APACHE_RUN_USER=wweb +export APACHE_RUN_GROUP=wweb # temporary state file location. This might be changed to /run in Wheezy+1 export APACHE_PID_FILE=/var/run/apache2$SUFFIX/apache2.pid export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
enable your virtual host sites:
$ sudo a2ensite wweb.conf
Enabling site wweb.
To activate the new configuration, you need to run:
systemctl reload apache2
disable the default site defined in 000-default.conf
$ sudo a2dissite 000-default.conf
Site 000-default disabled.
To activate the new configuration, you need to run:
systemctl reload apache2
Look at the current list of virtual hosts:
$ apache2ctl -S VirtualHost configuration: *:80 win11kan (/etc/apache2/sites-enabled/wweb.conf:1) ServerRoot: "/etc/apache2" Main DocumentRoot: "/var/www/html" Main ErrorLog: "/var/log/apache2/error.log" Mutex watchdog-callback: using_defaults Mutex default: dir="/var/run/apache2/" mechanism=default Mutex mpm-accept: using_defaults PidFile: "/var/run/apache2/apache2.pid" Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name="wweb" id=1001 not_used Group: name="wweb" id=1001 not_used
DirectoryIndex on Apache
/etc/apache2/mods-enabled/dir.conf
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
/etc/apache2/apache2.conf
$ cgit diff /etc/apache2/apache2.conf diff --git a/etc/apache2/apache2.conf b/etc/apache2/apache2.conf index 2410e6d..b943101 100644 --- a/etc/apache2/apache2.conf +++ b/etc/apache2/apache2.conf @@ -167,9 +167,9 @@ Include ports.conf Require all granted </Directory> -<Directory /var/www/> - Options Indexes FollowSymLinks - AllowOverride None +<Directory /home/wweb/www/> + Options Indexes FollowSymLinks execCGI + AllowOverride All Require all granted </Directory>
/etc/apache2/conf-available/serve-cgi-bin.conf
$ cgit diff /etc/apache2/conf-available/serve-cgi-bin.conf diff --git a/etc/apache2/conf-available/serve-cgi-bin.conf b/etc/apache2/conf-available/serve-cgi-bin.conf index ae660b1..19fa042 100644 --- a/etc/apache2/conf-available/serve-cgi-bin.conf +++ b/etc/apache2/conf-available/serve-cgi-bin.conf @@ -8,8 +8,8 @@ </IfModule> <IfDefine ENABLE_USR_LIB_CGI_BIN> - ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ - <Directory "/usr/lib/cgi-bin"> + ScriptAlias /cgi-bin/ /home/wweb/www/html/dokuwiki/ + <Directory "/home/wweb/www/html"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Require all granted
/etc/apache2/sites-available/default-ssl.conf
# DocumentRoot /var/www/html DocumentRoot /home/wweb/www/html
configuration test and restart apache2
test for configuration errors:
$ sudo apache2ctl configtest Syntax OK
reload Apache so these changes take effect:
$ sudo systemctl restart apache2 $ sudo systemctl status apache2 $ sudo systemctl status apache2 ● apache2.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled) Active: active (running) since Tue 2025-02-25 20:27:32 JST; 5s ago ..... or $ sudo /etc/init.d/apache2 restart $ sudo /etc/init.d/apache2 status
Enabling SSL
make wweb_ssl.conf from 000-default.conf
/etc/apache2/sites-available/wweb_ssl.conf –
<VirtualHost *:443> ServerAdmin webmaster@localhost DocumentRoot /home/wweb/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key <FilesMatch "\.(?:cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> </VirtualHost>
–
sudo a2ensite wweb_ssl.conf sudo a2enmod ssl sudo systemctl reload apache2
To Be Continued
