Getting Started

Yichun Zhang , 03 Nov 2011 (created 20 Jun 2011)

First of all, please go to the Download page to get the source code tarball of OpenResty, and see the Installation page for how to build and install it into your system.

HelloWorld

Prepare directory layout

We first create a separate directory for our experiments. You can use an arbitrary directory. Here for simplicity, we just use ~/work:

  1. mkdir ~/work
  2. cd ~/work
  3. mkdir logs/ conf/

Note that we’ve also created the logs/ directory for logging files and conf/ for our config files.

Prepare the nginx.conf config file

Create a simple plain text file named conf/nginx.conf with the following contents in it:

  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. server {
  8. listen 8080;
  9. location / {
  10. default_type text/html;
  11. content_by_lua_block {
  12. ngx.say("<p>hello, world</p>")
  13. }
  14. }
  15. }
  16. }

If you’re familiar with Nginx configuration, it should look very familiar to you. OpenResty is just an enhanced version of Nginx by means of addon modules anyway. You can take advantage of all the exisitng goodies in the Nginx world.

Start the Nginx server

Assuming you have installed OpenResty into /usr/local/openresty (this is the default), we make our nginx executable of our OpenResty installation available in our PATH environment:

  1. PATH=/usr/local/openresty/nginx/sbin:$PATH
  2. export PATH

Then we start the nginx server with our config file this way:

  1. nginx -p `pwd`/ -c conf/nginx.conf

Error messages will go to the stderr device or the default error log files logs/error.log in the current working directory.

Access our HelloWorld web service

We can use curl to access our new web service that says HelloWorld:

  1. curl http://localhost:8080/

If everything is okay, we should get the output

  1. <p>hello, world</p>

You can surely point your favorite web browser to the location http://localhost:8080/.

Test performance

See Benchmark for details.

Where to go from here

View the documentation of each component at the Components page and find Nginx related stuff on the Nginx Wiki site.