add lines to the apache2 confing - linux

I need to add this to my apache2.conf in my VPS:
Include /etc/phpmyadmin/apache.conf
extension=mysql.so
extension=memcache.so
extension=mbstring.so
extension=gd.so
extension=mcrypt
After I add this and save the apache.conf, and trying to restart the apache, i get an error - Failed.
Why?

You find that error because you put php directives in apache.

You have to add these lines in php.in file.
you can find php.ini file location using,
#php -i | grep php.ini

Related

Running a Script to change a wp-config file DB Name

I teach students how to fix wordpress sites and I would like to write a script that looks at what information they have in their wp-config.php and add a single letter or number to the database name.
For example the line is such
define('DB_NAME', 'cpanelUser_NameofDB');
I would like to add a number or a line to the end of NameofDB
I can use this to isolate the cpanelUser_NameofDB
grep -i 'DB_NAME' wp-config.php | cut -d"'" -f4
but I'm not sure how to add information, nor if this is the correct script I should run to get there. I would also like it to not matter what the name of the database is since it will be ran on multiple sites. I'm sure I could use regex but I'm not too versed in that and would not know where to start. Help please!
You can use WP CLI on the server:
Go to the website root:
cd /var/www/mysite.com/htdocs
...and list all the wp-config values:
wp config list
...or set the value you need to change:
sudo wp config set DB_NAME put_my_custom_db_name_here --allow-root
See more WP CONFIG features here:
https://developer.wordpress.org/cli/commands/config/
You can use sed for this purpose to fix this line:
#!/bin/bash
id=34
sed -i "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" wp-config.php
flag -i means that we do change in file directly.
Alternatively you can make template file and generate new file while sed works on stdin and stdout
#!/bin/bash
id=34
cat wp-config.php-template| sed "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" > wp-config.php.$id

replace a text inside a block of code in apache2.conf file using sed

I have been playing with some tricks of useful sed command in linux to replace a word of text inside a block of code in apache2.conf file. Tried searching for already answered solutions on google and stackoverflow. But, those couldn't really get help.
Here is the piece of code in apache2.conf file.
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
i want the the word denied inside the block to be chnanged to granted. Like below,
<FilesMatch "^\.ht">
Require all granted
</FilesMatch>
I'm trying with the command
sudo sed -i "/<IfModule "^\.ht">/,\#</IfModule># s/Require all denied/Require all granted/" /etc/apache2/apache2.conf
and i am sure i am somewhere wrong at the the double quotes used inside the opening tag .
any help would be really appreciated.
Thanks in advance.
sed is for doing s/old/new on individual lines, that is all. For anything else you should be using awk:
$ awk '/<FilesMatch "\^\\\.ht">/{f=1} f{sub(/denied/,"granted")} /<\/FilesMatch>/{f=0} 1' file
<FilesMatch "^\.ht">
Require all granted
</FilesMatch>
The above just sets a flag when the start line is found, clears it when the end line is found, and changes denied to granted when that flag is set.
sed -i '/<FilesMatch "^\\.ht">/,\#</FilesMatch># s/Require all denied/Require all granted/' /etc/apache2/apache2.conf

Gearman: troubles with first use

