Vim: Sudo Edit Breaks Apache Syntax Highlighting - apache

Sudo Edit (sudo -e) allows unprivileged users to edit files securely. To achieve this, it makes a temporary copy of a file to edit and then copies it over when editing is done.
When I'm editing an apache file (eg, /etc/apache2/sites-enabled/mysite.com), sudoedit vim can't figure out that it should use Apache syntax highlighting, so I have to manually :set syntax=apache. I suspect that Vim's rule for syntax highlighting relies on the full path of the file, and since sudoedit changes the file to something like /var/tmp/mysiteRANDOMCHARS.com, it loses that path information.
Is there any way for me to automatically tell Vim that it should use apache syntax highlighting?
Thanks!

This sounds like one of the rare use-cases for the vi modeline feature:
In your .vimrc:
set modeline
And in your Apache config file, somewhere at the top or the bottom:
# vi: syntax=apache

See /usr/share/vim/vim7x/filetype.vim for how Vim determines that a file is an apache file.
In my config, using $ vim or $ sudo -e makes no difference as the .com extension is treated as dcl anyway.
I don't see a smart and solid way to workaround that beside overriding the .com autocommand in your ~/.vimrc:
au BufNewFile,BufRead *.com set ft=apache

Related

Windows 10 - Apache 2.4.53 + PHP 8.0.19 not loading php modules [duplicate]

I have found that:
When I type the following on terminal:
php -i | grep php.ini
I get the output:
The Loaded Configuration file is # /etc/php5/cli/php.ini
However, from phpinfo(), I get to see:
The loaded ini file is # /etc/php5/apache2/php.ini
Which one of these is working right now? How is it possible to have two php.ini files ?
Depends on where you are running PHP from. If you run it from command line, it uses the cli/php.ini and apache2/php.ini when run through apache.
You are executing phpinfo() through the browser, hence you get /etc/php5/apache2/php.ini as the answer. Running php -r "phpinfo();" | grep "Loaded Configuration" from the terminal should output the CLI ini. Same function, context changes.
The advantage of this system is obviously to allow different configurations depending on the context. For a simplified example, you might want to have safe_mode on in apache but it's unnecessary in CLI mode.
Your .ini paths are actually quite unusual. Normally, the default .ini is just php.ini and CLI .ini is called php-cli.ini and they reside in the same folder.
I'm no expert on the subject but this should be the basic idea. If anyone has any corrections, I'd be happy to hear them.

How to remove custom vim syntax and filetype settings for a specific

I somehow botched the default syntax highlighting and filetype recognition for my apache httpd-vhosts.conf file, such that every time I open it in vim, the syntax and filetype are both set to "conf". All of the other files in the ../conf.d/ directory are recognized properly, i.e. as syntax and filetype both set to "apache". I've tried the following:
manually setting both in vim, i.e.
:set syntax=apache, :set filteype=apache
This works for the session, but they both reset back to "conf" when the file is reopened
Looking for the ~/.vimrc
I don't seem to have one, since I typically edit this file as root
yum remove and reinstall of vim
no change.
I know that there are a lot of posts regarding similar issues with vim syntax and filetype problems, but I cannot figure out how to resolve mine. My guess is that there is a mapping somewhere that says this one particular file should be recognized as a "conf", but that's all I've got. tia.
edit: to clarify, what I would like to know is how this one file got "stuck" on the "conf" syntax and filetype, whereas all of the other .conf files are "apache" syntax and filetype.
I would recommend using a modeline. Put this at the top of your file:
# vim: ft=apache
If you do not have a .vimrc file, you can create one. Keep in mind that each user has a separate .vimrc in their home folder. You can also look for the system default vimrc (without a dot) located in your vim installation folder. To find out where that is, type :echo $VIM in vim.
If you do create a new .vimrc for your user, you can set all .conf files to have apache highlighting as follows:
au BufRead,BufNewFile *.conf set filetype=apache

Attach bash script to apache start

I have a bash script that I want to be executed before apache starts or restarts.
I want my bash script to be executed when apache starts during the bootup process and when I manually run "/etc/init.d/apache2 restart/start".
There is an init.d script "/etc/init.d/apache2" but I rather not touch that file.
Google is not very helpful :)
Because of the way /etc/init.d/apache2 is written, you can't hijack it by putting your script ahead of apache2ctl in the PATH and modifying or renaming /usr/sbin/apache2ctl would be more likely to get undone during an update. So that leaves you with a choice of modifying /etc/init.d/apache2 or magic.
It may be that the magic comes in the form of creating a symlink to your script in the appropriate /etc/rc?.d directories with an appropriate prefix that would cause it to run before Apache. On my system, the name might be S88scriptname, for example. You can make these links individually for each runlevel and manage them manually or, on systems such as Debian and Ubuntu that support it, you can model your script after /etc/init.d/skeleton and set the options in the LSB header appropriately (particularly the X-Start-Before keyword, perhaps) and use update-rc.d to manage the rc?.d symlinks for you.

How to unload all the plugins from vim and change VIMRUNTIME?

I have an account at my hosting providers server and I can't install my own copy of vim.
So the only personalization I can make is editing .vimrc in my account, but it won't suffice
What I'd Like to do is:
on startup I'd like to unload all the plugins and loaded stuff, and tell vim to use other folder as its' runtime.
Any idea how to aproach it?
You can start your vim with -u NONE. From the man-page:
-u {vimrc} Use the commands in the file {vimrc} for initializations.
All the other initializations are skipped. Use this to
edit a special kind of files. It can also be used to skip
all initializations by giving the name "NONE". See ":help
initialization" within vim for more details.
For changing $VIMRUNTIME at runtime, use
:let $VIMRUNTIME = "/new/path/"
To answer your question: you just need to set the runtimepath option in your .vimrc file, because your .vimrc is read before any plugins are loaded.
However, the default runtimepath value usually includes the ~/.vim folder (use :set runtimepath? to check), so you should be able to add whatever plugins you need to the ~/.vim folder. Also you can prevent many plugins from loading by adding a line such as :let g:loaded_<plugin name> = 1 to your .vimrc file. Check the help docs for each plugin to find out how to do this.

How to check where Apache is looking for a php.ini file?

I have a feeling that Apache is using a different php.ini file that the one I am editing. I make a change in my php.ini file, restart Apache, and the change doesn't take affect. So I don't know what to do anymore.
Any ideas?
Update: Found out it's using the right php.ini file...but I still don't know what to do!
To find the file that's being run by PHP, create a phpinfo file (just <?php phpinfo();?>) and look for the 'Configuration File (php.ini) Path' directive.
from the command line, run
php -i |grep "php.ini"
This will describe the location php is loading its ini file from. You can reconfigure the php.ini location by recompiling php.
The output from phpinfo() will contain this. When using PHP as an Apache module, it can be configured using PHPIniDir in httpd.conf (or similar).
To get the php.ini file which is being used by Apache you will probably have to add phpinfo() into a .php file and open it in the browser. As php -r "phpinfo();" | grep php.ini will outout the same as php --ini would. Which php.ini is used for the CLI.
Question for you, what platform are you running on unix or windows?
If it is unix based, check if your php.ini is residing in the same directory as /etc/httpd. Again, installation of apache can vary so check...or issue the command "find / -name php.ini -print" (without quotes) to see which one is it you are using
Ok. Since you said you have found the correct php.ini, sounds like something is missing when you edited the php.ini and reloaded apache. Look in the log directory /var/log/httpd for error_log and check to see if there was errors...that would be a start!