Set up a LAMP stack on OpenWrt
Read here: LAMP (software bundle) about the concept. This guide provides step by step instructions for installing a full featured LAMP stack on OpenWrt.
| Service | Software used | Description |
|---|---|---|
| Web server | http.overview | a lot of web server to choose from |
| uHTTPd | this is already used for the WebUI LuCI | |
| Lighttpd | ||
| Apache | ||
| Nginx | ||
| Database server | database.overview | you have again the choice |
| MySQL | ||
| PostgreSQL | ||
| Scripting language | PHP | |
| Perl | ||
| Python |
Basic System Configuration
Modify your /etc/hosts file to resemble the following example. Replace "domain.tld" with your own domain name, and select a unique name to replace "llmp" with. This will be your system's FQDN (Fully Qualified Domain Name).
To get such a /etc/hosts file from a standard OpenWrt hosts file you can run the one liner below:
echo "$(uci get network.lan.ipaddr) llmp.domain.tld llmp" >> /etc/hosts
Your /etc/hosts file should look like this then:
File: /etc/hosts
127.0.0.1 localhost
192.168.1.1 llmp.domain.tld llmp
|
Issue the following commands to set your system's hostname, replacing "OpenWrt" with the hostname you picked above.
uci set system.@system[0].hostname=OpenWrt-LLMP uci commit system echo "$(uci get system.@system[0].hostname)" > /proc/sys/kernel/hostname
Installing and configuring a web server
You might already have a web server for the Web UI installed and running. Choose any of the available WebServer for this purpose: http.overview. If the web server is not in the OpenWrt packet repository, you could always crosscompile it from source.
uHTTPd
→ http.uhttpd is an in-house web server under BSD-license. LuCI WebUI already uses this. If uHTTPd is not already installed you can install it with:
opkg update opkg install uhttpd
The default image runs a WebUI for OpenWrt on port 80 (HTTP) and port 443 (HTTPS). For our PHP5 enabled uHTTPd web server we start a new uHTTPd instance on a different port. We use port 81 here.
uci set uhttpd.llmp=uhttpd uci set uhttpd.llmp.listen_http=81 uci set uhttpd.llmp.home=/srv/www uci commit uhttpd
Create a directory for our web server content
mkdir -p $(uci get uhttpd.llmp.home)
If uHTTPd was already installed and running restart it now with
/etc/init.d/uhttpd restart
If you installed uHTTPd via opkg start the web server manually and also at boot by enabling the init script
/etc/init.d/uhttpd start /etc/init.d/uhttpd enable
Further configuration can also be performed manually, e.g. to enable php. http://wiki.openwrt.org/doc/uci/uhttpd
Lighttpd
→ http.lighttpd is a lightweight and very flexible web server with lots of additional modules available.
opkg update opkg install lighttpd lighttpd-mod-cgi
Edit /etc/lighttpd/lighttpd.conf and change a few settings:
OLD:
#server.modules = (
# "mod_cgi"
#) |
NEW:
server.modules = (
"mod_cgi"
) |
OLD:
server.document-root = "/www/" |
NEW:
server.document-root = "/srv/www/" |
OLD:
#server.port = 81 |
NEW:
server.port = 81 |
(End of changes to /etc/lighttpd/lighttpd.conf)
Edit /etc/php.ini and change:
OLD:
doc_root = "/www" |
NEW:
doc_root = "/srv/www" |
(End of changes to /etc/php.ini)
Create a directory for our web server content
mkdir -p /srv/www
Start the server manually and also at boot by enabling the init script
/etc/init.d/lighttpd start /etc/init.d/lighttpd enable
Nginx
→ http.nginx is nice as well.
Apache
→ http.apache is nice as well.
Testing the web server
Create a little test web page, e.g. /srv/www/index.html:
echo "<P>Hello, this web server runs on OpenWrt!!</P>" > /srv/www/index.html
Point your browser to the routers IP address and the port the web server is listening on (e. g. http://192.168.1.1:81/index.html)
Installing PHP
See →PHP to install a version of PHP.
Configuring PHP
uHTTPd
uci add_list uhttpd.llmp.interpreter=".php=/usr/bin/php-cgi" uci set uhttpd.llmp.index_page="index.html index.htm default.html default.htm index.php" uci commit uhttpd
sed -i 's,doc_root.*,doc_root = "",g' /etc/php.ini
sed -i 's,;short_open_tag = Off,short_open_tag = On,g' /etc/php.ini
Restart uHTTPd now with
/etc/init.d/uhttpd restart
Further configuration can also be performed manually, e.g. to enable php. http://wiki.openwrt.org/doc/uci/uhttpd
Lighttpd
In /etc/lighttpd/lighttpd.conf change
Right after
#cgi.assign = ( ".pl" => "/usr/bin/perl", ".cgi" => "/usr/bin/perl" ) |
add
cgi.assign = ( ".php" => "/usr/bin/php-cgi" ) |
Change
index-file.names = ( "index.html", "default.html", "index.htm", "default.htm" ) |
to
index-file.names = ( "index.html", "default.html", "index.htm", "default.htm", "index.php" ) |
And in /etc/php.ini change
doc_root = "/www" |
to
doc_root = "/srv/www" |
ps: If you are making some crazy URL rewriting in lighttpd and getting a 'No input file specified.' error in the browser, remove this configuration (as per lighttpd FAQ)
Restart lighttpd
/etc/init.d/lighttpd restart
Apache
Nginx
Testing PHP
We are using the phpinfo() function for a first test.
echo "<?php phpinfo(); ?>" > /srv/www/index.php
Point your browser to the routers IP address and the port the web server is listening on (e. g. http://192.168.1.1:81/index.php)
If you get a blank page you can run your script with php-cgi from the router's shell to see if there are any errors
php-cgi /srv/www/index.php
Installing and configuring a database server
MySQL
opkg update opkg install libpthread libncurses libreadline mysql-server sed -i 's,^datadir.*,datadir = "/srv/mysql",g' /etc/my.cnf sed -i 's,^tmpdir.*,tmpdir = "/tmp",g' /etc/my.cnf mkdir -p /srv/mysql mysql_install_db --force /etc/init.d/mysqld start /etc/init.d/mysqld enable mysqladmin -u root password 'new-password'
To enable MySQL in PHP install
opkg update opkg install php5-mod-mysql
and load the mysql.so module in /etc/php.ini
sed -i 's,;extension=mysql.so,extension=mysql.so,g' /etc/php.ini
https://forum.openwrt.org/viewtopic.php?pid=145009#p145009
To use the MySQLi module in PHP install
opkg update opkg install php5-mod-mysqli
and load the mysqli.so module in /etc/php.ini
sed -i 's,;extension=mysqli.so,extension=mysqli.so,g' /etc/php.ini
Besides, in /etc/php.ini, duplicate the block named [MySQL] to [MySQLi] and rename all "mysql."-options to "mysqli.". To access a local MySQL server via socket, modify the value of "mysqli.default_socket" (which can be found in /etc/my.cnf):
mysqli.default_socket = /var/run/mysqld.sock
For MySQL to work with PHP, you must also configure the php.ini (vi /etc/php.ini) file, under the [MySQL] section.
- Here is an example:
[MySQL] mysql.allow_local_infile = On mysql.allow_persistent = On mysql.cache_size = 2000 mysql.max_persistent = -1 mysql.max_links = -1 mysql.default_port = 3306 mysql.default_socket = /tmp/run/mysqld.sock mysql.default_host = 127.0.0.1 mysql.default_user = root mysql.default_password = MySuperSecretPassword mysql.connect_timeout = 60 mysql.trace_mode = Off
PostgreSQL
Administering
CLI
WebUI
Troubleshooting
Notes
doc/howto/lamp.txt · Last modified: 2013/05/11 14:10 by tmomas
