ich habe heute eine LAMP-Umgebung mit Hilfe der Seite https://en.opensuse.org/SDB:LAMP_setup erstellt und habe ein paar Fragen zur weiteren Vorgehensweise.
Wenn ich es richtig sehe, wurden im Verzeichnis /etc mehrere Verzeichnisse neu angelegt, u. a. apache2 und php7
Das Hoch- und Runterfahren der Datenbank klappt:
systemctl start mysql
systemctl stop mysql
systemctl restart mysql
Der Webserver funktioniert ebenfalls:
Wenn ich localhost im Browser eingebe, wird die Seite index.html aus dem Verzeichnis /srv/www/htdocs angezeigt.
Ich habe dort eine weitere Seite angelegt - index.php mit dem Inhalt <?php>phpinfo() -, die wird auch angezeigt.
Nun möchte ich aber mit mehreren Projekten in meinem üblichen Projektpfad arbeiten. Ich habe unter ~/Projekte deshalb zwei Test-Projekte angelegt, php1 und php2.
Was muss ich tun, wo muss ich was ändern, damit ich im Browser nur php1 (für den Aufruf von index.html) bzw. php1/index.php eingeben muss - das gleiche mit php2 -, damit mir das Projekt meiner Wahl angezeigt wird bzw. ggf. auch localhost (für /srv/www/htdocs/index.html) und localhost/php1 (für ~Projekte/php1/index.html) statt php1 sowie localhost/php2 (für ~Projekte/php2/index.html) statt php2?
Und was ist generell üblich? Vielleicht habe ich ja einen falschen Ansatz. Irgendwann werde ich aus der Test-/Anfängerphase heraus sein und dann ein Projekt auf den Strato-Server portieren wollen unter https://www.mein-PHP-Projekt.de; könnte/sollte ich die URL jetzt schon irgendwo eingeben?
Grüße
Linux:/etc # cd ~/Projekte
Linux:~/Projekte # ls -l php*
php1:
insgesamt 8
-rw-r--r-- 1 user1 users 62 20. Feb 11:38 index.html
-rw-r--r-- 1 user1 users 39 20. Feb 11:34 index.php
php2:
insgesamt 8
-rw-r--r-- 1 user1 users 62 20. Feb 11:38 index.html
-rw-r--r-- 1 user1 users 39 20. Feb 11:34 index.php
Linux:~/Projekte #
-------------------------------------------------------
Linux:/srv/www/htdocs # ls -l
insgesamt 20
-rw-r--r-- 1 root root 302 13. Mär 2006 favicon.ico
drwxr-xr-x 1 root root 106 10. Nov 13:42 gif
-rw-r--r-- 1 root root 45 11. Jun 2007 index.html
-rw-r--r-- 1 root root 21 20. Feb 07:53 index.php
-rw-r--r-- 1 root root 2356 7. Aug 2017 info2html.css
drwxr-xr-x 1 root root 2954 20. Feb 07:07 phpMyAdmin
-rw-r--r-- 1 root root 26 29. Jan 12:06 robots.txt
Linux:/srv/www/htdocs #
Alias /icons/ "/usr/share/apache2/icons/"
<Directory "/usr/share/apache2/icons">
.........
</Directory>
Ich habe zB hinzugefügt:
Alias /milweb/ "/home/henk/milweb/"
<Directory "/home/henk/milweb">
.............
</Directory>
und
Alias /smweb/ "/home/smweb/midrange/"
<Directory "/home/smweb/midrange">
............
</Directory>
Vom client/browser werden die (durch benützen der Alias Name) als http://localhost/ http://localhost/icons/
http:://localhost/milweb/
http:/localhost/midrange/
gefunden (von andere clients localhost natürlich erzetzt durck den URL des Systems.
Natürlicherweise muß das Apache prozess diese Directories/Files lesen können!
Ok, hab “übersehen” dass es ja scheinbar in /srv/www/htdocs/ funktioniert, aber nicht in anderen Verzeichnissen.
Liegt dann vermutlich an der Apache Konfiguration.
Wie greifst du jetzt genau auf diese alternativen Verzeichnisse zu?
Wie ist die zugehörige Config?
Evtl. kannst/musst du mit einer .htaccess Datei im entsprechenden Verzeichnis das Ausführen von PHP-Scripts erlauben, bin aber im Moment überfragt wie genau das geht (ist ne Weile her dass ich Apache konfiguriert habe…)
In /home/user/public_html/ sollts aber per default gehen, vielleicht wäre ein akzeptabler Workaround für die einzelnen Projekte verschiedene User zu benützen (bzw. Unterordner von /home/user/public_html/ eines einzelnen Benutzer).
Ich bin mir nicht sicher, welche Datei(en) du meinst.
Linux:/etc/apache2 # ls -lr
insgesamt 124
drwxr-xr-x 1 root root 64 20. Feb 07:07 vhosts.d
-rw-r--r-- 1 root root 22 29. Jan 12:06 uid.conf
drwxr-xr-x 1 root root 76 20. Feb 07:07 sysconfig.d
drwxr-xr-x 1 root root 20 20. Feb 07:07 ssl.prm
drwx------ 1 root root 20 20. Feb 07:07 ssl.key
-rw-r--r-- 1 root root 6388 29. Jan 12:06 ssl-global.conf
drwxr-xr-x 1 root root 20 20. Feb 07:07 ssl.csr
drwxr-xr-x 1 root root 20 20. Feb 07:07 ssl.crt
drwxr-xr-x 1 root root 20 20. Feb 07:07 ssl.crl
-rw-r--r-- 1 root root 4868 29. Jan 12:06 server-tuning.conf
-rw-r--r-- 1 root root 85 29. Jan 12:06 mod_usertrack.conf
-rw-r--r-- 1 root root 1504 29. Jan 12:06 mod_userdir.conf
-rw-r--r-- 1 root root 456 29. Jan 12:06 mod_status.conf
-rw-r--r-- 1 root root 1020 29. Jan 12:06 mod_reqtimeout.conf
-rw-r--r-- 1 root root 763 18. Mär 2017 mod_perl-startup.pl
-rw-r--r-- 1 root root 5243 29. Jan 12:06 mod_mime-defaults.conf
-rw-r--r-- 1 root root 1057 29. Jan 12:06 mod_log_config.conf
-rw-r--r-- 1 root root 499 29. Jan 12:06 mod_info.conf
-rw-r--r-- 1 root root 344 29. Jan 12:06 mod_cgid-timeout.conf
-rw-r--r-- 1 root root 1503 29. Jan 12:06 mod_autoindex-defaults.conf
lrwxrwxrwx 1 root root 13 20. Feb 07:07 mime.types -> ../mime.types
-rw-r--r-- 1 root root 13077 29. Jan 12:03 magic
-rw-r--r-- 1 root root 1957 29. Jan 12:06 loadmodule.conf
-rw-r--r-- 1 root root 1053 29. Jan 12:06 listen.conf
-rw-r--r-- 1 root root 9033 29. Jan 12:06 httpd.conf
-rw-r--r-- 1 root root 128 29. Jan 12:06 global.conf
-rw-r--r-- 1 root root 2924 29. Jan 12:06 errors.conf
-rw-r--r-- 1 root root 4260 29. Jan 12:06 default-server.conf
drwxr-xr-x 1 root root 124 20. Feb 14:58 conf.d
-rw-r--r-- 1 root root 1764 29. Jan 12:06 charset.conv
---
Linux:/etc/apache2/conf.d # more php7.conf
<IfModule mod_php7.c>
<FilesMatch "\.ph(p[345]?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.php[345]?s$">
SetHandler application/x-httpd-php-source
</FilesMatch>
DirectoryIndex index.php4
DirectoryIndex index.php5
DirectoryIndex index.php
</IfModule>
---
Linux:/etc/apache2 # more httpd.conf
#
# /etc/apache2/httpd.conf
#
# This is the main Apache server configuration file. It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information about
# the directives.
# Based upon the default apache configuration file that ships with apache,
# which is based upon the NCSA server configuration files originally by Rob
# McCool. This file was knocked together by Peter Poeml <poeml+apache@suse.de>.
# If possible, avoid changes to this file. It does mainly contain Include
# statements and global settings that can/should be overridden in the
# configuration of your virtual hosts.
# Quickstart guide:
# http://en.opensuse.org/SDB:Apache_installation
# Overview of include files, chronologically:
#
# httpd.conf
# |
# |-- uid.conf . . . . . . . . . . . . . . UserID/GroupID to run under
# |-- server-tuning.conf . . . . . . . . . sizing of the server (how many processes to start, ...)
# |-- loadmodule.conf . . . . . . . . . . .
[li] load these modules[/li]# |-- listen.conf . . . . . . . . . . . . . IP adresses / ports to listen on
# |-- mod_log_config.conf . . . . . . . . . define logging formats
# |-- global.conf . . . . . . . . . . . . .
[li] server-wide general settings[/li]# |-- mod_status.conf . . . . . . . . . . . restrict access to mod_status (server monitoring)
# |-- mod_info.conf . . . . . . . . . . . . restrict access to mod_info
# |-- mod_reqtimeout.conf . . . . . . . . . set timeout and minimum data rate for receiving requests
# |-- mod_cgid-timeout.conf . . . . . . . . set CGIDScriptTimeout if mod_cgid is loaded/active
# |-- mod_usertrack.conf . . . . . . . . . defaults for cookie-based user tracking
# |-- mod_autoindex-defaults.conf . . . . . defaults for displaying of server-generated directory listings
# |-- mod_mime-defaults.conf . . . . . . . defaults for mod_mime configuration
# |-- errors.conf . . . . . . . . . . . . . customize error responses
# |-- ssl-global.conf . . . . . . . . . . . SSL conf that applies to default server _and all_ virtual hosts
# |
# |-- default-server.conf . . . . . . . . . set up the default server that replies to non-virtual-host requests
# | |--mod_userdir.conf . . . . . . . . enable UserDir (if mod_userdir is loaded)
# | `--conf.d/apache2-manual?conf . . . add the docs ('?' = if installed)
# |
# `-- vhosts.d/ . . . . . . . . . . . . . . for each virtual host, place one file here
# `-- *.conf . . . . . . . . . . . . . (*.conf is automatically included)
#
#
# Files marked
[li] are NOT read when server is started via systemd service. When server[/li]# is started via service, defaults from /etc/sysconfig/apache2 are taken into account.
#
# Filesystem layout:
#
# /etc/apache2/
# |-- charset.conv . . . . . . . . . . . . for mod_auth_ldap
# |-- conf.d/
# | |-- apache2-manual.conf . . . . . . . conf that comes with apache2-doc
# | |-- mod_php4.conf . . . . . . . . . . (example) conf that comes with apache2-mod_php4
# | `-- ... . . . . . . . . . . . . . . . other configuration added by packages
# |-- default-server.conf
# |-- errors.conf
# |-- httpd.conf . . . . . . . . . . . . . top level configuration file
# |-- listen.conf
# |-- magic
# |-- mime.types -> ../mime.types
# |-- mod_autoindex-defaults.conf
# |-- mod_info.conf
# |-- mod_log_config.conf
# |-- mod_mime-defaults.conf
# |-- mod_perl-startup.pl
# |-- mod_status.conf
# |-- mod_userdir.conf
# |-- mod_usertrack.conf
# |-- server-tuning.conf
# |-- ssl-global.conf
# |-- ssl.crl/ . . . . . . . . . . . . . . PEM-encoded X.509 Certificate Revocation Lists (CRL)
# |-- ssl.crt/ . . . . . . . . . . . . . . PEM-encoded X.509 Certificates
# |-- ssl.csr/ . . . . . . . . . . . . . . PEM-encoded X.509 Certificate Signing Requests
# |-- ssl.key/ . . . . . . . . . . . . . . PEM-encoded RSA Private Keys
# |-- ssl.prm/ . . . . . . . . . . . . . . public DSA Parameter Files
# |-- global.conf
# |-- loadmodule.conf
# |-- uid.conf
# `-- vhosts.d/ . . . . . . . . . . . . . . put your virtual host configuration (*.conf) here
# |-- vhost-ssl.template
# `-- vhost.template
### Global Environment ######################################################
#
# The directives in this section affect the overall operation of Apache,
# such as the number of concurrent requests.
# run under this user/group id
Include /etc/apache2/uid.conf
# - how many server processes to start (server pool regulation)
# - usage of KeepAlive
Include /etc/apache2/server-tuning.conf
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
ErrorLog /var/log/apache2/error_log
# generated from default value of APACHE_MODULES in /etc/sysconfig/apache2
<IfDefine !SYSCONFIG>
Include /etc/apache2/loadmodule.conf
</IfDefine>
# IP addresses / ports to listen on
Include /etc/apache2/listen.conf
# predefined logging formats
Include /etc/apache2/mod_log_config.conf
# generated from default values of global settings in /etc/sysconfig/apache2
<IfDefine !SYSCONFIG>
Include /etc/apache2/global.conf
</IfDefine>
# optional mod_status, mod_info
Include /etc/apache2/mod_status.conf
Include /etc/apache2/mod_info.conf
# mod_reqtimeout protects the server from the so-called "slowloris"
# attack: The server is not swamped with requests in fast succession,
# but with slowly transmitted request headers and body, thereby filling up
# the request slots until the server runs out of them.
# mod_reqtimeout is lightweight and should deliver good results
# with the configured default values. You shouldn't notice it at all.
Include /etc/apache2/mod_reqtimeout.conf
# Fix for CVE-2014-0231 introduces new configuration parameter
# CGIDScriptTimeout. This directive and its effect prevent request
# workers to be eaten until starvation if cgi programs do not send
# output back to the server within the timout set by CGIDScriptTimeout.
Include /etc/apache2/mod_cgid-timeout.conf
# optional cookie-based user tracking
# read the documentation before using it!!
Include /etc/apache2/mod_usertrack.conf
# configuration of server-generated directory listings
Include /etc/apache2/mod_autoindex-defaults.conf
# associate MIME types with filename extensions
TypesConfig /etc/apache2/mime.types
Include /etc/apache2/mod_mime-defaults.conf
# set up (customizable) error responses
Include /etc/apache2/errors.conf
# global (server-wide) SSL configuration, that is not specific to
# any virtual host
Include /etc/apache2/ssl-global.conf
# forbid access to the entire filesystem by default
<Directory />
Options None
AllowOverride None
<IfModule !mod_access_compat.c>
Require all denied
</IfModule>
<IfModule mod_access_compat.c>
Order deny,allow
Deny from all
</IfModule>
</Directory>
# use .htaccess files for overriding,
AccessFileName .htaccess
# and never show them
<Files ~ "^\.ht">
<IfModule !mod_access_compat.c>
Require all denied
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Deny from all
</IfModule>
</Files>
# List of resources to look for when the client requests a directory
DirectoryIndex index.html index.html.var
### 'Main' server configuration #############################################
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition. These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
#
Include /etc/apache2/default-server.conf
### Virtual server configuration ############################################
#
# VirtualHost: If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
IncludeOptional /etc/apache2/vhosts.d/*.conf
# Note: instead of adding your own configuration here, consider
# adding it in your own file (/etc/apache2/httpd.conf.local)
# putting its name into APACHE_CONF_INCLUDE_FILES in
# /etc/sysconfig/apache2 -- this will make system updates
# easier :)
Und ich bin mir nicht sicher was du bis jetzt genau gemacht hast, um auf deine Verzeichnisse zuzugreifen.
Du musst die ja irgendwo mittels <Directory “xxx”>…</Directory> deklariert haben (wie von hcvv beschrieben), oder?
(ich denke sonst würde Apache ja überhaupt keinen Zugriff erlauben, bzw. sie nicht “finden”)
Dort fügst du eben diese Zeilen hinzu.
Oder eben in einer .htaccess Datei im entsprechenden Verzeichnis (mit den HTML/PHP Files).
Bzw. ich denke ein “Include /etc/apache2/conf.d/php7.conf” sollte auch gehen.
Falls du mehrere vhosts angelegt hast (was meines Erachtens nach die “normale” Herangehensweise wäre um mehrere Webseiten/Projekte zu haben), ist wahrscheinlich dort was “falsch”. Ein Posten der entsprechended vhosts Config könnte dann helfen…
Ich habe keine vhosts angelegt. Es gibt in /etc eine hosts, die sieht so aus:
#
# hosts This file describes a number of hostname-to-address
# mappings for the TCP/IP subsystem. It is mostly
# used at boot time, when no name servers are running.
# On small systems, this file can be used instead of a
# "named" name server.
# Syntax:
#
# IP-Address Full-Qualified-Hostname Short-Hostname
#
127.0.0.1 localhost
# special IPv6 addresses
::1 localhost ipv6-localhost ipv6-loopback
fe00::0 ipv6-localnet
ff00::0 ipv6-mcastprefix
ff02::1 ipv6-allnodes
ff02::2 ipv6-allrouters
ff02::3 ipv6-allhosts
Und dann gibt es noch zwei httpd-vhosts.conf in /opt/lampp/etc/extra und /opt/lampp/etc/original/extra/ mit folgendem Inhalt:
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/opt/lampp/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/opt/lampp/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
Das ist was komplett anderes, darin kannst du statisch Hostnamen zu IP-Adressen zuweisen.
Ich probiers nochmal:
Apache erlaubt normalerweise nur Zugriff auf /srv/www/htdocs/ bzw. ~/public_html/ (via http://localhost/~username/)
Du hast aber geschrieben dass deine HTML/PHP-Dateien in ~/Projekte/ liegen.
Was hast du wo geändert dass diese als Webseite angezeigt werden?
(eine Möglichkeit wäre vhosts mit einem anderen DocumentRoot anzulegen, das hast du aber scheinbar nicht gemacht…)
Obwohl, da fällt mir grade noch was anderes auf.
Du hast geschrieben:
Um PHP-Code auszuführen, muss die Datei aber auch xxx.php heissen, also nicht index.html sondern index.php.
Ist das vielleicht das unmittelbare Problem?
Bzw. du hast ja scheinbar beides, also probier mal index.html zu löschen…
Ansonsten kanns sein dass die bevorzugt wird wenn du nicht explicit http://xxx/index.php aufrufst.
Und das ist normal.
Ein HTML-File ist eben ein HTML-File, und kein PHP-File.
Wenn du PHP-Code verwenden willst, muss die Datei xxx.php heissen, wie geschrieben (oder alternativ xxx.php3, xxx.php4, xxx.php5 oder xxx.phtml). Nur dann schickt Apache sie durch den PHP-Interpreter.
Das ist von den SetHandler Direktiven in der Config (php7.conf) so definiert:
Das sagt Apache eben, dass Dateien mit dem angegebenen Muster PHP-Files sind…
Du könntest natürlich auch konfigurieren dass .html Files in Wirklichkeit auch PHP-Files sind, aber das macht nicht wirklich Sinn, meiner Meinung nach.
Das Thema PHP-Code-Interpretation im HTML-File hake ich ab, da es offenbar kein Standard ist und nur mit Klimmzügen und von hinten durch die Brust ins Auge möglich ist.
Bevor es jetzt noch chaotischer wird, würde ich sagen, ich versuche es mit Hilfe der vorgenannten Seite hinzubekommen in Sachen php1- und php2-Testpfaden, und dann melde ich mich noch mal.
“PHP-Code-Interpretation im HTML-File” ist schon Standard. Aber die Datei muss eben xxx.php heissen um Apache zu sagen dass da (auch) PHP-Code enthalten ist. Ansonsten wird Apache eben einfach den Inhalt der Datei liefern ohne den PHP-Interpreter drüberlaufen zu lassen.
Apache selbst hat keine Ahnung von PHP, außerdem gibts ja auch noch andere Skriptsprachen…