Simple script to add a new Virtual Host on Mac OSX
Posted by Andrea De Pirro in bash, Unix on June 23, 2011
Ideally when you develop a lot of websites on a local machine on OSX you have to create a virtual host for each website.
Below we will describe a really simple script to do this boring operation automatically.
Firstly a simple control to remember you have to choose a host name:
#!/bin/sh
if [ $# -lt 1 ]
then
echo
echo "You have to choose a host name!"
exit 0
fi
Then append a new host to the hosts file and verify it’s all right with a cat:
echo "\n" >> /etc/hosts
echo "127.0.0.1 $1" >> /etc/hosts
cat /etc/hosts
Now you can append the new Virtual Host to the apache configuration file httpd-vhosts.conf:
echo "\n" >> /etc/apache2/extra/httpd-vhosts.conf
echo "<VirtualHost *:80>" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerAdmin yourmail@email.com" >> /etc/apache2/extra/httpd-vhosts.conf
echo " DocumentRoot /Library/WebServer/Documents/$1/" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerName $1" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ErrorLog \"/private/var/log/apache2/$1-error_log\"" >> /etc/apache2/extra/httpd-vhosts.conf
echo " CustomLog \"/private/var/log/apache2/$1-access_log\" common" >> /etc/apache2/extra/httpd-vhosts.conf
echo " <Directory \"/Library/WebServer/Documents/$1/\">" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Options All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " AllowOverride All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Order allow,deny" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Allow from all" >> /etc/apache2/extra/httpd-vhosts.conf
echo " </Directory>" >> /etc/apache2/extra/httpd-vhosts.conf
echo "</VirtualHost>" >> /etc/apache2/extra/httpd-vhosts.conf
cat /etc/apache2/extra/httpd-vhosts.conf
Finally restart apache:
apachectl restart
This is the final script:
#!/bin/sh
if [ $# -lt 1 ]
then
echo
echo "You have to choose a host name!"
exit 0
fi
echo "\n" >> /etc/hosts
echo "127.0.0.1 " >> /etc/hosts
cat /etc/hosts
echo "\n" >> /etc/apache2/extra/httpd-vhosts.conf
echo "<VirtualHost *:80>" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerAdmin yourmail@email.com" >> /etc/apache2/extra/httpd-vhosts.conf
echo " DocumentRoot /Library/WebServer/Documents//" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerName " >> /etc/apache2/extra/httpd-vhosts.conf
echo " ErrorLog \"/private/var/log/apache2/-error_log\"" >> /etc/apache2/extra/httpd-vhosts.conf
echo " CustomLog \"/private/var/log/apache2/-access_log\" common" >> /etc/apache2/extra/httpd-vhosts.conf
echo " <Directory \"/Library/WebServer/Documents//\">" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Options All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " AllowOverride All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Order allow,deny" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Allow from all" >> /etc/apache2/extra/httpd-vhosts.conf
echo " </Directory>" >> /etc/apache2/extra/httpd-vhosts.conf
echo "</VirtualHost>" >> /etc/apache2/extra/httpd-vhosts.conf
cat /etc/apache2/extra/httpd-vhosts.conf
apachectl restart
download it here.
Once unzipped remember to edit the email on the script, and to make it executable with:
chmod +x newHost.sh
To execute the command you have to sudo (apache and the 2 files are root property):
sudo ./newHost chosenHostName
How to execute remote ssh commands without interactive password
Posted by Andrea De Pirro in bash, ssh, Unix on February 4, 2011
When you manage a lot of remote machines sometimes you need to write scripts to automate commands to be executed or open multiple shells to look at logs.
For security reasons SSH doesn’t give you a -p option to set a password to launch a session, but this can be circumvented with a command line tool called sshpass (you can find it in several repositories, Google is your friend). Mind you! SSH doesn’t give you this ability because is really difficult to store password in a secure way! So don’t blame me if your system will be compromised ![]()
Basically sshpass is a wrapper that add the -p option, so if you want to launch an ssh session just type this:
sshpass -p password ssh user@host
Sometimes you want to execute commands on the remote machine (like open a given folder) and keep the connection open. To do this just add the -t option and the command followed by the command bash separated by a semi-colon:
sshpass -p password ssh user@host -t "cd /some/folder/you/want/to/open;bash"
Another cool trick if you are under gnome is to open a separated terminal window:
gnome-terminal -e "sshpass -p password ssh user@host -t \"cd /some/folder/you/want/to/open;bash\""
You can even open multiple tabs in the same window with a title for each one:
gnome-terminal --tab -e "sshpass -p password ssh user@host1 -t \"cd /some/folder/you/want/to/open;bash\"" --title "Server1" --tab -e "sshpass -p password ssh user@host2 -t \"cd /some/folder/you/want/to/open;bash\"" --title "Server2"
For convenience you can add the –maximize option at the end to open the terminal full screen.
Just keep in mind that this method is quite unsecure, so use it at your own risk.
Alternative PHP Cache (APC)
Posted by Andrea De Pirro in PHP on October 23, 2009
The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.
Intermediate code is the internal memory structures produced during compilation that are fed into the executor. APC increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compiling is almost completely eliminated. APC stores and executes compiled PHP scripts from shared memory.

To activate APC on Windows with Xampp simply uncomment this string on the php.ini file in the php directory of Xampp. From:

To:

APC comes with useful graphical monitoring tool. It provides information about the APC state, cached files, amount of used and free memory, etc. Using this APC application, you can also clear all the caches without restarting Apache. This tool is also useful for tuning memory usage.
Access the application from a web browser via the URL http://localhost/apc.php.
The screenshot below shows the APC application:
Export tables from MS-Access to Mysql
Posted by Enrico Aillaud in Database on September 30, 2009
Note: The following guide was tested on WindowsXP, MS-Access 2007, Mysql ODBC connector 5.1
Prerequisites: Mysql and MS-Access installed in your localhost
Sometimes could happens that we are not familiar with some tools, in this case I was not familiar with MS-Access and I needed to export a table I need from Access to Mysql.
There are several tools which allow you to export from Access to Mysql but there is a very simple procedure you can perform to solve this issue very quickly using the ODBC driver.
So the first thing to do is to download the mysql connector from http://dev.mysql.com/downloads/connector/odbc/5.1.html accordingly with your OS.
After the install click start -> run and load the following command: control admintools; run Data Sources (ODBC) to set up your mysql database connection.
Under ‘User DSN’ tab click the ‘add’ button:
- select the MYSQL ODBC (version of the connector) Driver
- Edit the connection parameters to connect through ODBC driver to your database
If all worked pressing the ‘test’ button will confirm if the configuration settings are correct.
Open your mdb file with MS-Access:
- Right click on the table you want to export to mysql
- In the contextual menu selet the ‘export’ -> ‘ODBC Database’ option
- A new Export window will appear telling you the name of the table you want to export; press the ok button
- In the new window ‘Select Data Source’, select the ‘Machine Data Source’ and select the Mysql Database profile you created in the first steps of this guide to finish the import procedure.
After these commands, the database you selected in the last step of this guide, will be populated with the table selected.
Rollback to previous version with Tortoise SVN
Posted by Enrico Aillaud in Tortoise SVN on September 30, 2009
How to rollback to a previous SVN version with TortoiseSVN
- Update the repository
- Right click on your local repository and in the TortoiseSVN options select the Merge one
- In the new window select the option Merge a range of revisions and click next
- Edit the option URL to merge from with the address of the repository location (in this case http://your-typo3-repository)
- Edit the option Revision range to merge in the following way:
revision_from-revision_to (so 4-8 will merge from revision 4 to 8 ) - Check the option Reverse Merge and click next
- In the new window select Working Copy as Merge Depth and leave the default selection for the Radio Box
TYPO3 on Windows localhost with Subversion and XDebug
Posted by Enrico Aillaud in Typo3 on September 30, 2009
SVN Client side
Prerequisites: a remote svn repository with all your typo3 sources.
Download TortoiseSVN at http://tortoisesvn.net/downloads
Clicking on the file saved will automatically install the SVN client on the windows environment.
Download XAMPP at http://www.apachefriends.org/en/xampp-windows.html
Click on the saved file and an icon will ask where to extract the files; extract them to c:\Program Files.
The system will save the files into the C:\Program Files\xampp.
Open the folder and click the setup_xampp.bat; this will configure all files to work correctly in your system like the php.ini needed by apache web server.
Xampp default installation installs webserver Apache2 too.
Right click on the desktop, on the menu select the SVN Checkout and change these values:
- URL of Repository: http://your-svn-repository/repositoryname
- Checkout Directory: c:\…xampp\htdocs
Once the checkout ends, users can find all source code inside their working area in c:\…xampp\htdocs\
Setting Data Base Access:
In the foder typo3conf there is a file named localconf.php, open it and make the following changes:
$typo_db_username = “root”;
$typo_db_password = “”;
$typo_db_host = ‘localhost’;
$typo_db = ‘your-typo3-database‘;
Note: to rollback to a previous version with Tortoise SVN see our related post.
Installing Junction
Junction is a tool thank to which users can take advantage of symbolic link on window.
It is available to http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx and it is a simple exe file that will be executed when needed to create symbolic links typo3 needs to run without problem.
Junction configuration
After downloading the repository the following symbolic links where created to work with TYPO3:
-
junction -s c:htdocs c:Program Files\xampp\htdocs
-
junction -s c:Program Files\xampp\htdocs\t3lib\ c:Program Files\xampp\htdocs\typo3_src-version\t3lib
-
junction -s c:Program Files\xampp\htdocs\typo3\ c:Program Files\xampp\htdocs\typo3_src-version\typo3
Database settings
The structure of the production database is too heavy for a local environment so it was performed a dump of the production database only with CREATE statement avoiding so all the INSERT statements which would take too hard disk space problems and the query execution very slow.
The files created are stored in the subversion repository and to execute the batch file we need a Xampp installation with a working and a loaded mysql process.
You can create a mysqlbuild.bat. Clicking on it will execute all sql commands necessary to create the typo3 database structure.
The file will execute the following commands:
c:
CD C:\Program Files\xampp\mysql\bin
mysqladmin -u root create typo3-database
mysql -u root typo3-database < “C:\htdocs\sql_fixtures\structure.sql”
mysql -u root typo3-database < “C:\htdocs\sql_fixtures\be_users.sql”
mysql -u root typo3-database < “C:\htdocs\sql_fixtures\pages.sql”
mysql -u root typo3-database < “C:\htdocs\sql_fixtures\static_template.sql”
mysql -u root typo3-database < “C:\htdocs\sql_fixtures\sys_template.sql”
mysql -u root typo3-database < “C:\htdocs\sql_fixtures\tx_templavoila_datastructure.sql”
mysql -u root typo3-database < “C:\htdocs\sql_fixtures\tx_templavoila_tmplobj.sql”
(Note: the .sql files are store in the sql_fixtures folder)
Thank to this file a local copy of the database will be available for development purpose
XDEBUG – phpDesigner2008
- Note: the following settings could be applied with different IDEs.
- Download the Xdebug dll located in http://www.xdebug.org/download.php; the version you have to install is the 5.2 VC6 that will work fine with the latetest release of Xampp (In this guide we refer to Xampp Windows version 1.7.0)
Save the dll file into the folder C:\Program Files\xampp\php\ext\ - Go into the C:\Program Files\xampp\php folder
We need to modify here the php.ini in this way:
- In the [Zend] section comment all the lines putting a ; at the line beginning
- In the [Xdebug] section you must have this lines:
zend_extension_ts=”C:\Program Files\xampp\php\ext\php_xdebug-2.0.4-5.2.8.dll”
xdebug.remote_enable=true
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.profiler_enable=1
xdebug.profiler_output_dir=”C:\Program Files\xampp\tmp” - Copy and paste the C:\Program Files\xampp\php\php.ini into C:\Program Files\xampp\apache\bin (overwriting the existing php.ini)
- Open the C:\Program Files\xampp\xampp-control.exe and a tray icon will appear by which we can control xampp services like mysql and apache. To verify Xampp is working fine open a browser like Firefox and type http://localhost/xampp/; if installation goes fine you should see the xampp welcome page.
- Go in C:\Program Files\xampp\htdocs and move all existing files in another folder: you can perform this creating a new temporary folder under htdocs named /temp and cut and paste the files under /htdocs into /htdocs/temp.
Firefox configuration
- Download Firefox add-on located in https://addons.mozilla.org/en-US/firefox/addon/3960
- Once the installation is finished, in Firefox go to the menu Tools -> Add-ons and select the Options button under Xdebug Helper addon to access the setup page.
- In the first text box of the add-on be sure these lines are present:
zend_extension_ts=php_xdebug.dll
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.idekey=default - In the second text box insert phpDesigner2008. This key represents the Xdebug idekey value that allows firefox understanding which integrated development environment (IDE) we will use to start the debug mode (users can install also Eclipse, Netbeans, etc.)
- The extension will insert an icon in the status bar: clicking on it the linkage with Xdebug will be active or disabled.
If the icon is green firefox will run pages in debug mode.
NOTE:
It’s important to know that the debug mode runs from firefox. The first thing to do is to light the new icon present in the status bar.
Once the light became green the user has to insert into the address bar the link he wants to access in debug mode (e.g. 127.0.0.1/typo3/backend.php). Firefox doesn’t load the page until users presses F7 (debug mode to see work flow line by line) or F9 (debug mode to see where code breaks) in phpDesigner2008.
PhpDesigner2008 configuration
Create the project selecting the folder C:\Program Files\xampp\htdocs; phpDesigner will import all files located under the htdocs folder.
Open the menu Tools -> Preferences
Select Debugger and in the PHP panel insert
C:\Program Files\xampp\php\php-cgi.exe
under the Configuration panel insert
C:\Program Files\xampp\php\php.ini
Select Run (under debugger) and under the php panel write
C:\Program Files\xampp\php\php-cgi.exe
and under configuration panel insert
C:\Program Files\xampp\php\php.ini
Select localhost (under debugger) and under Server path write http://localhost while in Local Server Path write
C:\Program Files\xampp\htdocs
The port must be the 80.
Awstats installation on Red Hat 5.3
Posted by Enrico Aillaud in Unix on September 7, 2009
System requirements:
- A Web Server which must have access to log file had to analyse
- PERL
Steps for installing awstats
- Download the latest stable awstat source code (tar.gz) from:
http://sourceforge.net/projects/awstats/files/
and save it in a folder (i.e. /home/awstats)
- Go in the /home/awstat folder and uncompress the tar.gz file with the following command:
#tar xvf awstats-6.9.tar.gz
- After the decompression move the folder awstats-version in /var/www/html folder with the command:
#mv awstats-version /var/www/html/awstats
- go in the var/www/html/awstats/tools folder and type the following command:
#perl awstats_configure.pl
- Setup steps (the installation will ask for the following directives):
- Do you want to continue setup from this standard directory [yN]?
Type y and press enter to install in the /var/www/html folder - Enter full config file path of your web server.
Type none and press enter - Do you want me to build a new AWStats config/profile file [y/N]
Type y and press enter - What is the name of your web site or profile analysis?
Type the name of the website you want to analyze (ServerName) and press enter - In which directory do you plan to store your config file(s)?
Press enter to use the default one (etc/awstats) - Press enter until you return to shell prompt
- Do you want to continue setup from this standard directory [yN]?
- Create a folder in which store the log file you want to analyze:
#mkdir /etc/awstats/log_servername
- Copy the log file from the server in which are stored the apache access_log:
#scp root@ip_remote_server:/folderInWhichIsStoredAccessLog /etc/awstats/log
(i.e. #scp root@ip-remote-server:/var/log/apache2/access_log /etc/awstats/log)
- The system will ask for a password; insert the root password of the remote host.
- Edit the config file in /etc/awstats (in this case we create the mysite.conf file) with vi:
vi mysite.conf
- Go in the LogFile option and edit it to setup the right logfile that awstats has to analyze:
LogFile = “/etc/awstats/access_log” - Create the folder /var/lib/awstats
#mkdir /var/lib/awstats - Set the permissions in the folders created for apache:
#chown -R apache.apache /var/lib/awstats
#chown -R apache.apache /etc/awstats
#chown -R apache.apache /var/www/html/awstats
(If SELinux activated execute the following 2 commands)
#chcon -R -h -t httpd_sys_content_t /var/www/html/awstats/
#chcon -R -h -t httpd_sys_content_t /var/lib/awstats/ - go in /var/www/html/awstats/wwwroot/cgi-bin/ and type
#perl awstats.pl -config=mysite-update - restart apache:
#service httpd restart - Access from the browser the awstats statistics typing in the address bar:
localhost/awstats/awstats.pl?config=mysite
Notes for old log file:
A fresh awstats installation will not provide full statistics history but there is a way thank to which users could set up awstats to allow a full history statistics (previous log files must be available)
How load previous log file
(Usually previous log files are available in a compress format; in this case for the remote-host we suppose to have a bz2 file extension for old files)
- Copy the access_log.bz2 from the remote host to the local host running the command:
#scp root@remote-server:/var/log/apache2/access_log-*.bz2 /etc/awstats/log - Go in the following folder:
#cd /var/www/html/awstats/tools - Type:
#perl logresolvemerge.pl /folder/access_log-*.* > /folder/access_log - Run the command to update the log file awstats use to make the statistics:
cd /var/www/html/awstats/wwwroot/cgi-bin/
#perl awstats.pl -config=mysite -update
Note for access_log automatic update
In order to have an automatic update of the log file which resides in the remote host we set up a cron job which will update our local log file which resides in the /etc/awstats/log folder.
To accomplish this work we must avoid the password request in ssh setting up the private-public keys security.
Setup SSH without password
- In the localhost run ssh-keygen
- Write the folder in which save the key: /root/.ssh/id_rsa
- Rename id_rsa.pub:
#mv /root/.ssh/id_rsa.pub /root/.ssh/redhat.pub - Run:
scp /root/.ssh/id_rsa root@remote-server:/root/.ssh/ - Access remote server in ssh
- Go in the .ssh folder
#cd /root/.ssh - Type
#cat redhat.pub >> authorized_keys - Run the command:
#service sshd restart
After this setup the connection with the host will be without password
Updating access_log with crontab
#crontab -e
10 9 * * * scp root@remote-host:/var/log/apache2/access_log /etc/awstats/log
11 9 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=mysite –update
How to convert Squid log file to Apache format in order to use Awstats
Posted by Enrico Aillaud in Unix on September 3, 2009
The following guide is the result of a problem I met while trying to analyze old Squid files log with Awstats.
The problem was that the Squid option to write the log format in HTTPD common log file format was disabled and so it was impossible analyzed all previous Squid log files.
So I post this guide to explain how allow Awstats to analyze Squid log format formatted in native mode.
First of all there is a useful free software available at: http://pwebstats.gleeson.net/
The author is Martin Gleeson and he wrote the perl script we need to convert the squid log file.
Save the zipped application file, uncompress it and save the squid2common.pl file in /unix-folder/.
To convert the file simply run the perl script:
perl /unix-folder/squid2common.pl squid-access-log-file
The script will generate two different log files (cache.convert and proxy.convert), the proxy.convert one contains the information Awstats needs to make the reports.
Personally I run the scrip for all old Squid log file and I create a unique access_log file (set up in the awstats.site.com) needed by Awstats to make the statistics with the command:
less cache.convert >> access_log
Finally we must set up the correct LogFormat option on awstats.site.conf in order to allow Awstats to read the squid log file converted:
LogFormat=4
In this way, running the Awstats perl command to update the statistics, we will be able to have in our reports the old squid log files not directly processable by Awstats.
Enrico Aillaud
Upgrading Prestashop with rsync and SVN
Posted by Andrea De Pirro in Prestashop on August 21, 2009
Upgrading prestashop core is pretty straightforward, but when you have to deal with SVN metafiles some issues could arise. Normally you can follow instructions given by Prestashop authors, but we have to add some simple steps. First of all you have to install rsync (is installed by default in every unix distribution, including Mac OS X). Now download the latest version of Prestashop, unzip it, and change the folder name admin to the name you have given during Prestashop installation (tipically is something like admin542). Now all you have to do is synchronize the new version with the one installed in your local machine typing this command:
rsync -rlv --inplace path/to/new/prestashop path/to/local/prestashop
With this method you don’t have to worry about existing modules, tabs or translations, because everything is only updated, never deleted. You can launch the Prestashop installer going to http://local-installation-of-prestashop/install and follow the instruction to upgrade the core. Don’t forget to delete the install folder after the upgrade process. Now you’ll have some new files and a lot of modified files to be committed to the SVN repository. With this simple command you can add every file that is not present in the repository:
svn status |grep '\?' |awk '{print $2}'| xargs svn add


