28

I want to be able to start an apache server from the command line, typing something like apache site-folder or apache . --port=2000

This should read and use .htaccess files.

I know about python -m SimpleHTTPServer and it's close to what I need, but not quite.

Ideal solutions:

  1. Contributing a great command line interface to apache itself
  2. Writing a simple command line tool that wraps/contains apache (or something)
  3. Linking to docs on an existing cli for apache

I just want to type command <Directory> --port=8000 --other-options

The command name could also be pache

At some point I may want to use this in production. It should be easy to send the process to the background, and then stop that instance or all instances, like forever

Relevant links: http://httpd.apache.org/docs/2.4/programs/httpd.html

Also

It should be only one command for anyone to install the script for immediate use

Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
  • A .globalhtaccess file in the home directory would be awesome – Devin Rhode Apr 04 '13 at 19:00
  • Why are you looking for such an approach as opposed to traditional virtual hosts? – Chris Wesseling Apr 07 '13 at 06:22
  • I just need a simple way to start an apache server from my current directory and have it use .htaccess files for development. What exactly do you mean traditional virtual hosts? – Devin Rhode Apr 08 '13 at 01:27
  • 1
    I mean the ones you define with the [VirtualHost](http://httpd.apache.org/docs/2.4/vhosts/examples.html) directive. – Chris Wesseling Apr 08 '13 at 09:32
  • If I knew about this i might use it, I don't know much of anything about using a VirtualHost directive. Feel free to provide an answer – Devin Rhode Apr 08 '13 at 21:40
  • 1
    The 500 bounty looks tantalizing, but it doesn't answer your question. In production, you should use the `apachectl` command to (re)start and stop. And properly configure your port based hosts with VirtualHost directives. How to do that is to broad a question. It takes at least one [book](http://shop.oreilly.com/product/9780596007249.do) to answer. Study and understand other default configs like from Debian or OpenBSD, before you think up your own production config. Convenience in production is more than a one-liner; it's not being called out of your nights sleep 'cause your servers are down. – Chris Wesseling Apr 09 '13 at 05:44
  • 1
    *Or the init/service script, whatever that is on your platform, instead of apachectl.* – Chris Wesseling Apr 09 '13 at 05:45
  • You want a command that will start an instance of apache but with the root http folder being the current working directory? – Menelaos Apr 09 '13 at 12:41
  • When starting apachectl you may point him to any config you want - why not creating config on the fly? Take current directory, take arguments (port, name of virtual server), concat them with "basic apache config", create config for virtual server and add them to your freshly created apache config and run apachectl. In 30 minutes I'd do it in c# ;) but if you're already using apache, looks like bash/python would be much more of use. – mrówa Apr 10 '13 at 03:34
  • sounds alright, just need an easy to use script – Devin Rhode Apr 10 '13 at 07:29

7 Answers7

18

What about apache debug mode (-X) ?

apache2 -X -d. -f.htaccess -C"PidFile `mktemp`"  -C"Listen 1025" 
-C"ErrorLog /dev/stdout" -C"DocumentRoot `pwd`"

to put it in the background once started you may use Ctrl^Z then type "bg"

Using the the FOREGROUND flag, wrapping it up in a shell script:

#!/bin/sh
if [ $# -ne 2 ]; then 
    echo "$0 <port> <dir>"
    exit 10 
fi
/usr/sbin/apache2 -DFOREGROUND -d. -f.htaccess -C"PidFile `mktemp`" \ 
-C"Listen $1" -C"ErrorLog /dev/stdout" -C"DocumentRoot $2" -e debug

call it "pache", chmod +x, then you can run

./pache 1026 /tmp/webroot
Eric
  • 7,787
  • 5
  • 19
  • 34
gpilotino
  • 13,055
  • 9
  • 48
  • 61
  • Really cool! I got this output: $ ./pache 1026 . –– usage: mktemp [-d] [-q] [-t prefix] [-u] template ... –– mktemp [-d] [-q] [-u] -t prefix –– ./pache: line 6: /usr/sbin/apache2: No such file or directory –– ./pache: line 7: -CListen 1026: command not found So I was going to install the latest apache from homebrew.. which is in homebrew-dupes and not regular homebrew by default https://github.com/Homebrew/homebrew-dupes/blob/master/httpd.rb I think this should be updated to 2.4.4, but shouldn't I install it WITHOUT --disable-debug to use your method? – Devin Rhode Apr 07 '13 at 23:33
  • afaik there are three ways to put it in foreground. using -X (debug flag), using -DFOREGOUND, using -DNO_DETACH. the latest two should be independent from --disable-debug flag. can you try with this http://pastebin.com/raw.php?i=p7sfyN5c ? "apache2" must point to your local apache installation binary. – gpilotino Apr 08 '13 at 07:43
  • So, I don't have an apache2, command but an httpd command instead. I changed that. And then.. http://pastebin.com/PS4qWgRN – Devin Rhode Apr 08 '13 at 08:36
  • don't use sudo. just add -C"LockFile foobar" – gpilotino Apr 08 '13 at 10:59
  • so doesn't `mktemp fooXXX` need to be a variable, and then you'd LockFile ? I don't know how to do that – Devin Rhode Apr 08 '13 at 21:29
  • Apache probably needs more variables to run -- couldn't you read your current installed apache config and then append the configurations you need to set to current folder (using -c instead of -C)? Otherwise you'll need to set several other configurations, like User, Group, LockFile etc. Also, if you wish to use this in production you shouldn't use debug mode, and you'd need to define StartServers, MaxClients and so on... – Capilé Apr 09 '13 at 03:53
  • This is only for development, not production. The variable I'm talking about is a bash variable, not exactly an apache variable. Are you saying I should modify the apache config to be able to work in a certain folder? I want to be able to start it in any directory. I'm not sure if -c would be better – Devin Rhode Apr 09 '13 at 16:50
  • If there could be a one time change to the apache config to easily allow starting an apache instance in any directory, that would be great. Interestingly, if there could be a simple bash script that dynamically sets the apache configs DocumentRoot to the directory that was passed in, that might work great too. – Devin Rhode Apr 09 '13 at 20:10
  • I think I'm close to a working solution: `sudo httpd -k start -c "DocumentRoot \`pwd\`" -c "ServerName localhost" -c "Listen 2000" -c "AccessFileName .htaccess" -e debug -DFOREGROUND` Aside from making a script that takes an optional directory and port as arguments, there's just last thing: how can I output requests and errors to the console? – Devin Rhode Apr 09 '13 at 22:16
  • P.S. The script should also be installable, uninstallable, and updatable with a popular package manager. I'm thinking of using node/npm – Devin Rhode Apr 09 '13 at 22:18
  • try -C"CustomLog /dev/stdout". anyway, for ports > 1024 you don't need sudo at all – gpilotino Apr 10 '13 at 07:40
  • Without sudo, after loading modules, it fails: (13)Permission denied: make_sock: could not bind to address [::]:80 –– (13)Permission denied: make_sock: could not bind to address 0.0.0.0:80 –– no listening sockets available, shutting down –– Unable to open logs – Devin Rhode Apr 11 '13 at 04:14
  • Ok, this works with logging of requests! `sudo httpd -k start -c "DocumentRoot \`pwd\`" -c "ServerName localhost" -c "Listen 2000" -c "AccessFileName .htaccess" -c "ErrorLog /dev/stdout" -c "CustomLog /dev/stdout common" -e debug -DFOREGROUND` Now it just needs to be easy to install and use – Devin Rhode Apr 11 '13 at 04:19
7

http-server is a much better simple http server than pache, it's what I use currently! :)

Use [pache][1]

Install with npm - which comes with node here: http://nodejs.org/

sudo npm install pache -g

Run on current dir, port 3000:

pache

Or specify directory and port:

pache site-directory 2000

[1]: https://github.com/devinrhode2/pache

Devin Rhode
  • 23,026
  • 8
  • 58
  • 72
  • Dear others - I'm not trying to sandbag you, there just needs to be an easy solution for my co-workers and the rest of the world to run apache at the terminal – Devin Rhode Apr 11 '13 at 11:22
  • 5
    I do hope you give the bounty to whomever inspired your javascript solution the most. Why didn't you stick with `sh`? Having to install nodejs isn't as easy as using what's already installed. – Chris Wesseling Apr 11 '13 at 12:42
  • @DevinGRhode Nice find. Hadn't heard of pache before, but I like the simplicity of it. :) Very very cool. Too bad it wasn't found by someone else. Though, having said that, for more flexibility and for the user base who might be more fluent in Apache configs, a shell wrapper solution with templated configs would work better for a larger deployment. Well, either that or customizing the pache code to do the same thing. :) – Wing Tang Wong Apr 11 '13 at 18:38
  • A pure bash solution would be better, I happen to be more familiar with node (which happens to be more cross-platform too) –– WingTangWong, I wrote pache last night – Chris, I would love to give the bounty to @gpilotino because he helped me most with figuring out the apache command, but the simplicity just isn't there yet in his answer (co-workers, friends etc need to be able to easily install the script) – Devin Rhode Apr 11 '13 at 22:43
  • Love this. It's a great option for devs who are more comfortable with node than with the lamp stack. – Askdesigners Mar 03 '15 at 09:32
5

This works:

Your apache config could point to /var/www

Then use:

sudo mount -o bind /home/webcreatorperson/mywebsite /var/www

to unbind use:

sudo umount /var/www

If you want several ports you could preconfigure ports in apache to point to directories like /var/www/8000.

Rembunator
  • 1,305
  • 7
  • 13
1

Why not use Gatling which allows you to do exactly what you want?

http://www.fefe.de/gatling/

Cyrus
  • 19
  • 1
  • Seems pretty sub-par. This should use a package manager instead of a one-time tarball. "El-cheapo .htaccess support" isn't exactly what I want. "Quick-and-dirty SSL/TLS support" also sounds bad. – Devin Rhode Apr 04 '13 at 22:39
1

For the benefit of anyone stumbling across this with the same question, I wanted something that was as simple as the pache package mentioned, but not relying on having node.js installed.

My use-case was in looking for a replacement for the webrick server used in Jekyll. Webrick, like most lightweight run-anywhere http servers, doesn't support .htaccess files.

So I took gpilotino's answer and packaged it up a bit. I've been using it for a few weeks now and although I am sure it can be improved upon it's doing the job. It comes as a script and a minimal httpd.conf file, which makes it easy to extend to support, say, PHP.

You can find it at: https://github.com/julianbrowne/apache-anywhere

Essentially, once it's installed (an optionally config tweaked) you just run:

apache -d document_root_directory -p {port}

and:

apache stop {port}

when you are done.

1

Hope you find your solution. I hate .htaccess. So I wrote this:

#!/bin/bash
cat >._apache2_dir_conf << EOF
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
ErrorLog $1/._apache2_dir_error.log
HostnameLookups Off
NameVirtualHost *:$2
ServerName joyeruc
Listen $2
PidFile $1/._apache2_pid
<VirtualHost *:$2>
    ServerAdmin joyer@uc
    DocumentRoot $1
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory $1/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
    </Directory>
    ErrorLog $1/._apache2_dir_error.log
    LogLevel warn
</VirtualHost>

EOF

#apache2 -k $3 -X -f $1/._apache2_dir_conf
apache2 -X -f $1/._apache2_dir_conf
Joyer
  • 371
  • 2
  • 9
0

Hmm... interesting.

I can see why you want to be able to spawn a separate apache instance, on demand, on a different port, etc. etc.

Ie, sometimes, you want to just change the config for one instance, or you would like users to be able to self serve, etc.

But more importantly, you want to be able to give your users Apache flexibility without it breaking Apache for everyone else. That's something that a massive virtualhost config file can't deal with.

I have something similar setup in my own environments.

In either case, to fire up Apache on a Mac with custom settings, this is what I did:

Folders/files:

~/site/conf/httpd.conf ~/site/logs/ ~/site/public/

Contents of the httpd.conf file:

# Can be substituted with an Include statement, which all httpd.conf can pull in.
# Included here for simplicity/single file example.
LoadModule authn_file_module /usr/libexec/apache2/mod_authn_file.so
LoadModule authn_dbm_module /usr/libexec/apache2/mod_authn_dbm.so
LoadModule authn_anon_module /usr/libexec/apache2/mod_authn_anon.so
LoadModule authn_dbd_module /usr/libexec/apache2/mod_authn_dbd.so
LoadModule authn_default_module /usr/libexec/apache2/mod_authn_default.so
LoadModule authz_host_module /usr/libexec/apache2/mod_authz_host.so
LoadModule authz_groupfile_module /usr/libexec/apache2/mod_authz_groupfile.so
LoadModule authz_user_module /usr/libexec/apache2/mod_authz_user.so
LoadModule authz_dbm_module /usr/libexec/apache2/mod_authz_dbm.so
LoadModule authz_owner_module /usr/libexec/apache2/mod_authz_owner.so
LoadModule authz_default_module /usr/libexec/apache2/mod_authz_default.so
LoadModule auth_basic_module /usr/libexec/apache2/mod_auth_basic.so
LoadModule auth_digest_module /usr/libexec/apache2/mod_auth_digest.so
LoadModule cache_module /usr/libexec/apache2/mod_cache.so
LoadModule disk_cache_module /usr/libexec/apache2/mod_disk_cache.so
LoadModule mem_cache_module /usr/libexec/apache2/mod_mem_cache.so
LoadModule dbd_module /usr/libexec/apache2/mod_dbd.so
LoadModule dumpio_module /usr/libexec/apache2/mod_dumpio.so
LoadModule reqtimeout_module /usr/libexec/apache2/mod_reqtimeout.so
LoadModule ext_filter_module /usr/libexec/apache2/mod_ext_filter.so
LoadModule include_module /usr/libexec/apache2/mod_include.so
LoadModule filter_module /usr/libexec/apache2/mod_filter.so
LoadModule substitute_module /usr/libexec/apache2/mod_substitute.so
LoadModule deflate_module /usr/libexec/apache2/mod_deflate.so
LoadModule log_config_module /usr/libexec/apache2/mod_log_config.so
LoadModule log_forensic_module /usr/libexec/apache2/mod_log_forensic.so
LoadModule logio_module /usr/libexec/apache2/mod_logio.so
LoadModule env_module /usr/libexec/apache2/mod_env.so
LoadModule mime_magic_module /usr/libexec/apache2/mod_mime_magic.so
LoadModule cern_meta_module /usr/libexec/apache2/mod_cern_meta.so
LoadModule expires_module /usr/libexec/apache2/mod_expires.so
LoadModule headers_module /usr/libexec/apache2/mod_headers.so
LoadModule ident_module /usr/libexec/apache2/mod_ident.so
LoadModule usertrack_module /usr/libexec/apache2/mod_usertrack.so
LoadModule setenvif_module /usr/libexec/apache2/mod_setenvif.so
LoadModule version_module /usr/libexec/apache2/mod_version.so
LoadModule proxy_module /usr/libexec/apache2/mod_proxy.so
LoadModule proxy_connect_module /usr/libexec/apache2/mod_proxy_connect.so
LoadModule proxy_ftp_module /usr/libexec/apache2/mod_proxy_ftp.so
LoadModule proxy_http_module /usr/libexec/apache2/mod_proxy_http.so
LoadModule proxy_scgi_module /usr/libexec/apache2/mod_proxy_scgi.so
LoadModule proxy_ajp_module /usr/libexec/apache2/mod_proxy_ajp.so
LoadModule proxy_balancer_module /usr/libexec/apache2/mod_proxy_balancer.so
LoadModule ssl_module /usr/libexec/apache2/mod_ssl.so
LoadModule mime_module /usr/libexec/apache2/mod_mime.so
LoadModule dav_module /usr/libexec/apache2/mod_dav.so
LoadModule status_module /usr/libexec/apache2/mod_status.so
LoadModule autoindex_module /usr/libexec/apache2/mod_autoindex.so
LoadModule asis_module /usr/libexec/apache2/mod_asis.so
LoadModule info_module /usr/libexec/apache2/mod_info.so
LoadModule cgi_module /usr/libexec/apache2/mod_cgi.so
LoadModule dav_fs_module /usr/libexec/apache2/mod_dav_fs.so
LoadModule vhost_alias_module /usr/libexec/apache2/mod_vhost_alias.so
LoadModule negotiation_module /usr/libexec/apache2/mod_negotiation.so
LoadModule dir_module /usr/libexec/apache2/mod_dir.so
LoadModule imagemap_module /usr/libexec/apache2/mod_imagemap.so
LoadModule actions_module /usr/libexec/apache2/mod_actions.so
LoadModule speling_module /usr/libexec/apache2/mod_speling.so
LoadModule userdir_module /usr/libexec/apache2/mod_userdir.so
LoadModule alias_module /usr/libexec/apache2/mod_alias.so
LoadModule rewrite_module /usr/libexec/apache2/mod_rewrite.so
LoadModule hfs_apple_module /usr/libexec/apache2/mod_hfs_apple.so

# These can also be turned into an Include. 
PidFile logs/httpd.pid
LockFile logs/httpd.lock

# Can be substituted for a variable in an Include.
Listen 8099
NameVirtualHost *:8099

# Just put this here to stop the startup error. Can be replaced with something else.
ServerName test

Include /etc/apache2/extra/httpd-autoindex.conf
Include /etc/apache2/extra/httpd-default.conf
Include /etc/apache2/extra/httpd-info.conf

<VirtualHost *:8099>
    DocumentRoot /Users/doe/site/public
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    ErrorLog logs/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

</VirtualHost>

This makes use of the system wide installation of Apache.

To fire up the instance:

export PATH=/usr/sbin:$PATH
httpd -f /Users/doe/site/conf/httpd.conf  -d `pwd` -T  -k start

Configuration for modules, ports listened to, php, pid and lock file locations, etc. could be moved to an outside include, which would be controlled by the administrator and not the user. You can then provide the user with a baseline template httpd.conf file, a simple start/stop/reload/etc. wrapper, and give all of your users the ability to fire up their own website.

Given that it's a Mac OS environment, the less you need to install, the more straightforward management will be.

Wing Tang Wong
  • 802
  • 4
  • 10