I tried to start with Gearman. After downloading and setting it, gearman_version() works. But, when I start server and try to init worker like so:
php myFileName.php &
I see the code:
And when I init the client, I see code too. What am I doing wrong?
i don`t know why, but the examples from youtube was not correct in my case. The first scripts, which worked I get from http://php.net/manual/ru/gearman.examples-reverse-bg.php
You likely have short open tags disabled on your install. Notice, running a php file that doesn't actually contain any PHP will just echo the contents of the file.
>$ echo 'hello' > text.php
>$ php text.php
hello
>$
You can verify the setting for your install with the following
>$ php -i | grep "short_open_tag"
short_open_tag => On => On
If tags are On you're all set.

php path, cron and cpanel

I've spent days still can't figure out this.
I have following file structure under public_html:
cron_jobs/file.php contains - > include('../base/basefile.php')
base/basefile.php contains - > include('baseSubFile.php')
when I run
/pathtophp/php -f ~/public_html/cron_jobs/file.php
it works ok but when I copy the same command to cron in cpanel, I get error saying
'basesubfile.php' can't be found
Please help.
Cron won't run from the same directory as your php file is in, so you'll need to change to it first:
cd /home/user/public_html/cron_jobs/ && /pathtophp/php -f file.php
I recommend the full path versus ~ when dealing with cron scripts to avoid confusion
You should used
include dirname( __FILE__ ) . '/../base/basefile.php';
and
include dirname( __FILE__ ) . '/baseSubFile.php';
The function dirname returns parent directory's path
Simply put this at the top of your PHP script:
chdir(dirname(__FILE__));

Find hosted directories Jetty/Apache

Let say I have a directory which is being hosted by Jetty or Apache (i'd like an answer for both), i know the URL including the port and i can log into the server.
How can i find the directory that is being hosted by a certain port?
I'd also like to go the other way, i have a folder on the server, which i know if being hosted, but i don't know the port so i can't find it in a web browser.
How can i find a list of directories that are being hosted?
This has been bugging me for ages but i've never bothered to ask before!
Thanks.
This is the way how to find it out for Apache. Lets say you have an URL http://myserver.de:8081/somepath/index.html
Step 1: Find the process that has the given port open
You can do this by using lsof in a shell of the server, which lists open files (and ports) as well as the processes associated to it:
myserver:~ # lsof -i -P | grep LISTEN | grep :80
apache2 17479 root 4u IPv6 6271473 TCP *:80 (LISTEN)
We now know there is a process called "apache2" with process ID 17479
Step 2: Find out more about the process
We can now look at the environment of the process, where more information should be available:
myserver:~ # (cat /proc/17479/environ; echo) | tr "\000" "\n"
PATH=/usr/local/bin:/usr/bin:/bin
PWD=/
LANG=C
SHLVL=1
_=/usr/sbin/apache2
Okey, the process executable is /usr/sbin/apache2. Now lets look at the command line.
myserver:~ # (cat /proc/17479/cmdline; echo) | tr "\000" " "
/usr/sbin/apache2 -k start
Step 3: Finding the config of the process
Our previous examination has shown that no special configuration file has been given at the command line with the -f option, so we have to find the default location for that process. This depends on how the default command line is compiled into the apache2 executable. This could be extracted from it somehow, but obviously its the default location for Apache 2 on my machine (Debian Etch), namely /etc/apache2/apache2.conf.
Step 4: Examining the Apache config file
This again needs some knowledge about apache configurations. The config file can include other files, so we need to find those first:
myserver:~# cat /etc/apache2/apache2.conf | grep -i ^Include
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
Include /etc/apache2/httpd.conf
Include /etc/apache2/ports.conf
Include /etc/apache2/conf.d/
Include /etc/apache2/sites-enabled/
A nice list. These configs tell evetything about your configuration, and there are many options that might map files to URLs. In particular apache can serve different directories for different domains, if those domains are all mapped to the same IP. So lets say on your server you host a whole bunch of domains, then "myserver.de" is either mapped by the default configuration or by a configuration that serves this domain specifically.
The most important directives are DocumentRoot, Alias and Redirect. On my system the following gives a quick overview (comments omitted):
myserver:~# ( cat /etc/apache2/apache2.conf; cat /etc/apache2/sites-enabled/* ) | grep 'DocumentRoot\|Alias\|Redirect'
Alias /icons/ "/usr/share/apache2/icons/"
DocumentRoot /var/www/
RedirectMatch ^/$ /apache2-default/
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
Alias /doc/ "/usr/share/doc/"
DocumentRoot /var/www/
RedirectMatch ^/$ /apache2-default/
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
Alias /doc/ "/usr/share/doc/"
Since the "mypath" part of the URL has no direct match, I can savely assume it lies below the DocumentRoot /var/www/, so the result of my search is that
http://myserver.de:8081/somepath/index.html --> /var/www/mypath/index.html
You can do a lookup in a similar way for jetty.
As a convention you could maintain a document detailing all the filesystem directories and corresponding URLs that are created. This will ansewr quetsions of file>URL and URL->file mappings, and is also useful for planning, resource management and security reviews.
What follows is food for thought rather than any serious proposal. My "proper" answer is to use good documentation. However...
An automated approach might be possible. Thinking freely (not necessarily practically!) you could find/create an Apache module/jetty extension to add a small virtual file to each web directory as it is served from the filesystem. The contents of that virtual file would contain the location of the directory on the server the files are served from as well as maybe internal server name, IP or other details to help bridge the gap from what you see on the web side and where it is in your intranet.
Mapping files to URLs is tricky. You might be able to automate it by scanning the http access logs, which is configured with a custom logger that logs an entry when a file is served. By scanning the URL accessed and corresponding file served, you can map files back to URLs. (You also get the URL->file mapping, in case you don't want to manually browse the URL as I outlined in the paragraph above.)
apache2ctl -S
or (for old computers):
apachectl -S
You'll see vhosts files with lines where these sites are described. There you can look for directories.

Resources