/*** Frankly, this page is a mess. It has lots of overlap with the main pages for the different components. Also, steps to take are sometimes overly specific, things like NEW: <it was this> OLD: <change it to this> and also "change the line after <... this line ...> to <... this line ...>. That's too specific: readers are not stupid. Second thing is they don't explain things, but only provide the config. This is not how a guide should be written: you should give the config lines, but also explain what they do. I removed most of them now, but maybe this page can be removed in its entirety. ***/
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 | Examples | Description |
|---|---|---|
| Web server | uHTTPd | OpenWrt's in-house server, used by default for the WebUI LuCI |
| ::: | Lighttpd | Lightweight and flexible, many addons |
| ::: | Apache | Powerful and widely used |
| ::: | Nginx | Aimed at good performance, low memory |
| Database server | MySQL | Widely used SQL server |
| ::: | PostgreSQL | Another popular SQL server |
| ::: | SQLite | Easy to use SQL library for low powered devices, runs within process |
| Scripting language | php | Specially designed for making websites |
| ::: | perl | Flexible high level general purpose language |
| ::: | python | Another high level scripting language |
This article is a collection of examples of the configuration and integration of web servers, database servers and scripting languages, i.e. LAMP. For each example we assume to be creating a web page with /srv/www/ as the document root and assume an otherwise standard OpenWrt configuration. Note that it currently has a lot of overlap with the main articles for the respective services. FIXME It should be made more to the point and only about installing and especially integrating these services.
/* THIS SECTION HAS BEEN COMMENTED OUT, PENDING DELETION. To the author of this part: It is very unclear to me why you would be changing your hosts file just to accomodate an example. Also, the llmp you take as a host name is almost never used in the rest of the article. Please clarify and complete the rest of the examples, while trying not to create to much overlap with the main articles for the web servers, scripting and database tools etc. For now, I have commented out this section and updated the remainder of the article. Feel free to discuss about it.
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
*/
You might already have a web server for the Web UI installed and running. Choose any of the available WebServer for this purpose: webserver. If the web server is not in the OpenWrt packet repository, you could always crosscompile it from source.
-> 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. uhttpd
-> 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:
Enable CGI:
server.modules = (
"mod_cgi"
)
Set the document root and the port for our example:
server.document-root = "/srv/www/"
server.port = 81
Edit /etc/php.ini and set the document root here as well (or leave it empty, in which case it allows PHP serving anywhere outside the docroot):
doc_root = "/srv/www"
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 is nice as well.
-> http.apache is nice as well.
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)
See ->php to install a version of PHP. The remainder of this section assumes you have a proper PHP install.
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. uhttpd
In /etc/lighttpd/lighttpd.conf set the following to activate PHP for files with .php extension.
cgi.assign = ( ".php" => "/usr/bin/php-cgi" ) |
Add index.php to the default file names, such that it loads it automatically if present.
index-file.names = ( "index.html", "default.html", "index.htm", "default.htm", "index.php" ) |
Set the proper document root
doc_root = "/srv/www" |
/* what is meant by this? what configuration? I commented it out, please someone explain or delete: Remark: If you are doing 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
[http.apache#Configuring Apache and PHP5](/docs/guide-user/services/webserver/http.apache#Configuring Apache and PHP5)
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
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.
<!-- -->
[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