I'm trying to update httpd.conf in my Cedar-based Heroku app. I got to my Heroku bash with
heroku run bash
and found the conf dir under apache. But when I try to open any editor vi, vim, or emacs, I can't find any of these programs. How do you edit conf files on Heroku?
I recently turned the original gist into a heroku cli plugin.
Just install:
heroku plugins:install https://github.com/naaman/heroku-vim
And use:
heroku vim
The heroku vim
command will drop you into a bash shell with vim
installed on your $PATH
. All you have to do is retrain your fingers to type heroku vim
instead of heroku run bash
.
If you don't want to mess around with plugins and just want a copy of nano in your one-off dyno, just run
mkdir /app/nano
curl https://github.com/Ehryk/heroku-nano/raw/master/heroku-nano-2.5.1/nano.tar.gz --location --silent | tar xz -C /app/nano
export PATH=$PATH:/app/nano
This will download a copy of nano from this plugin and put it in your PATH.
there's ed
if you're a masochist.
It looks like you can download and install vim for one session:
#!/usr/bin/env bash
curl https://s3.amazonaws.com/heroku-jvm-buildpack-vi/vim-7.3.tar.gz --output vim.tar.gz
mkdir vim && tar xzvf vim.tar.gz -C vim
export PATH=$PATH:/app/vim/bin
This idea was found here.
Even if you could edit the files with vi
it probably wouldn't solve your problem because the file system is ephemeral. Meaning... If you edit a file via heroku run bash
you aren't actually changing the file for other dynos. To change a file for all dynos you need to either change what you push in a Git repo or change the buildpack. More details:
https://devcenter.heroku.com/articles/oneoff-admin-ps#formation-dynos-vs-oneoff-dynos
Debugging on Heroku
Prepare the dyno
After installing naaman/heroku-vim
you can create a new ephemeral dyno via heroku vim
. As pointed out correctly by other posts you won't be able to see your changes when viewing through the browser because changes won't be propagated, but... you can actually view the changes from inside the dyno itself.
I've only experimented with "browsing" via curl, but if you could get lynx
on there, or better yet get an ssh tunnel -- could be really great.
Start the server
The web server won't be running when you instantiate heroku-vim so you'll need to do it yourself. In my example I'm running php:
~ $ cat Procfile
web: vendor/bin/heroku-php-apache2
You can start this command yourself!
~ $ vendor/bin/heroku-php-apache2 2>/dev/null &
[2] 845
It's now running in the background!
curl your website
Dynos start up on random ports. Luckily you know which one because it's the $PORT
variable!
~ $ curl localhost:$PORT
Hello World!
Editing
Do your vim thing now, but when you save the file and curl again - you won't see the changes. I don't understand where it's cached, but it's cached. You have to kill the server and restart it.
Restarting the server
Find the process id
~ $ ps -f
UID PID PPID C STIME TTY TIME CMD
u6897 3 1 0 05:34 ? 00:00:00 bash
u6897 582 3 0 05:53 ? 00:00:00 bash vendor/bin/heroku-php-apache2
u6897 652 582 0 05:53 ? 00:00:00 bash vendor/bin/heroku-php-apache2
u6897 653 582 0 05:53 ? 00:00:00 bash vendor/bin/heroku-php-apache2
Here 582
is the parent id -- use that.
kill 582
Wait just 1 second, and then start the server again (you'll get a new process id!). Curling via the same command will now give you the updated page.
In the comments on the Brian Takita's answer link, there is the more recent solution to get Vim working on the Heroku console:
https://gist.github.com/dvdbng/7375821b20f189c189ab1bd29392c98e
Just saved me a lot of time! :)
An urgent alternative to edit a file in Heroku:
- place a copy of it on some remote host. I like to use Gist
- edit the file on Gist and when finished get the raw URL to it
wget
the raw URL on your Heroku bash
- copy the fetched file to the path of original file
There are now a number of buildpacks that include vim: https://elements.heroku.com/search/buildpacks?q=vim
You could add one of these to the Heroku app in question, using support buildpack support.
I wrote a complete article on How to Edit a File on Heroku Dynos using Nano or Vim, but basically:
Hope it helps!
the alternative way if your server run php is to upload PHP File Manager, it single file and you can download it from
http://phpfm.sourceforge.net/
The plugin provided by Naaman Newbold is no longer working with heroku-16
stack, so I made a new plugin out of this updated gist.
Install:
heroku plugins:install @jasonheecs/heroku-vim
And use:
heroku vim