Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

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

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.

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

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

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top