tag:blogger.com,1999:blog-89992208625533905922024-03-05T14:22:23.357+05:00Linux & Web Development Geek - Tips & TricksPHP,Joomla,WordPress & Linux based server management, error resolutions and fixing problems. Unknownnoreply@blogger.comBlogger30125tag:blogger.com,1999:blog-8999220862553390592.post-9247309492095484112016-11-17T14:37:00.000+05:002016-11-17T14:37:19.225+05:00Linux: How to find - Size of a directory & Free disk space<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonDisplay, Georgia, serif; font-weight: 400; line-height: 1.3em; margin: 0px 0px 5px; text-align: left;">
<br /></h2>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonDisplay, Georgia, serif; font-weight: 400; line-height: 1.3em; margin: 0px 0px 5px; text-align: left;">
Linux Server Management Tips . (File & Directory)</h2>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; font-weight: 700;">Tip.1 - Finding the size of a directory</span></div>
<div>
<span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; font-weight: 700;"><br /></span></div>
<div>
<span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; font-weight: 700;"><div style="box-sizing: border-box; font-weight: normal; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">$ du</span><br style="box-sizing: border-box;" />Typing the above at the prompt gives you a list of directories that exist in the current directory along with their sizes. The last line of the output gives you the total size of the current directory including its subdirectories. The size given includes the sizes of the files and the directories that exist in the current directory as well as all of its subdirectories. Note that by default the sizes given are in kilobytes. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /></span><span style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">$ du /home/david</span><br style="box-sizing: border-box;" />The above command would give you the directory size of the directory /home/david<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-weight: 700;">$ du -h</span><br style="box-sizing: border-box;" />This command gives you a better output than the default one. The option '-h' stands for <i style="box-sizing: border-box;">human readable format</i>. So the sizes of the files / directories are this time suffixed with a 'k' if its kilobytes and 'M' if its Megabytes and 'G' if its Gigabytes.</span></div>
<div style="box-sizing: border-box; font-weight: normal; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;"><br style="box-sizing: border-box;" />$ du -ah</span><br style="box-sizing: border-box;" />This command would display in its output, not only the directories but also all the files that are present in the current directory. Note that 'du' always counts all files and directories while giving the final size in the last line. But the '-a' <i style="box-sizing: border-box;">displays</i>the filenames along with the directory names in the output. '-h' is once again human readable format.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /></span><span style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">$ du -c</span><br style="box-sizing: border-box;" />This gives you a <i style="box-sizing: border-box;">grand total</i> as the last line of the output. So if your directory occupies 30MB the last 2 lines of the output would be<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />30M .<br style="box-sizing: border-box;" />30M total<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The first line would be the default last line of the 'du' output indicating the total size of the directory and another line displaying the same size, followed by the string '<i style="box-sizing: border-box;">total</i>'. This is helpful in case you this command along with the grep command to only display the final total size of a directory as shown below.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-weight: 700;">$ du -ch | grep total</span><br style="box-sizing: border-box;" />This would have only one line in its output that displays the total size of the current directory including all the subdirectories.</span></div>
<div style="box-sizing: border-box; font-weight: normal; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><br /></span></div>
<div style="box-sizing: border-box; font-weight: normal; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">$ du -s</span><br style="box-sizing: border-box;" />This displays a summary of the directory size. It is the simplest way to know the total size of the current directory.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /></span><span style="box-sizing: border-box; font-weight: 700;"><span style="box-sizing: border-box;">$ du -S</span></span><span style="box-sizing: border-box;"><br style="box-sizing: border-box;" />This would display the size of the current directory excluding the size of the subdirectories that exist within that directory. So it basically shows you the total size of <i style="box-sizing: border-box;">all the files</i> that exist in the current directory.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /></span><span style="box-sizing: border-box; font-weight: 700;"><span style="box-sizing: border-box;">$ du --exculde=mp3</span></span><span style="box-sizing: border-box;"><br style="box-sizing: border-box;" />The above command would display the size of the current directory along with all its subdirectories, <i style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 700;">but</span></i> it would exclude all the files having the given pattern present in their filenames. Thus in the above case if there happens to be any mp3 files within the current directory or any of its subdirectories, their size <i style="box-sizing: border-box;">would not be included</i> while calculating the total directory size.</span></span></div>
<div style="box-sizing: border-box; font-weight: normal; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><span style="box-sizing: border-box;"><br /></span></span></div>
<div style="box-sizing: border-box; font-weight: normal; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><span style="box-sizing: border-box;">Tip 2.F</span></span><span style="font-weight: 700;">inding the disk free space / disk usage</span></div>
<div style="box-sizing: border-box; font-weight: normal; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="font-weight: 700;"><br /></span></div>
<div style="box-sizing: border-box; font-weight: normal; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 700;">$ df</span><br style="box-sizing: border-box;" />Typing the above, outputs a table consisting of 6 columns. All the columns are very easy to understand. Remember that the 'Size', 'Used' and 'Avail' columns use kilobytes as the unit. The 'Use%' column shows the usage as a percentage which is also very useful.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-weight: 700;">$ df -h</span><br style="box-sizing: border-box;" />Displays the same output as the previous command but the '-h' indicates <i style="box-sizing: border-box;">human readable format</i>. Hence instead of kilobytes as the unit the output would have 'M' for Megabytes and 'G' for Gigabytes. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Most of the users don't use the other parameters that can be passed to 'df'. So I shall not be discussing them.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />I shall in turn show you an example that I use on my machine. I have actually stored this as a script named <i style="box-sizing: border-box;">'usage</i>' since I use it often. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-weight: 700;">Example :</span><br style="box-sizing: border-box;" />I have my Linux installed on /dev/hda1 and I have mounted my Windows partitions as well (by default every time Linux boots). So 'df' by default shows me the disk usage of my Linux as well as Windows partitions. And I am only interested in the disk usage of the Linux partitions. This is what I use :<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-weight: 700;">$ df -h | grep /dev/hda1 | cut -c 41-43</span><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />This command displays the following on my machine<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-weight: 700;">45%</span><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Basically this command makes 'df' display the disk usages of all the partitions and then extracts the lines with /dev/hda1 since I am only interested in that. Then it cuts the characters from the 41st to the 43rd column since they are the columns that display the usage in % , which is what I want.</div>
</span></div>
<div>
<br /></div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-77721191260712236272016-11-17T14:33:00.001+05:002016-11-17T14:33:28.401+05:00Export & Import all mysql databases at one time.<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">Most of the times you want to keep a backup of all of your MySQL databases. Suppose you have more than 100 MySQL databases & you want to export all of them at the same time and again import all of them into MySQL server at one time. How would you do that?</span><br />
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">The answer ....</span><br />
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">Go to the command line : </span><br />
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 0px;">
How to export all mysql databases at once:</h2>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">mysqldump -u root -p --all-databases > alldb.sql</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Edit the root and enter the password for root to export all the databases to alldb.sql file, notice that this file will be generated at the same place where you are working in shell from.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<h2 style="box-sizing: border-box; color: inherit; font-family: inherit; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
How to Import all mysql databases at once:</h2>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">mysql -u root -p < alldb.sql</span></code></pre>
</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<div style="box-sizing: border-box;">
Edit the user root if necessary and enter the password for root to import all the databases from alldb.sql file to mysql.</div>
<div style="box-sizing: border-box;">
<br /></div>
<div style="box-sizing: border-box;">
<br /></div>
<div style="box-sizing: border-box;">
Good luck!!</div>
</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-47567270465636042732016-11-14T17:18:00.000+05:002016-11-17T14:38:59.379+05:00Too many redirects occurred trying to open localhost - Joomla! [SOLVED]<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<span style="background-color: white;"><span style="font-family: "lyontext" , "georgia" , serif;"><span style="font-size: 18px;">How to fix Too many redirects occurred Error in Joomla!</span></span></span></h2>
<span style="background-color: white; font-family: "lyontext" , "georgia" , serif; font-size: 18px;"><br /></span>
<br />
<h3 style="text-align: left;">
<span style="background-color: white; font-family: "lyontext" , "georgia" , serif; font-size: 18px;">Too many redirects occurred trying to open </span>http://mywebsite.com/Joomla_3/index.php<span style="background-color: white; font-family: "lyontext" , "georgia" , serif; font-size: 18px;"> " how do I fix this?</span></h3>
<div>
<span style="background-color: white; font-family: "lyontext" , "georgia" , serif; font-size: 18px;"><br /></span></div>
<div>
<span style="font-family: "lyontext" , "georgia" , serif;"><span style="background-color: white; font-size: 18px;">Fix:</span></span></div>
<div>
<span style="font-family: "lyontext" , "georgia" , serif;"><span style="background-color: white; font-size: 18px;"><br /></span></span></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
You need to change the home menu item so it points to a published article. Assuming the home menu item is of type single article and is not pointing to a published article.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
Issue with migrating? Include logs/joomla_update.php in your report!<br />
Blank screen? Verify pagesource for HTML code (javascript error)<br />
Installation failing on populating database? Install with set_time_limit(0)<br />
Document your customizations!</div>
</div>
<div>
<span style="background-color: white; font-family: "lyontext" , "georgia" , serif; font-size: 18px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: "lyontext" , "georgia" , serif; font-size: 18px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: "lyontext" , "georgia" , serif; font-size: 18px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: "lyontext" , "georgia" , serif; font-size: 18px;"><br /></span></div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-68820179898053672082016-11-14T17:12:00.000+05:002016-11-17T14:39:36.155+05:00PHP Send data to browser, but keep executing script in background<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
How to echo or print PHP script while its still executing?</h2>
<div>
<br /></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
ob_flush writes the buffer. In other words, ob_flush tells PHP to give Apache (or nginx/lighttpd/whatever) the output and then for PHP to forget about it. Once Apache has the output, it does whatever it wants with it. (In other words, after ob_flush it's out of your control whether or not it gets immediately written to the browser).</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
So, short answer: There's no guaranteed way to do that.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Just a guess, you're likely looking for AJAX. Whenever people are trying to manipulate when page content loads as you're doing, AJAX is almost always the correct path.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you want to continue a task in the background, you can use ignore_user_abort, however, that is often not the optimal approach. You essentially lose control over that thread, and in my opinion, a web server thread is not where heavy processing belongs.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I would try to extract it out of the web facing stuff. This could mean a cron entry or just spawning a background process from inside of PHP (a process that though started from inside of script execution will not die with the script, and the script will not wait for it to finish before dying).</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you do go that route, it will mean that you can even make some kind of status system if necessary. Then you could monitor the execution and give the user periodic updates on the progress. (Technically you could make a status system with a ignore_user_abort-ed script too, but it doesn't seem as clean to me.)</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I have done this in the past and this is how I solved it:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">ob_start</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/*
* Generate your output here
*/</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// Ignore connection-closing by the client/user</span><span style="box-sizing: border-box;">
ignore_user_abort</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">true</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// Set your timelimit to a length long enough for your script to run, </span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// but not so long it will bog down your server in case multiple versions run </span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// or this script get's in an endless loop.</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">if</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">!</span><span style="box-sizing: border-box;">ini_get</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'safe_mode'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">&&</span><span style="box-sizing: border-box;"> strpos</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">ini_get</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'disable_functions'</span><span style="box-sizing: border-box;">),</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">'set_time_limit'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> FALSE
</span><span style="box-sizing: border-box;">){</span><span style="box-sizing: border-box;">
set_time_limit</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">60</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// Get your output and send it to the client</span><span style="box-sizing: border-box;">
$content </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> ob_get_contents</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Get the content of the output buffer</span><span style="box-sizing: border-box;">
ob_end_clean</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Close current output buffer</span><span style="box-sizing: border-box;">
$len </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> strlen</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$content</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Get the length</span><span style="box-sizing: border-box;">
header</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'Connection: close'</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Tell the client to close connection</span><span style="box-sizing: border-box;">
header</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">"Content-Length: $len"</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Close connection after $len characters</span><span style="box-sizing: border-box;">
echo $content</span><span style="box-sizing: border-box;">;</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Output content</span><span style="box-sizing: border-box;">
flush</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Force php-output-cache to flush to browser.</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// See caveats below.</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// Optional: kill all other output buffering</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">while</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">ob_get_level</span><span style="box-sizing: border-box;">()</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">0</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">
ob_end_clean</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-66795398650620993582016-11-14T11:18:00.004+05:002016-11-14T11:18:43.164+05:00Understanding font file types for HTML/CSS<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Understanding Font File Types</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The original snippet at the top of my previous post references a lot of files with strange extensions. Let's go over each one and get a better understanding of what they mean.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
WOFF / WOFF2</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Stands for:</span> Web Open Font Format.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Created for use on the web, and <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/WOFF" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">developed by Mozilla</a> in conjunction with other organizations, WOFF fonts often load faster than other formats because they use a compressed version of the structure used by OpenType (OTF) and TrueType (TTF) fonts. This format can also include metadata and license info within the font file. This format seems to be the winner and where all browsers are headed.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="http://dev.w3.org/webfonts/WOFF2/spec/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">WOFF2</a> is the next generation of WOFF and boasts better compression than the original.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
SVG / SVGZ</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Stands for:</span> Scalable Vector Graphics (Font)</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
SVG is a vector re-creation of the font, which makes it much lighter in file size, and also makes it ideal for mobile use. This format is the only one allowed by version 4.1 and below of Safari for iOS. SVG fonts are not currently supported by Firefox, IE or IE Mobile. <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_fonts" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Firefox has postponed implementation</a> indefinitely to focus on WOFF.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
SVGZ is the zipped version of SVG.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
EOT</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Stands for:</span> Embedded Open Type.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This format was created by Microsoft (the original innovators of <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">@font-face</code>) and is a proprietary file standard supported only by IE. In fact, it’s the only format that IE8 and below will recognize when using <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">@font-face</code>.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
OTF / TTF</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Stands for:</span> OpenType Font and TrueType Font.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The WOFF format was initially created as a reaction to OTF and TTF, in part, because these formats could easily (and illegally) be copied, However, OpenType has capabilities that many designers might be interested in (<a href="http://www.magnetstudio.com/words/2010/opentype-guide" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">ligatures and such</a>).</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
A Note on Performance</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Web fonts are great for design but it's important to also understand their impact on web performance. Custom fonts often cause sites to take a performance hit because the font must be downloaded before it's displayed.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
A common symptom <em style="box-sizing: border-box;">used to be</em> a brief moment where fonts first load as the fallback, then blink to the downloaded font. <a href="http://www.paulirish.com/2009/fighting-the-font-face-fout/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Paul Irish has an older post on this</a> (dubbed "FOUT": Flash Of Unstyled Text).</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<em style="box-sizing: border-box;">These days</em>, browsers are generally hiding the text before the custom font loads by default. Better or worse? You decide. You can exert some control over this through various techniques. A little out-of-scope for this article, but here's a trifecta of articles by Zach Leatherman to get you started down the rabbit hole:</div>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;"><a href="https://dev.opera.com/articles/better-font-face/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Better @font-face with Font Load Events</a></li>
<li style="box-sizing: border-box;"><a href="http://www.filamentgroup.com/lab/font-loading.html" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">How we use web fonts responsibly, or, avoiding a @font-face-palm</a></li>
<li style="box-sizing: border-box;"><a href="http://www.zachleat.com/web/foft/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Flash of Faux Text—still more on Font Loading</a></li>
</ul>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Here are some more considerations when implementing custom fonts:</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Watch the file size</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Fonts can be surprisingly heavy, so lean towards options that offer lighter versions. For example, favor a font set that is 50KB versus one that weighs 400KB.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Limit the character set, if possible</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Do you really need the bold and black weights for one font? Limiting your font sets to load only what is used is a good idea and there are some <a href="http://thenewcode.com/878/Slash-Page-Load-Times-With-CSS-Font-Subsetting" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">good tips on that here</a>.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Consider system fonts for small screens</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Many devices are stuck on crappy connections. One trick might be to target larger screens when loading the custom font using <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">@media</code>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
In this example, screens smaller than 1000px will be served a system font instead and screens that wide and above will be served the custom font.</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;">@media</span> <span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">min-width</span><span style="box-sizing: border-box;">:</span> 1000px<span style="box-sizing: border-box;">)</span></span> <span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">body </span><span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">font-family</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">'FontName'</span>, Fallback, sans-serif<span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">}</span>
<span style="box-sizing: border-box;">}</span></code></pre>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Font Services</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
There are a number of services that will host fonts and provide access to commercially-licensed fonts as well. The benefits of using a service often boil down to having a large selection of legal fonts delivered efficiently (e.g. serving them on a speedy CDN).</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Here are a few hosted font services:</div>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;"><a href="http://www.typography.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Cloud Typography</a></li>
<li style="box-sizing: border-box;"><a href="https://typekit.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Typekit</a></li>
<li style="box-sizing: border-box;"><a href="http://fontdeck.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Fontdeck</a></li>
<li style="box-sizing: border-box;"><a href="http://www.webtype.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Webtype</a></li>
<li style="box-sizing: border-box;"><a href="http://www.fontspring.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Fontspring</a></li>
<li style="box-sizing: border-box;"><a href="https://www.typotheque.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Typotheque</a></li>
<li style="box-sizing: border-box;"><a href="http://www.fonts.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Fonts.com</a></li>
<li style="box-sizing: border-box;"><a href="http://www.google.com/fonts" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Google Fonts</a></li>
<li style="box-sizing: border-box;"><a href="http://www.fontsquirrel.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Font Squirrel</a></li>
</ul>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
What About Icon Fonts?</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
It's true, @font-face can load a font file full of icons that can be used for an icon system. However, I think you're far better off using SVG as an icon system. </div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-53634575093870448092016-11-14T11:17:00.002+05:002016-11-14T11:17:45.410+05:00How to use External fonts in CSS? @font-face,.WOFF,.TTF. [Solved]<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This is the method with the deepest support possible right now. The <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">@font-face</code>rule should be added to the stylesheet before any styles.</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">@font-face</span> <span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">font-family</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">'MyWebFont'</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">src</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">url('webfont.eot')</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">src</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">url('webfont.eot?#iefix')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'embedded-opentype'</span><span style="box-sizing: border-box;">)</span>,
<span style="box-sizing: border-box;">url('webfont.woff2')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'woff2'</span><span style="box-sizing: border-box;">)</span>,
<span style="box-sizing: border-box;">url('webfont.woff')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'woff'</span><span style="box-sizing: border-box;">)</span>,
<span style="box-sizing: border-box;">url('webfont.ttf')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'truetype'</span><span style="box-sizing: border-box;">)</span>,
<span style="box-sizing: border-box;">url('webfont.svg#svgFontName')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'svg'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Then use it to style elements like this:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">body </span><span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">font-family</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">'MyWebFont'</span>, Fallback, sans-serif<span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">}</span></code></pre>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
The WOFF Support: </h3>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">Things are </span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">shifting heavily toward WOFF</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">and</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">WOFF 2</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">, so we can probably get away with:</span></div>
<div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">@font-face</span> <span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">font-family</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">'MyWebFont'</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">src</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">url('myfont.woff2')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'woff2'</span><span style="box-sizing: border-box;">)</span>,
<span style="box-sizing: border-box;">url('myfont.woff')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'woff'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">}</span></code></pre>
<span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Chrome</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Safari</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Firefox</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Opera</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">IE</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Android</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">iOS</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> 5+ 5.1+ 3.6+ 11.50+ 9+ 4.4+ 5.1+</span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you need a sort of a happy medium between full support and practical support, this will cover a few more bases:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">@font-face</span> <span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">font-family</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">'MyWebFont'</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">src</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">url('myfont.woff2')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'woff2'</span><span style="box-sizing: border-box;">)</span>,
<span style="box-sizing: border-box;">url('myfont.woff')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'woff'</span><span style="box-sizing: border-box;">)</span>,
<span style="box-sizing: border-box;">url('myfont.ttf')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'truetype'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">}</span></code></pre>
<span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Chrome</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Safari</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Firefox</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Opera</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">IE</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Android</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">iOS</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> 3.5+ 3+ 3.5+ 10.1+ 9+ 2.2+ 4.3+</span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If we're staking the farm on WOFF, then we can expect things to shift toward WOFF2 at some point in time. That means we can live on the bleeding edge with:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">@font-face</span> <span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">font-family</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">'MyWebFont'</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">src</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">url('myfont.woff2')</span> <span style="box-sizing: border-box;">format</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'woff2'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">}</span></code></pre>
<span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Chrome</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Safari</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Firefox</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Opera</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">IE</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">Android</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> </span><span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">iOS</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"> 36+ No 35+ with flag 23+ No 37</span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span></div>
<div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Other Useful Techniques</h3>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">@import</code></h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
While <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">@font-face</code> is excellent for fonts that are hosted on our own servers, there may be situations where a hosted font solution is better. <a href="http://www.google.com/fonts" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Google Fonts</a> offers this as a way to use their fonts. The following is an example of using <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">@import</code> to load the Open Sans font from <a href="http://www.google.com/fonts" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Google Fonts</a>:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;">@import</span> <span style="box-sizing: border-box;">url(//fonts.googleapis.com/css?family=Open+Sans)</span><span style="box-sizing: border-box;">;</span></span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Then we can use it to style elements:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">body </span><span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">font-family</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">'Open Sans'</span>, sans-serif<span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">}</span></code></pre>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
<link>ing a stylesheet</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Similarly, you could link to the same asset as you would any other CSS filter, in the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;"><head></code> of the HTML document rather than in the CSS. Using the same example from Google Fonts, this is what we would use:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;"><</span>link</span> <span style="box-sizing: border-box;">href</span><span style="box-sizing: border-box;"><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">'</span>//fonts.googleapis.com/css?family<span style="box-sizing: border-box;">=</span>Open+Sans<span style="box-sizing: border-box;">'</span></span> <span style="box-sizing: border-box;">rel</span><span style="box-sizing: border-box;"><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">'</span>stylesheet<span style="box-sizing: border-box;">'</span></span> <span style="box-sizing: border-box;">type</span><span style="box-sizing: border-box;"><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">'</span>text/css<span style="box-sizing: border-box;">'</span></span><span style="box-sizing: border-box;">></span></span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Then, we can style our elements like the other methods:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">body </span><span style="box-sizing: border-box;">{</span>
<span style="box-sizing: border-box;">font-family</span><span style="box-sizing: border-box;">:</span> <span style="box-sizing: border-box;">'Open Sans'</span>, sans-serif<span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Again, this is importing the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">@font-face</code> rules but, instead of injecting them to our stylesheet, they are added to our HTML <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;"><head></code> instead.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
It's about the same thing... both techniques download the assets needed.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Good Luck!</div>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-65111798452073839252016-11-14T11:14:00.001+05:002016-11-14T11:14:15.301+05:00How to use iPad Specific CSS?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<br />
<h2 style="text-align: left;">
How to use iPad-specific CSS?</h2>
<div>
<br /></div>
<div>
<br /></div>
<div>
Many people have been lately asking me to post things about iPad specific CSS. So here we go :</div>
<div>
<br /></div>
<div>
<span class="token atrule" style="background-color: #1d1f21; box-sizing: border-box; color: #8f9c6c; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;"><span class="token rule" style="box-sizing: border-box;">@media</span> only screen and <span class="token punctuation" style="box-sizing: border-box; color: white;">(</span><span class="token property" style="box-sizing: border-box; color: #9b869c;">device-width</span><span class="token punctuation" style="box-sizing: border-box; color: white;">:</span> 768px<span class="token punctuation" style="box-sizing: border-box; color: white;">)</span></span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;"> </span><span class="token punctuation" style="background-color: #1d1f21; box-sizing: border-box; color: white; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">{</span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">
</span><span class="token comment" spellcheck="true" style="background-color: #1d1f21; box-sizing: border-box; color: #777777; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">/* For general iPad layouts */</span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">
</span><span class="token punctuation" style="background-color: #1d1f21; box-sizing: border-box; color: white; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">}</span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">
</span><span class="token atrule" style="background-color: #1d1f21; box-sizing: border-box; color: #8f9c6c; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;"><span class="token rule" style="box-sizing: border-box;">@media</span> only screen and <span class="token punctuation" style="box-sizing: border-box; color: white;">(</span><span class="token property" style="box-sizing: border-box; color: #9b869c;">min-device-width</span><span class="token punctuation" style="box-sizing: border-box; color: white;">:</span> 481px<span class="token punctuation" style="box-sizing: border-box; color: white;">)</span> and <span class="token punctuation" style="box-sizing: border-box; color: white;">(</span><span class="token property" style="box-sizing: border-box; color: #9b869c;">max-device-width</span><span class="token punctuation" style="box-sizing: border-box; color: white;">:</span> 1024px<span class="token punctuation" style="box-sizing: border-box; color: white;">)</span> and <span class="token punctuation" style="box-sizing: border-box; color: white;">(</span><span class="token property" style="box-sizing: border-box; color: #9b869c;">orientation</span><span class="token punctuation" style="box-sizing: border-box; color: white;">:</span>portrait<span class="token punctuation" style="box-sizing: border-box; color: white;">)</span></span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;"> </span><span class="token punctuation" style="background-color: #1d1f21; box-sizing: border-box; color: white; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">{</span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">
</span><span class="token comment" spellcheck="true" style="background-color: #1d1f21; box-sizing: border-box; color: #777777; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">/* For portrait layouts only */</span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">
</span><span class="token punctuation" style="background-color: #1d1f21; box-sizing: border-box; color: white; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">}</span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">
</span><span class="token atrule" style="background-color: #1d1f21; box-sizing: border-box; color: #8f9c6c; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;"><span class="token rule" style="box-sizing: border-box;">@media</span> only screen and <span class="token punctuation" style="box-sizing: border-box; color: white;">(</span><span class="token property" style="box-sizing: border-box; color: #9b869c;">min-device-width</span><span class="token punctuation" style="box-sizing: border-box; color: white;">:</span> 481px<span class="token punctuation" style="box-sizing: border-box; color: white;">)</span> and <span class="token punctuation" style="box-sizing: border-box; color: white;">(</span><span class="token property" style="box-sizing: border-box; color: #9b869c;">max-device-width</span><span class="token punctuation" style="box-sizing: border-box; color: white;">:</span> 1024px<span class="token punctuation" style="box-sizing: border-box; color: white;">)</span> and <span class="token punctuation" style="box-sizing: border-box; color: white;">(</span><span class="token property" style="box-sizing: border-box; color: #9b869c;">orientation</span><span class="token punctuation" style="box-sizing: border-box; color: white;">:</span>landscape<span class="token punctuation" style="box-sizing: border-box; color: white;">)</span></span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;"> </span><span class="token punctuation" style="background-color: #1d1f21; box-sizing: border-box; color: white; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">{</span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">
</span><span class="token comment" spellcheck="true" style="background-color: #1d1f21; box-sizing: border-box; color: #777777; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">/* For landscape layouts only */</span><span style="background-color: #1d1f21; color: #cd6a51; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">
</span><span class="token punctuation" style="background-color: #1d1f21; box-sizing: border-box; color: white; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;">}</span></div>
<div>
<span class="token punctuation" style="background-color: #1d1f21; box-sizing: border-box; color: white; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;"><br /></span></div>
<div>
<span class="token punctuation" style="background-color: #1d1f21; box-sizing: border-box; color: white; font-family: "Source Code Pro", Menlo, Consolas, Monaco, monospace; font-size: 18px; white-space: pre;"><br /></span></div>
<div>
<br /></div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-13495203469912148922016-11-14T11:12:00.001+05:002016-11-14T11:12:47.589+05:00CSS Cross-browser min-width & max-width - Explained!<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
Using the "device" keyword targets physical dimension of the screen, not the width of the browser window.</div>
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
For example:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen </span><span style="box-sizing: border-box;">and</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">max</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">device</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">480px</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* STYLES HERE for DEVICES with physical max-screen width of 480px */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span></code></pre>
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
Versus</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen </span><span style="box-sizing: border-box;">and</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">max</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">480px</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* STYLES HERE for BROWSER WINDOWS with a max-width of 480px.
This will work on desktops when the window is narrowed. */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span></code></pre>
</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
I've found the best method is to write your default CSS for the older browsers, as older browsers including i.e. 5.5, 6, 7 and 8. Can't read @media. When I use @media I use it like this:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;"><style</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">type</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">"text/css"</span><span style="box-sizing: border-box;">></span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* default styles here for older browsers.
I tend to go for a 600px - 960px width max but using percentages
*/</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">min-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">960px</span><span style="box-sizing: border-box;">){</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* styles for browsers larger than 960px; */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">min-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">1440px</span><span style="box-sizing: border-box;">){</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* styles for browsers larger than 1440px; */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">min-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">2000px</span><span style="box-sizing: border-box;">){</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* for sumo sized (mac) screens */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">max-device-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">480px</span><span style="box-sizing: border-box;">){</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* styles for mobile browsers smaller than 480px; (iPhone) */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">device-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">768px</span><span style="box-sizing: border-box;">){</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* default iPad screens */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* different techniques for iPad screening */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">min-device-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">481px</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">max-device-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">1024px</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">orientation</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">portrait</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* For portrait layouts only */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">@media</span><span style="box-sizing: border-box;"> only screen and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">min-device-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">481px</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">max-device-width</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">1024px</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> and </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">orientation</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">landscape</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/* For landscape layouts only */</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;"></style></span></code></pre>
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
But you can do whatever you like with your @media, This is just an example of what I've found best for me when building styles for all browsers.</div>
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
Good Luck!</div>
</div>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-13037332067287546782016-11-13T17:28:00.003+05:002016-11-13T17:28:38.411+05:00How to get absolute path of Current script in PHP? [Quick & Easy]<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
How to get absolute path of Current script in PHP?</h2>
<div>
<br /></div>
<div>
This is the most easiest and quicker approach to this very novice matter...</div>
<div>
<br /></div>
<div>
Go ahead and try :</div>
<div>
<br /></div>
<div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">echo realpath</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">dirname</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">__FILE__</span><span style="box-sizing: border-box;">));</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you place this in an included file, it prints the path to this include. To get the path of the parent script, replace <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">__FILE__</code> with <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">$_SERVER['PHP_SELF']</code>. But be aware that PHP_SELF is a security risk!</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
example for URL:<span style="box-sizing: border-box; font-weight: 600;"><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">http://example.com/folder1/folder2/yourfile.php?var=blabla#12345</code></span></div>
<h1 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.6em; line-height: 1.1em; margin: 2em 0px 0.7em;">
typical PHP codes:</h1>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"DOCUMENT_ROOT"</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/home/</span><span style="box-sizing: border-box;">user</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">public_html
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"SERVER_ADDR"</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">143.34</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">112.23</span><span style="box-sizing: border-box;">
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">'HTTP_HOST'</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> example</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">com </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">or</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">with</span><span style="box-sizing: border-box;"> WWW</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"REQUEST_URI"</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/folder1/</span><span style="box-sizing: border-box;">folder2</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">yourfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php</span><span style="box-sizing: border-box;">?</span><span style="box-sizing: border-box;">var</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">blabla
__FILE__ </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/home/</span><span style="box-sizing: border-box;">user</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">public_html</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">folder1</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">folder2</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">yourfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php
basename</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">__FILE__</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> yourfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php
__DIR__ </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/home/</span><span style="box-sizing: border-box;">user</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">public_html</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">folder1</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">folder2 </span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">same</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;"> dirname</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">__FILE__</span><span style="box-sizing: border-box;">)]</span><span style="box-sizing: border-box;">
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"QUERY_STRING"</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">var</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">blabla
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"REQUEST_URI"</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/folder1/</span><span style="box-sizing: border-box;">folder2</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">yourfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php</span><span style="box-sizing: border-box;">?</span><span style="box-sizing: border-box;">var</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">blabla
parse_url</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"REQUEST_URI"</span><span style="box-sizing: border-box;">],</span><span style="box-sizing: border-box;"> PHP_URL_PATH</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/folder1/</span><span style="box-sizing: border-box;">folder2</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">yourfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"PHP_SELF"</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/folder1/</span><span style="box-sizing: border-box;">folder2</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">yourfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php
</span><span style="box-sizing: border-box;">//if "YOURFILE.php" is included in "PARENTFILE.php" , and "PARENTFILE.PHP?abc" is opened:</span><span style="box-sizing: border-box;">
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"PHP_SELF"</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">parentfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"REQUEST_URI"</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">parentfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php</span><span style="box-sizing: border-box;">?</span><span style="box-sizing: border-box;">abc
$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"SCRIPT_FILENAME"</span><span style="box-sizing: border-box;">]===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">home</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">user</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">public_html</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">parentfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php
str_replace</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"DOCUMENT_ROOT"</span><span style="box-sizing: border-box;">],</span><span style="box-sizing: border-box;">''</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> str_replace</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'\\'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">'/'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">__FILE__ </span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/folder1/</span><span style="box-sizing: border-box;">folder2</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">yourfile</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">php</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Notice:</div>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
hastag (#...) url parts cant be detected from PHP (server-side). For that, use Javascript.</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">DIRECTORY_SEPARATOR</code> returns <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">\</code> for Windows-type hostings, instead of <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/</code>*</div>
</li>
</ul>
<h1 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.6em; line-height: 1.1em; margin: 2em 0px 0.7em;">
For WordPress</h1>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">home_url</span><span style="box-sizing: border-box;">()</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">//> http://example.com</span><span style="box-sizing: border-box;">
get_stylesheet_directory_uri</span><span style="box-sizing: border-box;">()</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">//> http://example.com/wp-content/themes/THEME_NAME [same: get_bloginfo('template_url') ]</span><span style="box-sizing: border-box;">
get_stylesheet_directory</span><span style="box-sizing: border-box;">()</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">//> /home/user/public_html/wp-content/themes/THEME_NAME</span><span style="box-sizing: border-box;">
plugin_dir_url</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">__FILE__</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">//> http://example.com/wp-content/plugins/MY-PLUGIN/ [while used inside plugin.. same as: plugins_url('',__FILE__) ]</span><span style="box-sizing: border-box;">
plugin_dir_path</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">__FILE__</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">//> /home/user/public_html/wp-content/plugins/MY-PLUGIN/ [while used inside plugin] </span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">//===============MY EXAMPLES - USAGE============//</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">i</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">e</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;"> wordpress </span><span style="box-sizing: border-box;">is</span><span style="box-sizing: border-box;"> installed </span><span style="box-sizing: border-box;">in</span><span style="box-sizing: border-box;"> subdirectory</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;"> http</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">//example.com/wpdir/)</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'domainURL'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">(((!</span><span style="box-sizing: border-box;">empty</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">'HTTPS'</span><span style="box-sizing: border-box;">])</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">&&</span><span style="box-sizing: border-box;"> $_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">'HTTPS'</span><span style="box-sizing: border-box;">]!==</span><span style="box-sizing: border-box;">'off'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">||</span><span style="box-sizing: border-box;"> $_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">'SERVER_PORT'</span><span style="box-sizing: border-box;">]==</span><span style="box-sizing: border-box;">443</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">?</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">'https://'</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">'http://'</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">).</span><span style="box-sizing: border-box;">$_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">'HTTP_HOST'</span><span style="box-sizing: border-box;">]);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> http://example.com</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'homeURL'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> home_url</span><span style="box-sizing: border-box;">());</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> http://example.com/wpdir/</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'homeFOLD'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> str_replace</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">domainURL</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">''</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> homeURL</span><span style="box-sizing: border-box;">));</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> /wpdir/</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'requestURI'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> $_SERVER</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box;">"REQUEST_URI"</span><span style="box-sizing: border-box;">]);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> /wpdir/any-page?with=parameters</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'requestURIfromHome'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> str_replace</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">homeFOLD</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">''</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">requestURI</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> /any-page?with=parameters</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'requestURIWithoutParametr'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">parse_url</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">requestURIfromHome</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> PHP_URL_PATH</span><span style="box-sizing: border-box;">));</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> /wpdir/any-page</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'currentURL'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> domainURL</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">requestURI</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> http://example.com/wpdir/any-page?with=parameters</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'THEME_URL'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> str_replace</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">domainURL</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">''</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> get_template_directory_uri</span><span style="box-sizing: border-box;">())</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">plugin_dir_url</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">__FILE__</span><span style="box-sizing: border-box;">))</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> http://example.com/wpdir/wp-content/themes/THE-THEME-NAME/</span><span style="box-sizing: border-box;">
define</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'PLUGIN_URL'</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> str_replace</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">domainURL</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">''</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> plugin_dir_url</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">__FILE__</span><span style="box-sizing: border-box;">))</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// -----> http://example.com/wpdir/wp-content/plugins/THE-PLUGIN-NAME/</span></code></pre>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-29560822154102522032016-11-13T17:26:00.003+05:002019-06-02T02:52:19.267+05:00PHP cURL HTTP CODE return 0 - How to Debug? [Solved]<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonDisplay, Georgia, serif; font-weight: 400; line-height: 1.3em; margin: 0px 0px 5px; text-align: left;">
PHP cURL HTTP CODE return 0</h2>
<div>
<br /></div>
<div>
<br /></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you connect with the server, then you can get a return code from it, otherwise it will fail and you get a 0. So if you try to connect to "geeks-wiki.blogspot.com/lksdfk" you will get a return code of 400, if you go directly to geeks-wiki.blogspot.com, you will get 302 (and then 200 if you forward to the next page... </div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Tested using the code below.</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;"><?</span><span style="box-sizing: border-box;">php
$html_brand </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">"www.google.com"</span><span style="box-sizing: border-box;">;</span><span style="box-sizing: border-box;">
$ch </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> curl_init</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;">
$options </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> array</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">
CURLOPT_URL </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> $html_brand</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
CURLOPT_RETURNTRANSFER </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">true</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
CURLOPT_HEADER </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">true</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
CURLOPT_FOLLOWLOCATION </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">true</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
CURLOPT_ENCODING </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">""</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
CURLOPT_AUTOREFERER </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">true</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
CURLOPT_CONNECTTIMEOUT </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">120</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
CURLOPT_TIMEOUT </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">120</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
CURLOPT_MAXREDIRS </span><span style="box-sizing: border-box;">=></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">10</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
curl_setopt_array</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;"> $ch</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> $options </span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
$response </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> curl_exec</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$ch</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
$httpCode </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> curl_getinfo</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$ch</span><span style="box-sizing: border-box;">,</span><span style="box-sizing: border-box;"> CURLINFO_HTTP_CODE</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">if</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;"> $httpCode </span><span style="box-sizing: border-box;">!=</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">200</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">){</span><span style="box-sizing: border-box;">
echo </span><span style="box-sizing: border-box;">"Return code is {$httpCode} \n"</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">curl_error</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$ch</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">else</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">
echo </span><span style="box-sizing: border-box;">"<pre>"</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">htmlspecialchars</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$response</span><span style="box-sizing: border-box;">).</span><span style="box-sizing: border-box;">"</pre>"</span><span style="box-sizing: border-box;">;</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
curl_close</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$ch</span><span style="box-sizing: border-box;">);</span></code></pre>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;">
</pre>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><span style="color: #333333;"><span style="white-space: pre-wrap;">The more easier approach is to check <b>curl_error() </b>as what it returns and then check the cURL Manual at PHP's website! Source : https://www.stackflair.com/curl-error0-in-php/</span></span></pre>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-38567363682156835692016-11-13T17:24:00.000+05:002016-11-13T17:24:11.127+05:00How to disable ModSecurity rules for Drupal and Wordpress [SOLVED]<div dir="ltr" style="text-align: left;" trbidi="on">
<h1 style="background-color: white; box-sizing: border-box; font-family: LyonDisplay, Georgia, serif; font-weight: 400; line-height: 1.3em; margin: 0px 0px 5px;">
How to disable ModSecurity rules for Drupal and Wordpress.</h1>
<div>
<br /></div>
<div>
<br /></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This guide is based on various community forum posts, and hours of frustration.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;">This guide is intended as a relatively easy step by step guide to:</span></div>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">Disable Apache2 <a href="http://www.modsecurity.org/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">ModSecurity</a> rules for Drupal and Wordpress.</li>
<li style="box-sizing: border-box;">ModSecurity is a very powerful web application firewall but needs to be configured to work with Drupal and Wordpress.</li>
<li style="box-sizing: border-box;">The ModSecurity rules can no longer be disabled in the .htaccess file and this guide explains how to disable the rules based on the specific location of a request on the server without having to disable rules for an entire domain in the httpd.conf file, by creating a local rule exceptions file.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;">Requirements:</span></div>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">Webserver with ModSecurity setup and configured. Click here for <a href="https://www.thefanclub.co.za/how-to/how-install-apache2-modsecurity-and-modevasive-ubuntu-1204-lts-server" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">How to install apache2 mod_security and mod_evasive on Ubuntu 12.04 LTS server</a></li>
<li style="box-sizing: border-box;">Ubuntu/Debian based linux server is used as reference during tutorial but instructions are fairly generic for all implementations of ModSecurity.</li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
1. <span style="box-sizing: border-box;">View</span> ModSecurity <span style="box-sizing: border-box;">Audit Log File</span>.</h2>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">We need to first find the rules that are being triggered by ModSecurity on your webserver.</li>
<li style="box-sizing: border-box;">Open the tail end of the ModSecurity log file called <span style="box-sizing: border-box; font-weight: 600;">modsec_audit.log</span> to view the last entries made to the log file.</li>
<li style="box-sizing: border-box;">For Apache2 servers it is located in<span style="box-sizing: border-box; font-weight: 600;"> /var/log/apache2/</span></li>
<li style="box-sizing: border-box;">Open the Terminal Window and enter :</li>
</ul>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><span style="box-sizing: border-box;">sudo tail /var/log/apache2/modsec_audit.log --lines 60 | less</span></pre>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">The output should look similar to this screenshot below.</li>
<li style="box-sizing: border-box;">Look for <span style="box-sizing: border-box; font-weight: 600;">Access denied with code 403 </span>and work backwards to find the start of the rule entry based on the log entry id.</li>
<li style="box-sizing: border-box;">In this case the log <span style="box-sizing: border-box; font-weight: 600;">entry ID</span> is --00aee77f (see marked in <span style="box-sizing: border-box;">yellow</span>)</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 600;">Find</span> the <span style="box-sizing: border-box; font-weight: 600;">GET item</span> - in this example it is <span style="box-sizing: border-box; font-weight: 600;">/modern-classic </span>(see marked in <span style="box-sizing: border-box;">blue</span>)</li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 600;">Find</span> the <span style="box-sizing: border-box; font-weight: 600;">ModSecurity rule</span> that was triggered by the GET - in this example the rule id<span style="box-sizing: border-box; font-weight: 600;"> 958291 </span>(see marked in <span style="box-sizing: border-box;">purple</span>)</li>
</ul>
<div>
<span style="font-family: LyonText, Georgia, serif;"><span style="font-size: 18px;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijWp6hGr5l53IP9gQJSG2s04Bwzy34BrMPDARpX6LqTzwyyLHikW1KgSKgxmEbyrwZPaaodiHIVb612JeXTcUqfDI5EmvjIkAYmU6CJMklUbPOwo0ZWCgURSgI55UNKdQLPCHGWuK-uZs/s1600/modsec_audit.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijWp6hGr5l53IP9gQJSG2s04Bwzy34BrMPDARpX6LqTzwyyLHikW1KgSKgxmEbyrwZPaaodiHIVb612JeXTcUqfDI5EmvjIkAYmU6CJMklUbPOwo0ZWCgURSgI55UNKdQLPCHGWuK-uZs/s1600/modsec_audit.png" /></a></span></span></div>
</div>
<div>
<span style="font-family: LyonText, Georgia, serif;"><br /></span></div>
<div>
<span style="font-family: LyonText, Georgia, serif;"><br /></span></div>
<div>
<span style="font-family: LyonText, Georgia, serif;"><br /></span></div>
<div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
2. Create a Local Exceptions ModSecurity rule file.</h2>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">To disable / exclude certain ModSecurity rules you need to create a local exceptions file.</li>
<li style="box-sizing: border-box;">There are various places you can create this file you only need to make sure that ModSecurity loads it during startup.</li>
<li style="box-sizing: border-box;">We are going to create a whitelist.conf file in the /etc/modsecurity/activated_rules/ directory as all files with .conf extension will be loaded during ModSecurity startup.</li>
<li style="box-sizing: border-box;">Open the Terminal Window and enter :</li>
</ul>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><span style="box-sizing: border-box;">sudo vi /etc/modsecurity/activated_rules/whitelist.conf</span></pre>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">For our example we add the location of the <span style="box-sizing: border-box; font-weight: 600;">GET</span> and ModSecurity rule <span style="box-sizing: border-box; font-weight: 600;">id from step 1.</span></li>
<li style="box-sizing: border-box;">Add the following to your <span style="box-sizing: border-box; font-weight: 600;">whitelist.conf</span> file and save :</li>
</ul>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;"><LocationMatch "<span style="box-sizing: border-box;">/modern-classic</span>"></span>
<span style="box-sizing: border-box;"> <span style="box-sizing: border-box; font-weight: 600;">SecRuleRemoveById</span> <span style="box-sizing: border-box;">958291</span></span>
<span style="box-sizing: border-box;"></LocationMatch></span>
</span></pre>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">You need to add the<span style="box-sizing: border-box; font-weight: 600;"> location as a regex</span> of the <span style="box-sizing: border-box; font-weight: 600;">directory path or file</span> that is causing the ModSecurity rule to be triggered.</li>
<li style="box-sizing: border-box;">In the following example we add the location directly to the file that triggers the ModSecurity rule.</li>
</ul>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><span style="box-sizing: border-box;"><span style="box-sizing: border-box;"><LocationMatch "/wp-admin/update.php"></span>
<span style="box-sizing: border-box;"> SecRuleRemoveById 981173</span>
<span style="box-sizing: border-box;"></LocationMatch></span>
</span></pre>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
3. Restart Webserver.</h2>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">To the changes to take effect you need to restart you webserver.</li>
<li style="box-sizing: border-box;">For Apache2 servers, open the Terminal Window and enter :</li>
</ul>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><span style="box-sizing: border-box;">sudo service apache2 restart</span></pre>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-61610064396801006732016-11-13T17:22:00.003+05:002016-11-13T17:22:45.336+05:00How to Secure SSH using Two-Factor Authentication? [SOLVED!]<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
How to Harden SSH?</h2>
<br />
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
As part of the <a href="https://code.google.com/p/google-authenticator/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Authenticator project</a> Google released a PAM (Pluggable Authentication Module) implementation of the 2-factor system. This is readily available and packaged as <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">libpam-google-authenticator</code> on Debian/Ubuntu.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Once installed, you can run <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">google-authenticator</code> to generate your user’s secret and configuration settings. Just follow the prompts and scan the ASCII QR code to add it to the Authenticator app.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
You can then add <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">auth required pam_google_authenticator.so</code> to <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/pam.d/sshd</code>. You’ll also need to have <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">ChallengeResponseAuthentication yes</code>in your <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/ssh/sshd_config</code>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This way, when you log in <em style="box-sizing: border-box;">with a password</em>, the system will request your one-time token as well.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The reason this is less than ideal is that any sufficiently hardened setup already disables password logins. What I wanted in my particular case was a public key login with a mandatory second factor. Turns out, however, that public key login completely bypasses challenge-response login.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
After some searching, I found the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">AuthenticationMethods</code> <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">sshd_config</code>parameter. Adding the line</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">AuthenticationMethods publickey,keyboard-interactive:pam</code></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
to my <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">sshd_config</code> did indeed prompt me for my verification code. But not before prompting me for my password as well. This was a bit too much, so I set out to limit the login sequence to just a prompt for the code.</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
PAM is magic</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The reason this was happening was that <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">publickey,keyboard-interactive:pam</code>required a successful complete PAM run before letting you proceed onwards. My PAM config (in <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/pam.d/sshd</code>), however, was asking for a password and I’d added the line at the end to also ask for a verification code.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I moved the line to the beginning of the file and changed it so that it is enough to log you in:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">auth sufficient pam_google_authenticator.so</code></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
However, pressing Ctrl+C or entering an empty code at the 2FA prompt still dropped me to a password prompt. This would clearly not do.</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
The solution</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Turns out, <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">sufficient</code> is an alias for <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">[success=done new_authtok_reqd=done default=ignore]</code>. Notice that the default action is <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">ignore</code>. That is, the module is sufficient to authenticate me <em style="box-sizing: border-box;">but not required</em>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The final solution was to prepend the following to my <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/pam.d/sshd</code> instead:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">auth [success=done new_authtok_reqd=done default=die] pam_google_authenticator.so nullok</code></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">nullok</code> option means that if the user doesn’t have 2FA set up, the module will still let you through.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I also left the rest of <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/pam.d/sshd</code> intact, even though it’s unnecessary now - the 2FA module either kills authentication or declares it complete, nothing else should ever execute. That said, PAM is magic and I’m no wizard.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I hope I freed someone’s afternoon for better things than fiddling with <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">ssh</code> and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">google-authenticator</code>.</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-77037291569258912812016-11-13T17:21:00.001+05:002016-11-13T17:21:32.113+05:00Faster Apache, How to Make Apache light & Fast - [Full Guide!]<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<h2 style="text-align: left;">
<span style="font-size: x-large;">How to make Apache Faster & Lighter!</span></h2>
<div>
<span style="font-size: x-large;"><br /></span></div>
<div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Unload Unneeded Modules</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
In Ubuntu and Debian based systems, you'll see a folder called <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/apache2/mods-enabled</code> and a folder called <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/apache2/mods-available/</code>. The mods-available folder is a list of all the modules installed on a particular server and mods-enabled is the modules that are currently active.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
On my VPS I have 17 modules active by default. This is too many and most aren't needed for my application. Unfortunately, the modules you need may not be precisely clear because some are dependencies of others.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
What I suggest doing is making a list of all the currently active modules and saving it for future reference in case you need to revert back. Then simply disable modules one-by-one and restart your Apache after each change to see if errors occur.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
On Ubuntu and Debian, you disable a module with the command (using autoindex as an example):</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">sudo a2dismod autoindex
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Some modules that are particularly resource hungry that you should disable if you don't need them are:</div>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">PHP</li>
<li style="box-sizing: border-box;">SSL</li>
<li style="box-sizing: border-box;">Rewrite</li>
<li style="box-sizing: border-box;">Perl</li>
<li style="box-sizing: border-box;">Python</li>
<li style="box-sizing: border-box;">Rack / Ruby / Passenger</li>
</ul>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Several of those modules are not enabled by default, so you may not have them enabled, and in some cases, they're enabled because you actually need them.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">A quick note about "rewrite":</span> Often this module is enabled when the "alias" module would actually work equally well. If you can get by with alias then disable rewrite. Rewrite is one of the more burdensome modules, but it also imbues Apache with some remarkable powers.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Switching from "rewrite" to "alias" is an advanced topic (with some <a href="http://httpd.apache.org/docs/trunk/rewrite/avoid.html" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">helpful documentation</a>) However, even if you can't turn off rewrite completely, but you are able to convert some of your rewrite rules to aliases, you'll gain an advantage.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
After you disable a module and reload the Apache configuration, you can check the apache error log for messages. In Ubuntu and Debian, check /var/log/apache2/error.log.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I get an error that looks like this:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">Syntax error on line 6 of /etc/apache2/sites-enabled/site1:
Invalid command 'DAVLockDB', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
That means that the module I just disabled was needed. In this case, the module was dav_fs, so I just re-enable it with:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">sudo a2enmod dav_fs
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Then I restarted Apache and looked for the next error. It may take several tries before you get the minimum list sorted out. Be patient, it's worth it.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Limit the Number of Apache Processes and Children</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Most operating systems' default Apache configurations are not well suited for smaller servers-- 25 child processes or more is common. If each of your Apache child processes uses 120MB of RAM, then your VPS would need 3GB just for Apache.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
One visitor's web browser may request 4 items from the website at once, so with only 7 or 8 people trying to load a page at the same time your cloud server can become overloaded. This causes the web page to hang in a constantly loading state for what seems like an eternity.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
It is often the case that the server will keep these dead Apache processes active, attempting to serve content long after the user gave up, which reduces the number of processes available to serve users and reduces the amount of system RAM available. This causes what is commonly known as a <em style="box-sizing: border-box;">downward spiral</em> that ends in a bad experience for both you and your site's visitors.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
What you should do is figure out how much RAM your application needs, and then figure out how much is left, and allocate most of that to Apache.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
For example, if you have three php-fpm processes handling dynamic content, and each can use up to 70MB of RAM, and your MySQL server may use up to 120MB of RAM, that combines for a total of 330MB used by the application. This allows you to allocate about 150MB to Apache.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
While Apache is running open the <span style="box-sizing: border-box; font-weight: 600;">top</span> command on the server. I'll paste a little bit of what you'd see, trimming out most of the lines that aren't pertinent:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">top -bn 1
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
[...]
15015 www-data 20 0 232m 9644 1900 S 0.0 1.6 0:00.02 apache2
15016 www-data 20 0 232m 9644 1900 S 0.0 1.6 0:00.01 apache2
15017 www-data 20 0 232m 9644 1900 S 0.0 1.6 0:00.02 apache2
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Notice the RES column for an Apache child process and make note of its RES value. For example, on my virtual server which has been well optimized, the value is 9,644, which means it's using not quite 10MB of RAM. If I limit Apache to a maximum of 15 child processes, then it should max out at about 150MB of RAM.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Edit your cloud server's apache config file, which on Ubuntu and Debian is <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/apache2/apache2.conf</code> and locate the section for the mpm<em style="box-sizing: border-box;">prefork</em>module configuration. Look for the MaxClients line and set it to 15, then save and restart Apache.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Here is an example of what you'll look for in Ubuntu:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><IfModule mpm_prefork_module>
StartServers 3
MinSpareServers 3
MaxSpareServers 5
MaxClients 30
MaxRequestsPerChild 0
</IfModule>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
See the <span style="box-sizing: border-box; font-weight: 600;">MaxClients</span> line there? We need to change that value to a lower number.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If your VPS gets overloaded, and reaches the maximum number of clients it can serve at once, it will serve those and other users will simply get a quick failure. They can then reload the page and maybe have greater success on the second try.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This sounds bad, but believe me, it's much better to have these connections close quickly but leave the server in a healthy state rather than hanging open for an eternity. Surprisingly you can get better performance from a server that has fewer child processes but responds faster than it is to have a server with more child processes that it is unable to handle.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Case in point, a Wordpress site I manage is hosted on a 1GB droplet using 4 php-fpm processes and is able to serve over 950 simultaneous users at one time. This translate to a peak capacity of about 42 million page views per day, should this website ever become popular enough!</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Consider Alternate MPM Configuration</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Most Apache configurations have historically used the <span style="box-sizing: border-box; font-weight: 600;">prefork</span> mpm, which is thread safe and therefore suitable for use with PHP and other embedded languages.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you get rid of external modules such as PHP or Rails then you can consider the <span style="box-sizing: border-box; font-weight: 600;">worker</span> MPM, which often is faster than prefork.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
In order to enable the worker module you have to install it.</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">sudo apt-get install apache2-mpm-worker
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Which will show you a message like this:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">The following packages will be REMOVED:
apache2-mpm-prefork libapache2-mod-php5
The following NEW packages will be installed:
apache2-mpm-worker
0 upgraded, 1 newly installed, 2 to remove and 2 not upgraded.
Need to get 2,284 B of archives.
After this operation, 8,718 kB disk space will be freed.
Do you want to continue [Y/n]?
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Take careful note, on Ubuntu, if you install the worker mpm it will uninstall the prefork mpm <em style="box-sizing: border-box;">and</em> it will uninstall mod_php and other incompatible add-on modules.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Here we've discussed four optimizations you can make to Apache that should greatly boost your application performance, even if you have a small droplet.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I strongly suggest trying this out on a test droplet rather than your production server. The beauty of DigitalOcean's service is that you can spin up a new droplet for just the amount of time you need to test the changes, and shut it down when you're done. With hourly billing, it is a low-risk, low-cost way to find the perfect configuration for your VPS.</div>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-17926806750959541512016-11-13T17:19:00.003+05:002016-11-13T17:19:32.600+05:00OpenVPN Client Configuration How to? - Windows,Linux,MAC,iOS & Android<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Windows</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Installing</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The OpenVPN client application for Windows can be found on <a href="https://openvpn.net/index.php/open-source/downloads.html" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">OpenVPN's Downloads page</a>. Choose the appropriate installer version for your version of Windows.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
Note</div>
<span style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">OpenVPN needs administrative privileges to install.</span><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"></span><br />
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
After installing OpenVPN, copy the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.ovpn</code> file to:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">C:\Program Files\OpenVPN\config
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
When you launch OpenVPN, it will automatically see the profile and makes it available.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
OpenVPN must be run as an administrator each time it's used, even by administrative accounts. To do this without having to right-click and select <span style="box-sizing: border-box; font-weight: 600;">Run as administrator</span> every time you use the VPN, you can preset this, but this must be done from an administrative account. This also means that standard users will need to enter the administrator's password to use OpenVPN. On the other hand, standard users can't properly connect to the server unless the OpenVPN application on the client has admin rights, so the elevated privileges are necessary.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To set the OpenVPN application to always run as an administrator, right-click on its shortcut icon and go to <span style="box-sizing: border-box; font-weight: 600;">Properties</span>. At the bottom of the <span style="box-sizing: border-box; font-weight: 600;">Compatibility</span> tab, click the button to <span style="box-sizing: border-box; font-weight: 600;">Change settings for all users</span>. In the new window, check <span style="box-sizing: border-box; font-weight: 600;">Run this program as an administrator</span>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Connecting</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Each time you launch the OpenVPN GUI, Windows will ask if you want to allow the program to make changes to your computer. Click <span style="box-sizing: border-box; font-weight: 600;">Yes</span>. Launching the OpenVPN client application only puts the applet in the system tray so that the VPN can be connected and disconnected as needed; it does not actually make the VPN connection.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Once OpenVPN is started, initiate a connection by going into the system tray applet and right-clicking on the OpenVPN applet icon. This opens the context menu. Select <span style="box-sizing: border-box; font-weight: 600;">client1</span> at the top of the menu (that's our <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">client1.ovpn</code> profile) and choose <span style="box-sizing: border-box; font-weight: 600;">Connect</span>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
A status window will open showing the log output while the connection is established, and a message will show once the client is connected.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Disconnect from the VPN the same way: Go into the system tray applet, right-click the OpenVPN applet icon, select the client profile and click <span style="box-sizing: border-box; font-weight: 600;">Disconnect</span>.</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
OS X</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Installing</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://tunnelblick.net/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Tunnelblick</a> is a free, open source OpenVPN client for Mac OS X. You can download the latest disk image from the <a href="https://tunnelblick.net/downloads.html" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Tunnelblick Downloads page</a>. Double-click the downloaded <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.dmg</code> file and follow the prompts to install.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Towards the end of the installation process, Tunnelblick will ask if you have any configuration files. It can be easier to answer <span style="box-sizing: border-box; font-weight: 600;">No</span> and let Tunnelblick finish. Open a Finder window and double-click <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">client1.ovpn</code>. Tunnelblick will install the client profile. Administrative privileges are required.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Connecting</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Launch Tunnelblick by double-clicking Tunnelblick in the <span style="box-sizing: border-box; font-weight: 600;">Applications</span> folder. Once Tunnelblick has been launched, there will be a Tunnelblick icon in the menu bar at the top right of the screen for controlling connections. Click on the icon, and then the <span style="box-sizing: border-box; font-weight: 600;">Connect</span> menu item to initiate the VPN connection. Select the <span style="box-sizing: border-box; font-weight: 600;">client1</span> connection.</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Linux</h3>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Installing</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you are using Linux, there are a variety of tools that you can use depending on your distribution. Your desktop environment or window manager might also include connection utilities.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The most universal way of connecting, however, is to just use the OpenVPN software.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
On Ubuntu or Debian, you can install it just as you did on the server by typing:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo apt-get update
</li>
<li style="box-sizing: border-box;">sudo apt-get install openvpn
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
On CentOS you can enable the EPEL repositories and then install it by typing:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo yum install epel-release
</li>
<li style="box-sizing: border-box;">sudo yum install openvpn
</li>
</ul>
</code></pre>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Configuring</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Check to see if your distribution includes a <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn/update-resolv-conf</code>script:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">ls /etc/openvpn
</li>
</ul>
</code></pre>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><div style="box-sizing: border-box;">
Output</div>
update-resolve-conf
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, edit the OpenVPN client configuration file you transfered:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">nano <span style="box-sizing: border-box;">client1</span>.ovpn
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Uncomment the three lines we placed in to adjust the DNS settings if you were able to find an <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">update-resolv-conf</code> file:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
client1.ovpn</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">script-security 2
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you are using CentOS, change the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">group</code> from <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">nogroup</code> to <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">nobody</code> to match the distribution's available groups:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
client1.ovpn</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">group <span style="box-sizing: border-box;">nobody</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Save and close the file.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now, you can connect to the VPN by just pointing the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">openvpn</code> command to the client configuration file:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo openvpn --config <span style="box-sizing: border-box;">client1</span>.ovpn
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This should connect you to your server.</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
iOS</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Installing</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
From the iTunes App Store, search for and install <a href="https://itunes.apple.com/us/app/id590379981" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">OpenVPN Connect</a>, the official iOS OpenVPN client application. To transfer your iOS client configuration onto the device, connect it directly to a computer.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Completing the transfer with iTunes will be outlined here. Open iTunes on the computer and click on <span style="box-sizing: border-box; font-weight: 600;">iPhone</span> > <span style="box-sizing: border-box; font-weight: 600;">apps</span>. Scroll down to the bottom to the <span style="box-sizing: border-box; font-weight: 600;">File Sharing</span> section and click the OpenVPN app. The blank window to the right, <span style="box-sizing: border-box; font-weight: 600;">OpenVPN Documents</span>, is for sharing files. Drag the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.ovpn</code> file to the OpenVPN Documents window.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDfMm3OlkM11YGlCOfya_gsQGxM63Tnj9ge97tQ-guywmJzsPJd9Ji2ga1fYPuGZEqIETFs-77WdmSw-vykpRhGkWNdK16Ut89SQ7bR0M96YtoIo6nfIUKAoKF7k8p5fSdV5iCNOWioIk/s1600/1.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDfMm3OlkM11YGlCOfya_gsQGxM63Tnj9ge97tQ-guywmJzsPJd9Ji2ga1fYPuGZEqIETFs-77WdmSw-vykpRhGkWNdK16Ut89SQ7bR0M96YtoIo6nfIUKAoKF7k8p5fSdV5iCNOWioIk/s1600/1.png" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now launch the OpenVPN app on the iPhone. There will be a notification that a new profile is ready to import. Tap the green plus sign to import it.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBeTTLg8ZMg0jRfeWAr5DqSQ2A_QlfK67S_L_YAU-HI57SJXoYioNFswyEBipvvWlpRdyENqMVtLhNq0Avqp_g48M3OfpyHaUUdPZ-gUVZ1-beEo0MG9oI8b3Bv0kgTuO79GKP2MgceiQ/s1600/2.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBeTTLg8ZMg0jRfeWAr5DqSQ2A_QlfK67S_L_YAU-HI57SJXoYioNFswyEBipvvWlpRdyENqMVtLhNq0Avqp_g48M3OfpyHaUUdPZ-gUVZ1-beEo0MG9oI8b3Bv0kgTuO79GKP2MgceiQ/s1600/2.png" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Connecting</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
OpenVPN is now ready to use with the new profile. Start the connection by sliding the <span style="box-sizing: border-box; font-weight: 600;">Connect</span> button to the <span style="box-sizing: border-box; font-weight: 600;">On</span> position. Disconnect by sliding the same button to <span style="box-sizing: border-box; font-weight: 600;">Off</span>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
Note</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;">The VPN switch under <span style="box-sizing: border-box; font-weight: 600;">Settings</span> cannot be used to connect to the VPN. If you try, you will receive a notice to only connect using the OpenVPN app.</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><br /></span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzcrLjqvDD6FUXW6gAouuXhQ2YRAg7QbkiBLI3ZdyRxs3DHCjo-HwT551_ldDbJLVUxiih1yJawuDsW4I8uhDk3Mxvd7LKk5WhJiJUDAEVegTROqd_FCsQgzNusZafsyco99lK7mifIzU/s1600/3.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzcrLjqvDD6FUXW6gAouuXhQ2YRAg7QbkiBLI3ZdyRxs3DHCjo-HwT551_ldDbJLVUxiih1yJawuDsW4I8uhDk3Mxvd7LKk5WhJiJUDAEVegTROqd_FCsQgzNusZafsyco99lK7mifIzU/s1600/3.png" /></a></span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Android</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Installing</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Open the Google Play Store. Search for and install <a href="https://play.google.com/store/apps/details?id=net.openvpn.openvpn" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">Android OpenVPN Connect</a>, the official Android OpenVPN client application.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.ovpn</code> profile can be transferred by connecting the Android device to your computer by USB and copying the file over. Alternatively, if you have an SD card reader, you can remove the device's SD card, copy the profile onto it and then insert the card back into the Android device.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Start the OpenVPN app and tap the menu to import the profile.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlX5_2EI-BbKOcZ8HebaVl0huOxJwZlNyy3pQIjlD8dqxK-h7dM8brS8EBPIve7-AodXlDnMoTm49qUBimt1_6sY3EVJ9E5ZZuJEf1LQK9dt-5j6Mpwx8JmMxjAxF7E_VEMG9BpeTLLDI/s1600/4.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlX5_2EI-BbKOcZ8HebaVl0huOxJwZlNyy3pQIjlD8dqxK-h7dM8brS8EBPIve7-AodXlDnMoTm49qUBimt1_6sY3EVJ9E5ZZuJEf1LQK9dt-5j6Mpwx8JmMxjAxF7E_VEMG9BpeTLLDI/s1600/4.png" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Then navigate to the location of the saved profile (the screenshot uses <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/sdcard/Download/</code>) and select the file. The app will make a note that the profile was imported.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3-a1ji769Nsc3TZwzt9CJzqgdzKeLPAx3_wyzSCXbBB94OXVNdn0Bt5Ya8FfMMOEek9GqX4-feO7iPF9_rCFnuPNemLxN3B7iz-CtuMABfRvpx0BcQeXIrjQideO2jxbGIQbF30DPCRk/s1600/5.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3-a1ji769Nsc3TZwzt9CJzqgdzKeLPAx3_wyzSCXbBB94OXVNdn0Bt5Ya8FfMMOEek9GqX4-feO7iPF9_rCFnuPNemLxN3B7iz-CtuMABfRvpx0BcQeXIrjQideO2jxbGIQbF30DPCRk/s400/5.png" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box; font-weight: 600;">Connecting</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To connect, simply tap the <span style="box-sizing: border-box; font-weight: 600;">Connect</span> button. You'll be asked if you trust the OpenVPN application. Choose <span style="box-sizing: border-box; font-weight: 600;">OK</span> to initiate the connection. To disconnect from the VPN, go back to the OpenVPN app and choose <span style="box-sizing: border-box; font-weight: 600;">Disconnect</span>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTnCgiWRvRAOnqSUDtbOvwfJAhNDmukzn985aq8Sqgd825reCJ8V_Wy_cI5GYztEG3a56i1_hNJsj-UjIUlUC6JO9QTkCs3gc3tXtzmHRa-kUeEUPvPvW0ij1It5t_dAzfyJii89YLgBA/s1600/6.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTnCgiWRvRAOnqSUDtbOvwfJAhNDmukzn985aq8Sqgd825reCJ8V_Wy_cI5GYztEG3a56i1_hNJsj-UjIUlUC6JO9QTkCs3gc3tXtzmHRa-kUeEUPvPvW0ij1It5t_dAzfyJii89YLgBA/s1600/6.png" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Test Your VPN Connection</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Once everything is installed, a simple check confirms everything is working properly. Without having a VPN connection enabled, open a browser and go to <a href="https://www.dnsleaktest.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">DNSLeakTest</a>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The site will return the IP address assigned by your internet service provider and as you appear to the rest of the world. To check your DNS settings through the same website, click on <span style="box-sizing: border-box; font-weight: 600;">Extended Test</span> and it will tell you which DNS servers you are using.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now connect the OpenVPN client to your Droplet's VPN and refresh the browser. The completely different IP address of your VPN server should now appear. That is now how you appear to the world. Again, <a href="https://www.dnsleaktest.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">DNSLeakTest's</a> <span style="box-sizing: border-box; font-weight: 600;">Extended Test</span> will check your DNS settings and confirm you are now using the DNS resolvers pushed by your VPN.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-51203202865753416612016-11-13T17:16:00.000+05:002016-11-13T17:16:04.019+05:00How to Setup OpenVPN Server - [Complete]<div dir="ltr" style="text-align: left;" trbidi="on">
<h1 style="background-color: white; box-sizing: border-box; font-family: LyonDisplay, Georgia, serif; font-weight: 400; line-height: 1.3em; margin: 0px 0px 5px;">
How To Set Up an OpenVPN Server on Ubuntu 16.04 <span style="font-size: small;">or any other Linux flavor, just follow on!</span></h1>
<div>
<span style="font-size: small;"><br /></span></div>
<div>
<span style="font-size: small;"><br /></span></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To start off, we will install OpenVPN onto our server. OpenVPN is available in Ubuntu's default repositories, so we can use <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">apt</code> for the installation. We will also be installing the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">easy-rsa</code> package, which will help us set up an internal CA (certificate authority) for use with our VPN.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To update your server's package index and install the necessary packages type:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo apt-get update
</li>
<li style="box-sizing: border-box;">sudo apt-get install openvpn easy-rsa
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The needed software is now on the server, ready to be configured.</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 2: Set Up the CA Directory</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
OpenVPN is an TLS/SSL VPN. This means that it utilizes certificates in order to encrypt traffic between the server and clients. In order to issue trusted certificates, we will need to set up our own simple certificate authority (CA).</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To begin, we can copy the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">easy-rsa</code> template directory into our home directory with the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">make-cadir</code> command:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">make-cadir ~/openvpn-ca
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Move into the newly created directory to begin configuring the CA:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">cd ~/openvpn-ca
</li>
</ul>
</code></pre>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 3: Configure the CA Variables</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To configure the values our CA will use, we need to edit the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">vars</code> file within the directory. Open that file now in your text editor:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">nano vars
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Inside, you will find some variables that can be adjusted to determine how your certificates will be created. We only need to worry about a few of these.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Towards the bottom of the file, find the settings that set field defaults for new certificates. It should look something like this:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/openvpn-ca/vars</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">. . .
export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_EMAIL="me@myhost.mydomain"
export KEY_OU="MyOrganizationalUnit"
. . .
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Edit the values in red to whatever you'd prefer, but do not leave them blank:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/openvpn-ca/vars</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">. . .
export KEY_COUNTRY="<span style="box-sizing: border-box;">US</span>"
export KEY_PROVINCE="<span style="box-sizing: border-box;">NY</span>"
export KEY_CITY="<span style="box-sizing: border-box;">New York City</span>"
export KEY_ORG="<span style="box-sizing: border-box;">DigitalOcean</span>"
export KEY_EMAIL="<span style="box-sizing: border-box;">admin@example.com</span>"
export KEY_OU="<span style="box-sizing: border-box;">Community</span>"
. . .
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
While we are here, we will also edit the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">KEY_NAME</code> value just below this section, which populates the subject field. To keep this simple, we'll call it <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">server</code> in this guide:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/openvpn-ca/vars</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">export KEY_NAME="<span style="box-sizing: border-box;">server</span>"
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
When you are finished, save and close the file.</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 4: Build the Certificate Authority</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now, we can use the variables we set and the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">easy-rsa</code> utilities to build our certificate authority.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Ensure you are in your CA directory, and then source the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">vars</code> file you just edited:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">cd ~/openvpn-ca
</li>
<li style="box-sizing: border-box;">source vars
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
You should see the following if it was sourced correctly:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><div style="box-sizing: border-box;">
Output</div>
NOTE: If you run ./clean-all, I will be doing a rm -rf on /home/sammy/openvpn-ca/keys
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Make sure we're operating in a clean environment by typing:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">./clean-all
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now, we can build our root CA by typing:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">./build-ca
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This will initiate the process of creating the root certificate authority key and certificate. Since we filled out the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">vars</code> file, all of the values should be populated automatically. Just press <span style="box-sizing: border-box; font-weight: 600;">ENTER</span> through the prompts to confirm the selections:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><div style="box-sizing: border-box;">
Output</div>
Generating a 2048 bit RSA private key
..........................................................................................+++
...............................+++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [US]:
State or Province Name (full name) [NY]:
Locality Name (eg, city) [New York City]:
Organization Name (eg, company) [DigitalOcean]:
Organizational Unit Name (eg, section) [Community]:
Common Name (eg, your name or your server's hostname) [DigitalOcean CA]:
Name [server]:
Email Address [admin@email.com]:
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We now have a CA that can be used to create the rest of the files we need.</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 5: Create the Server Certificate, Key, and Encryption Files</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we will generate our server certificate and key pair, as well as some additional files used during the encryption process.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Start by generating the OpenVPN server certificate and key pair. We can do this by typing:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 600;">Note</span>: If you choose a name other than <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">server</code> here, you will have to adjust some of the instructions below. For instance, when copying the generated files to the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn</code> directroy, you will have to substitute the correct names. You will also have to modify the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn/server.conf</code> file later to point to the correct <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.crt</code> and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.key</code> files.</span></div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">./build-key-server server
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Once again, the prompts will have default values based on the argument we just passed in (<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">server</code>) and the contents of our <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">vars</code> file we sourced.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Feel free to accept the default values by pressing <span style="box-sizing: border-box; font-weight: 600;">ENTER</span>. Do <em style="box-sizing: border-box;">not</em> enter a challenge password for this setup. Towards the end, you will have to enter <span style="box-sizing: border-box; font-weight: 600;">y</span> to two questions to sign and commit the certificate:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><div style="box-sizing: border-box;">
Output</div>
. . .
Certificate is to be certified until May 1 17:51:16 2026 GMT (3650 days)
Sign the certificate? [y/n]:<span style="box-sizing: border-box;">y</span>
1 out of 1 certificate requests certified, commit? [y/n]<span style="box-sizing: border-box;">y</span>
Write out database with 1 new entries
Data Base Updated
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we'll generate a few other items. We can generate a strong Diffie-Hellman keys to use during key exchange by typing:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">./build-dh
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This might take a few minutes to complete.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Afterwards, we can generate an HMAC signature to strengthen the server's TLS integrity verification capabilities:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">openvpn --genkey --secret keys/ta.key
</li>
</ul>
</code></pre>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 6: Generate a Client Certificate and Key Pair</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we can generate a client certificate and key pair. Although this can be done on the client machine and then signed by the server/CA for security purposes, for this guide we will generate the signed key on the server for the sake of simplicity.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We will generate a single client key/certificate for this guide, but if you have more than one client, you can repeat this process as many times as you'd like. Pass in a unique value to the script for each client.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Because you may come back to this step at a later time, we'll re-source the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">vars</code>file. We will use <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">client1</code> as the value for our first certificate/key pair for this guide.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To produce credentials without a password, to aid in automated connections, use the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">build-key</code> command like this:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">cd ~/openvpn-ca
</li>
<li style="box-sizing: border-box;">source vars
</li>
<li style="box-sizing: border-box;">./build-key <span style="box-sizing: border-box;">client1</span>
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If instead, you wish to create a password-protected set of credentials, use the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">build-key-pass</code> command:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">cd ~/openvpn-ca
</li>
<li style="box-sizing: border-box;">source vars
</li>
<li style="box-sizing: border-box;">./build-key-pass <span style="box-sizing: border-box;">client1</span>
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Again, the defaults should be populated, so you can just hit <span style="box-sizing: border-box; font-weight: 600;">ENTER</span> to continue. Leave the challenge password blank and make sure to enter <span style="box-sizing: border-box; font-weight: 600;">y</span> for the prompts that ask whether to sign and commit the certificate.</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 7: Configure the OpenVPN Service</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we can begin configuring the OpenVPN service using the credentials and files we've generated.</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Copy the Files to the OpenVPN Directory</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To begin, we need to copy the files we need to the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn</code> configuration directory.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We can start with all of the files that we just generated. These were placed within the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">~/openvpn-ca/keys</code> directory as they were created. We need to move our CA cert and key, our server cert and key, the HMAC signature, and the Diffie-Hellman file:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">cd ~/openvpn-ca/keys
</li>
<li style="box-sizing: border-box;">sudo cp ca.crt ca.key server.crt server.key ta.key dh2048.pem /etc/openvpn
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we need to copy and unzip a sample OpenVPN configuration file into configuration directory so that we can use it as a basis for our setup:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf
</li>
</ul>
</code></pre>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Adjust the OpenVPN Configuration</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now that our files are in place, we can modify the server configuration file:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo nano /etc/openvpn/server.conf
</li>
</ul>
</code></pre>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Basic Configuration</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
First, find the HMAC section by looking for the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">tls-auth</code> directive. Remove the "<span style="box-sizing: border-box; font-weight: 600;">;</span>" to uncomment the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">tls-auth</code> line. Below this, add the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">key-direction</code>parameter set to "0":</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">tls-auth ta.key 0 # This file is secret
<span style="box-sizing: border-box;">key-direction 0</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, find the section on cryptographic ciphers by looking for the commented out <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">cipher</code> lines. The <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">AES-128-CBC</code> cipher offers a good level of encryption and is well supported. Remove the "<span style="box-sizing: border-box; font-weight: 600;">;</span>" to uncomment the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">cipher AES-128-CBC</code> line:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">cipher AES-128-CBC
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Below this, add an <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">auth</code> line to select the HMAC message digest algorithm. For this, <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">SHA256</code> is a good choice:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">auth SHA256
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Finally, find the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">user</code> and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">group</code> settings and remove the "<span style="box-sizing: border-box; font-weight: 600;">;</span>" at the beginning of to uncomment those lines:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">user nobody
group nogroup
</code></pre>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
(Optional) Push DNS Changes to Redirect All Traffic Through the VPN</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The settings above will create the VPN connection between the two machines, but will not force any connections to use the tunnel. If you wish to use the VPN to route all of your traffic, you will likely want to push the DNS settings to the client computers.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
You can do this, uncomment a few directives that will configure client machines to redirect all web traffic through the VPN. Find the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">redirect-gateway</code> section and remove the semicolon "<span style="box-sizing: border-box; font-weight: 600;">;</span>" from the beginning of the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">redirect-gateway</code> line to uncomment it:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">push "redirect-gateway def1 bypass-dhcp"
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Just below this, find the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">dhcp-option</code> section. Again, remove the "<span style="box-sizing: border-box; font-weight: 600;">;</span>" from in front of both of the lines to uncomment them:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This should assist clients in reconfiguring their DNS settings to use the VPN tunnel for as the default gateway.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
(Optional) Adjust the Port and Protocol</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
By default, the OpenVPN server uses port 1194 and the UDP protocol to accept client connections. If you need to use a different port because of restrictive network environments that your clients might be in, you can change the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">port</code>option. If you are not hosting web content your OpenVPN server, port 443 is a popular choice since this is usually allowed through firewall rules.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"># Optional!
port <span style="box-sizing: border-box;">443</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Often if the protocol will be restricted to that port as well. If so, change <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">proto</code>from UDP to TCP:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"># Optional!
proto <span style="box-sizing: border-box;">tcp</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you have no need to use a different port, it is best to leave these two settings as their default.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
(Optional) Point to Non-Default Credentials</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you selected a different name during the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">./build-key-server</code> command earlier, modify the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">cert</code> and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">key</code> lines that you see to point to the appropriate <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.crt</code> and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.key</code> files. If you used the default <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">server</code>, this should already be set correctly:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/openvpn/server.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">cert <span style="box-sizing: border-box;">server</span>.crt
key <span style="box-sizing: border-box;">server</span>.key
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
When you are finished, save and close the file.</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 8: Adjust the Server Networking Configuration</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we need to adjust some aspects of the server's networking so that OpenVPN can correctly route traffic.</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Allow IP Forwarding</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
First, we need to allow the server to forward traffic. This is fairly essential to the functionality we want our VPN server to provide.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We can adjust this setting by modifying the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/sysctl.conf</code> file:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo nano /etc/sysctl.conf
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Inside, look for the line that sets <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">net.ipv4.ip_forward</code>. Remove the "<span style="box-sizing: border-box; font-weight: 600;">#</span>" character from the beginning of the line to uncomment that setting:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/sysctl.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">net.ipv4.ip_forward=1
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Save and close the file when you are finished.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
To read the file and adjust the values for the current session, type:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo sysctl -p
</li>
</ul>
</code></pre>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Adjust the UFW Rules to Masquerade Client Connections</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you followed the Ubuntu 16.04 initial server setup guide in the prerequisites, you should have the UFW firewall in place. Regardless of whether you use the firewall to block unwanted traffic (which you almost always should do), we need the firewall in this guide to manipulate some of the traffic coming into the server. We need to modify the rules file to set up masquerading, an <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">iptables</code> concept that provides on-the-fly dynamic NAT to correctly route client connections.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Before we open the firewall configuration file to add masquerading, we need to find the public network interface of our machine. To do this, type:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">ip route | grep default
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Your public interface should follow the word "dev". For example, this result shows the interface named <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">wlp11s0</code>, which is highlighted below:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><div style="box-sizing: border-box;">
Output</div>
default via 203.0.113.1 dev <span style="box-sizing: border-box;">eth0</span> proto static metric 600
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
When you have the interface associated with your default route, open the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/ufw/before.rules</code> file to add the relevant configuration:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo nano /etc/ufw/before.rules
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This file handles configuration that should be put into place before the conventional UFW rules are loaded. Towards the top of the file, add the highlighted lines below. This will set the default policy for the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">POSTROUTING</code> chain in the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">nat</code> table and masquerade any traffic coming from the VPN:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<span style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: 600;">Note</span>: Remember to replace <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">eth0</code> in the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">-A POSTROUTING</code> line below with the interface you found in the above command.</span></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/ufw/before.rules</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">#
# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
# ufw-before-input
# ufw-before-output
# ufw-before-forward
#
<span style="box-sizing: border-box;"># START OPENVPN RULES</span>
<span style="box-sizing: border-box;"># NAT table rules</span>
<span style="box-sizing: border-box;">*nat</span>
<span style="box-sizing: border-box;">:POSTROUTING ACCEPT [0:0]</span>
<span style="box-sizing: border-box;"># Allow traffic from OpenVPN client to eth0</span>
<span style="box-sizing: border-box;">-A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADE</span>
<span style="box-sizing: border-box;">COMMIT</span>
<span style="box-sizing: border-box;"># END OPENVPN RULES</span>
# Don't delete these required lines, otherwise there will be errors
*filter
. . .
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Save and close the file when you are finished.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We need to tell UFW to allow forwarded packages by default as well. To do this, we will open the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/default/ufw</code> file:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo nano /etc/default/ufw
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Inside, find the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">DEFAULT_FORWARD_POLICY</code> directive. We will change the value from <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">DROP</code> to <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">ACCEPT</code>:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
/etc/default/ufw</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">DEFAULT_FORWARD_POLICY="<span style="box-sizing: border-box;">ACCEPT</span>"
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Save and close the file when you are finished.</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Open the OpenVPN Port and Enable the Changes</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we'll adjust the firewall itself to allow traffic to OpenVPN.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you did not change the port and protocol in the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn/server.conf</code> file, you will need to open up UDP traffic to port 1194. If you modified the port and/or protocol, substitute the values you selected here.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We'll also add the SSH port in case you forgot to add it when following the prerequisite tutorial:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo ufw allow <span style="box-sizing: border-box;">1194</span>/<span style="box-sizing: border-box;">udp</span>
</li>
<li style="box-sizing: border-box;">sudo ufw allow OpenSSH
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now, we can disable and re-enable UFW to load the changes from all of the files we've modified:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo ufw disable
</li>
<li style="box-sizing: border-box;">sudo ufw enable
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Our server is now configured to correctly handle OpenVPN traffic.</div>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 9: Start and Enable the OpenVPN Service</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We're finally ready to start the OpenVPN service on our server. We can do this using systemd.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We need to start the OpenVPN server by specifying our configuration file name as an instance variable after the systemd unit file name. Our configuration file for our server is called <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn/<span style="box-sizing: border-box;">server</span>.conf</code>, so we will add <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;"><span style="box-sizing: border-box;">@server</span></code> to end of our unit file when calling it:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo systemctl start openvpn@<span style="box-sizing: border-box;">server</span>
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Double-check that the service has started successfully by typing:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo systemctl status openvpn@server
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If everything went well, your output should look something that looks like this:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><div style="box-sizing: border-box;">
Output</div>
● openvpn@server.service - OpenVPN connection to server
Loaded: loaded (/lib/systemd/system/openvpn@.service; disabled; vendor preset: enabled)
Active: <span style="box-sizing: border-box;">active (running)</span> since Tue 2016-05-03 15:30:05 EDT; 47s ago
Docs: man:openvpn(8)
https://community.openvpn.net/openvpn/wiki/Openvpn23ManPage
https://community.openvpn.net/openvpn/wiki/HOWTO
Process: 5852 ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid (code=exited, sta
Main PID: 5856 (openvpn)
Tasks: 1 (limit: 512)
CGroup: /system.slice/system-openvpn.slice/openvpn@server.service
└─5856 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/server.conf --writepid /run/openvpn/server.pid
May 03 15:30:05 openvpn2 ovpn-server[5856]: /sbin/ip addr add dev tun0 local 10.8.0.1 peer 10.8.0.2
May 03 15:30:05 openvpn2 ovpn-server[5856]: /sbin/ip route add 10.8.0.0/24 via 10.8.0.2
May 03 15:30:05 openvpn2 ovpn-server[5856]: GID set to nogroup
May 03 15:30:05 openvpn2 ovpn-server[5856]: UID set to nobody
May 03 15:30:05 openvpn2 ovpn-server[5856]: UDPv4 link local (bound): [undef]
May 03 15:30:05 openvpn2 ovpn-server[5856]: UDPv4 link remote: [undef]
May 03 15:30:05 openvpn2 ovpn-server[5856]: MULTI: multi_init called, r=256 v=256
May 03 15:30:05 openvpn2 ovpn-server[5856]: IFCONFIG POOL: base=10.8.0.4 size=62, ipv6=0
May 03 15:30:05 openvpn2 ovpn-server[5856]: IFCONFIG POOL LIST
May 03 15:30:05 openvpn2 ovpn-server[5856]: Initialization Sequence Completed
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
You can also check that the OpenVPN <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">tun0</code> interface is available by typing:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">ip addr show tun0
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
You should see a configured interface:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><div style="box-sizing: border-box;">
Output</div>
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 100
link/none
inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0
valid_lft forever preferred_lft forever
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If everything went well, enable the service so that it starts automatically at boot:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sudo systemctl enable openvpn@server
</li>
</ul>
</code></pre>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 10: Create Client Configuration Infrastructure</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we need to set up a system that will allow us to create client configuration files easily.</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Creating the Client Config Directory Structure</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Create a directory structure within your home directory to store the files:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">mkdir -p ~/client-configs/files
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Since our client configuration files will have the client keys embedded, we should lock down permissions on our inner directory:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">chmod 700 ~/client-configs/files
</li>
</ul>
</code></pre>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Creating a Base Configuration</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, let's copy an example client configuration into our directory to use as our base configuration:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Open this new file in your text editor:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">nano ~/client-configs/base.conf
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Inside, we need to make a few adjustments.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
First, locate the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">remote</code> directive. This points the client to our OpenVPN server address. This should be the public IP address of your OpenVPN server. If you changed the port that the OpenVPN server is listening on, change <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">1194</code> to the port you selected:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/client-configs/base.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">. . .
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote <span style="box-sizing: border-box;">server_IP_address</span> <span style="box-sizing: border-box;">1194</span>
. . .
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Be sure that the protocol matches the value you are using in the server configuration:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/client-configs/base.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;">proto <span style="box-sizing: border-box;">udp</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, uncomment the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">user</code> and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">group</code> directives by removing the "<span style="box-sizing: border-box; font-weight: 600;">;</span>":</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/client-configs/base.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"># Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Find the directives that set the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">ca</code>, <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">cert</code>, and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">key</code>. Comment out these directives since we will be adding the certs and keys within the file itself:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/client-configs/base.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"># SSL/TLS parms.
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
<span style="box-sizing: border-box;">#</span>ca ca.crt
<span style="box-sizing: border-box;">#</span>cert client.crt
<span style="box-sizing: border-box;">#</span>key client.key
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Mirror the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">cipher</code> and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">auth</code> settings that we set in the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn/server.conf</code>file:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/client-configs/base.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">cipher AES-128-CBC</span>
<span style="box-sizing: border-box;">auth SHA256</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, add the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">key-direction</code> directive somewhere in the file. This <span style="box-sizing: border-box; font-weight: 600;">must</span> be set to "1" to work with the server:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/client-configs/base.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">key-direction 1</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Finally, add a few <span style="box-sizing: border-box; font-weight: 600;">commented out</span> lines. We want to include these with every config, but should only enable them for Linux clients that ship with a <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn/update-resolv-conf</code> file. This script uses the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">resolvconf</code> utility to update DNS information for Linux clients.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/client-configs/base.conf</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;"># script-security 2</span>
<span style="box-sizing: border-box;"># up /etc/openvpn/update-resolv-conf</span>
<span style="box-sizing: border-box;"># down /etc/openvpn/update-resolv-conf</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If your client is running Linux and has an <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">/etc/openvpn/update-resolv-conf</code> file, you should uncomment these lines from the generated OpenVPN client configuration file.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Save the file when you are finished.</div>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Creating a Configuration Generation Script</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Next, we will create a simple script to compile our base configuration with the relevant certificate, key, and encryption files. This will place the generated configuration in the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">~/client-configs/files</code> directory.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Create and open a file called <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">make_config.sh</code> within the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">~/client-configs</code>directory:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">nano ~/client-configs/make_config.sh
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Inside, paste the following script:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
~/client-configs/make_config.sh</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">#!/bin/bash
</span>
KEY_DIR=~/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat <span style="box-sizing: border-box;">${BASE_CONFIG}</span> \
<(<span style="box-sizing: border-box;">echo</span> <span style="box-sizing: border-box;">-e</span> <span style="box-sizing: border-box;">'<ca>'</span>) \
<span style="box-sizing: border-box;">${KEY_DIR}</span>/ca.crt \
<(<span style="box-sizing: border-box;">echo</span> <span style="box-sizing: border-box;">-e</span> <span style="box-sizing: border-box;">'</ca>\n<cert>'</span>) \
<span style="box-sizing: border-box;">${KEY_DIR}</span>/<span style="box-sizing: border-box;">${1}</span>.crt \
<(<span style="box-sizing: border-box;">echo</span> <span style="box-sizing: border-box;">-e</span> <span style="box-sizing: border-box;">'</cert>\n<key>'</span>) \
<span style="box-sizing: border-box;">${KEY_DIR}</span>/<span style="box-sizing: border-box;">${1}</span>.key \
<(<span style="box-sizing: border-box;">echo</span> <span style="box-sizing: border-box;">-e</span> <span style="box-sizing: border-box;">'</key>\n<tls-auth>'</span>) \
<span style="box-sizing: border-box;">${KEY_DIR}</span>/ta.key \
<(<span style="box-sizing: border-box;">echo</span> <span style="box-sizing: border-box;">-e</span> <span style="box-sizing: border-box;">'</tls-auth>'</span>) \
> <span style="box-sizing: border-box;">${OUTPUT_DIR}</span>/<span style="box-sizing: border-box;">${1}</span>.ovpn
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Save and close the file when you are finished.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Mark the file as executable by typing:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">chmod 700 ~/client-configs/make_config.sh
</li>
</ul>
</code></pre>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 11: Generate Client Configurations</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now, we can easily generate client configuration files.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you followed along with the guide, you created a client certificate and key called <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">client1.crt</code> and <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">client1.key</code> respectively by running the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">./build-key <span style="box-sizing: border-box;">client1</span></code> command in step 6. We can generate a config for these credentials by moving into our <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">~/client-configs</code> directory and using the script we made:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">cd ~/client-configs
</li>
<li style="box-sizing: border-box;">./make_config.sh <span style="box-sizing: border-box;">client1</span>
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If everything went well, we should have a <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">client1.ovpn</code> file in our <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">~/client-configs/files</code> directory:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">ls ~/client-configs/files
</li>
</ul>
</code></pre>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><div style="box-sizing: border-box;">
Output</div>
client1.ovpn
</code></pre>
<h3 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.2em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Transferring Configuration to Client Devices</h3>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
We need to transfer the client configuration file to the relevant device. For instance, this could be your local computer or a mobile device.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
While the exact applications used to accomplish this transfer will depend on your choice and device's operating system, you want the application to use SFTP (SSH file transfer protocol) or SCP (Secure Copy) on the backend. This will transport your client's VPN authentication files over an encrypted connection.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Here is an example SFTP command using our <span style="box-sizing: border-box;">client1.ovpn</span> example. This command can be run from your local computer (OS X or Linux). It places the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.ovpn</code> file in your home directory:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><ul style="box-sizing: border-box; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;">sftp <span style="box-sizing: border-box;">sammy</span>@<span style="box-sizing: border-box;">openvpn_server_ip</span>:client-configs/files/client1.ovpn ~/
</li>
</ul>
</code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Here are several tools and tutorials for securely transferring files from the server to a local computer:</div>
<ul style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 10px; margin-top: 0px;">
<li style="box-sizing: border-box;"><a href="http://winscp.net/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">WinSCP</a></li>
<li style="box-sizing: border-box;"><a href="https://www.digitalocean.com/community/tutorials/how-to-use-sftp-to-securely-transfer-files-with-a-remote-server" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">How To Use SFTP to Securely Transfer Files with a Remote Server</a></li>
<li style="box-sizing: border-box;"><a href="https://www.digitalocean.com/community/tutorials/how-to-use-filezilla-to-transfer-and-manage-files-securely-on-your-vps" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">How To Use Filezilla to Transfer and Manage Files Securely on your VPS</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Step 12: Install the Client Configuration</h2>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now, we'll discuss how to install a client VPN profile on Windows, OS X, iOS, and Android. None of these client instructions are dependent on one another, so feel free to skip to whichever is applicable to you.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The OpenVPN connection will be called whatever you named the <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">.ovpn</code> file. In our example, this means that the connection will be called <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">client1.ovpn</code> for the first client file we generated.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
For installing clients , see my other post!</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<b>Good luck! </b></div>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-89713987697609493782016-11-13T17:13:00.003+05:002016-11-13T17:13:51.156+05:00Mod_Security For WordPress - How to!<div dir="ltr" style="text-align: left;" trbidi="on">
<h1 style="background-color: white; box-sizing: border-box; font-family: LyonDisplay, Georgia, serif; font-weight: 400; line-height: 1.3em; margin: 0px 0px 5px;">
Mod_Security For WordPress</h1>
<div>
<br /></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This is so far what I have included in my Mod_Security Custom/Whitelist setting which makes my site act kinda nicely so far:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><LocationMatch "/wp-admin/post.php">
SecRuleRemoveById 300015 300016 300017 950907 950005 950006 960008 960011 960904
SecRuleRemoveById phpids-17
SecRuleRemoveById phpids-20
SecRuleRemoveById phpids-21
SecRuleRemoveById phpids-30
SecRuleRemoveById phpids-61
</LocationMatch>
<LocationMatch "/wp-admin/admin-ajax.php">
SecRuleRemoveById 300015 300016 300017 950907 950005 950006 960008 960011 960904
SecRuleRemoveById phpids-17
SecRuleRemoveById phpids-20
SecRuleRemoveById phpids-21
SecRuleRemoveById phpids-30
SecRuleRemoveById phpids-61
</LocationMatch>
<LocationMatch "/wp-admin/page.php">
SecRuleRemoveById 300015 300016 300017 950907 950005 950006 960008 960011 960904
SecRuleRemoveById phpids-17
SecRuleRemoveById phpids-20
SecRuleRemoveById phpids-21
SecRuleRemoveById phpids-30
SecRuleRemoveById phpids-61
</LocationMatch>
<LocationMatch "/wp-admin/options.php">
SecRuleRemoveById 300015 300016 300017 950907 950005 950006 960008 960011 960904
SecRuleRemoveById phpids-17
SecRuleRemoveById phpids-20
SecRuleRemoveById phpids-21
SecRuleRemoveById phpids-30
SecRuleRemoveById phpids-61
</LocationMatch>
<LocationMatch "/wp-admin/theme-editor.php">
SecRuleRemoveById 300015 300016 300017 950907 950005 950006 960008 960011 960904
SecRuleRemoveById phpids-17
SecRuleRemoveById phpids-20
SecRuleRemoveById phpids-21
SecRuleRemoveById phpids-30
SecRuleRemoveById phpids-61
</LocationMatch>
<LocationMatch "/wp-includes/">
SecRuleRemoveById 960010 960012 950006
SecRuleRemoveById phpids-17
SecRuleRemoveById phpids-20
SecRuleRemoveById phpids-21
SecRuleRemoveById phpids-30
SecRuleRemoveById phpids-61
</LocationMatch></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Some of these rules are WordPress Specific while some are plugin specific. Do you have anything else included that makes your wordpress & the plugins act nicely with Mod_Security? If yes, then please post it so that we can compile the ultimate mod_security and wordpress specific whitelist ruleset!</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Good luck!</div>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-87662422061389004632016-11-13T17:12:00.003+05:002016-11-13T17:12:39.046+05:00PHP5 - Unable to Loading Dynamic Library [SOLVED]<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;">Error or Unable to Load Dynamic Library PHP5</span><br />
<span style="font-size: x-large;"><br /></span>
<span style="font-size: x-large;"><br /></span>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Installing more unnecessary software is generally <span style="box-sizing: border-box; font-weight: 600;">not</span> the best solution. Instead, you should fix the underlying problem. The reason these messages appear is because you are trying to load those extensions, but they are not installed. So the easy solution is simply to tell PHP to stop trying to load them:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
First, find out which files are trying to load the above extensions:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">$ grep </span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">Hrv</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">";"</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5 </span><span style="box-sizing: border-box;">|</span><span style="box-sizing: border-box;"> grep </span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">E </span><span style="box-sizing: border-box;">"extension(\s+)?="</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Example output for Ubuntu:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">/etc/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mods</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">available</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">gd</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">gd</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mods</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">available</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">pdo_sqlite</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">pdo_sqlite</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mods</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">available</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">pdo</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">pdo</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mods</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">available</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">pdo_mysql</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">pdo_mysql</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mods</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">available</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mysqli</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">mysqli</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mods</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">available</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mysql</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">mysql</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mods</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">available</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">curl</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">curl</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mods</span><span style="box-sizing: border-box;">-</span><span style="box-sizing: border-box;">available</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">sqlite3</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">sqlite3</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">conf</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">d</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">mcrypt</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">mcrypt</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">conf</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">d</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">imagick</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">imagick</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so
</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">etc</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php5</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">apache2</span><span style="box-sizing: border-box;">/</span><span style="box-sizing: border-box;">php</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">ini</span><span style="box-sizing: border-box;">:</span><span style="box-sizing: border-box;">extension</span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;">http</span><span style="box-sizing: border-box;">.</span><span style="box-sizing: border-box;">so</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now just find the files that are loading the extensions that are causing the errors and comment out those lines with a <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">;</code>. For some reason this happened to me with the default install of Ubuntu, so hopefully this helps someone else.</div>
<span style="font-size: x-large;"><br /></span>
<span style="font-size: x-large;"><br /></span>
<b>Good luck!!</b><br />
<span style="font-size: x-large;"><br /></span>
<span style="font-size: x-large;"><br /></span></div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-88683960717746906592016-11-13T16:55:00.003+05:002016-11-13T16:55:54.962+05:00How to install Softaculous on cPanel / WHM 2017 [Full Guide!]<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<h1 style="background-color: white; box-sizing: border-box; font-family: LyonDisplay, Georgia, serif; font-weight: 400; line-height: 1.3em; margin: 0px 0px 5px;">
Install Softaculous Auto Installer on WHM/cPanel</h1>
<div>
<br /></div>
<div>
<br /></div>
<div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh22FvJacJsItVsDB0sx8RazWeDXAWtr2XRM9FTlHFDFxrK03NQ6vzD-TDK_A-coGvmk21SdDZb4M4cWR0yGeKgtQOZQUH622P8mgNLxYKfKECJxjSDNZk6P5Bjec0s-dDFkTfjga2qa64/s1600/softaculous.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh22FvJacJsItVsDB0sx8RazWeDXAWtr2XRM9FTlHFDFxrK03NQ6vzD-TDK_A-coGvmk21SdDZb4M4cWR0yGeKgtQOZQUH622P8mgNLxYKfKECJxjSDNZk6P5Bjec0s-dDFkTfjga2qa64/s1600/softaculous.png" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Softaculous is an auto installer which allows you to install 350+ great scripts on your server. By using Softaculous you can install WordPress, Joomla, Drupal, PrestaShop, Magento etc. within seconds. It can be installed on a Linux VPS with WHM/cPanel, DirectAdmin, Plesk, InterWorx and H-Sphere control panel. Today we are going to show you how to install Softaculous on WHM/cPanel.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br style="box-sizing: border-box;" />Before we start, make sure that your server meets the following requirements:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
* WHM/cPanel is installed<br style="box-sizing: border-box;" />* ionCube Loader is enabled</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Just make sure that you select a CentOS VPS when you make an order at any VPS hosting service since the WHM/cPanel requires CentOS to be installed as an operating system on your VPS.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Once your VPS is ready and the WHM/cPanel is installed, you need to enable ionCube Loader. To enable ionCube Loader you need to log in to your WHM as root and navigate to Server Configuration > Tweak Settings > PHP. Check ionCube as cPanel PHP loader and click on the Save button. That’s it.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now you are ready to install Softaculous auto installer on your VPS.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Log in to your VPS via SSH and execute the following commands one by one:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;">wget -N http://files.softaculous.com/install.sh
chmod 755 install.sh
./install.sh</pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
After the installation is completed, Softaculous will become enabled in your WHM. Log in to your WHM and navigate to Plugins > Softaculous – Instant Installs. As you can see, Softaculous is enabled and can be used by your cPanel clients. They can log in to their cPanel accounts and use the one-click installer to install their favorite applications.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
One thing that is very important is that Softaculous is not free of charge. You can use the trial license but it is not recommended since Softaculous will be available to your clients only for a limited period of timeThe Premium license will cost you only $1 per month. Also, if you select this option, please note that our admins will install Softaculous on your server for you.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
More information about Softaculous auto installer you can find on the official web site. Open your favorite web browser and navigate to:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><a href="https://www.softaculous.com/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">https://www.softaculous.com/</a></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Also, if you like to check the list of applications which you can install on your VPS using Softaculous, please navigate to:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><a href="https://www.softaculous.com/softaculous/apps" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">https://www.softaculous.com/softaculous/apps</a></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The list is huge, but you can easily find the application which match your needs since all the applications are grouped into categories. Additionally, you can preview the applications one by one and check their features, ratings, screen shots and user reviews.</div>
</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-29427818470062944202016-11-13T16:52:00.000+05:002016-11-13T16:52:21.159+05:00The MySQL adapter mysqli is not available - Joomla [SOLVED!]<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 1.4em; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
<span style="color: #0b5394;">The MySQL adapter mysqli is not available</span></h2>
<div>
<br /></div>
<div>
<br /></div>
<div>
During the installation of Joomla 3.0 many users are encountering this issue, for this i have got two solutions for you :</div>
<div>
<br /></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">MySQL adapter mysqli is not available could depend from php configuration</span><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">so you could ask your host to enable mysqli support</span><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">or </span><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">check your php.ini and make sure that PHP has mysqli support enabled</span><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">or</span><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">In configuration.php change</span><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<div style="box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
Code:</div>
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">public $dbtype = 'mysql';</code></div>
<br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">to </span><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><div style="box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<div style="background-color: white; box-sizing: border-box; margin-bottom: 1.5em; margin-top: 1.5em;">
Code:</div>
</div>
</div>
<div>
<br /></div>
<div>
<span style="background-color: #d6eaf4; color: #003659; font-family: monospace; font-size: 14.4px; white-space: nowrap;">public $dbtype = 'mysqli';</span></div>
<div>
<br /></div>
<h3 style="text-align: left;">
<span style="color: #003659; font-family: monospace; font-size: large;"><span style="background-color: white; white-space: nowrap;">OR Second solution is :</span></span></h3>
<div>
<span style="color: #003659; font-family: monospace; font-size: large;"><span style="background-color: white; white-space: nowrap;"><br /></span></span></div>
<div>
<span style="color: #003659; font-family: monospace; font-size: large;"><span style="background-color: white; white-space: nowrap;"><br /></span></span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">You need go to Cpanel, then choose the PHP version, when you choose the PHP version, there are modules which allow you to install. </span><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><br style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;" /><span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">You need to tick the [Mysqli]</span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;"><br /></span></div>
<div>
<span style="background-color: white; font-family: LyonText, Georgia, serif; font-size: 18px;">Good luck!!</span></div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-72093788923806128642016-11-13T16:44:00.002+05:002016-11-13T16:44:34.165+05:00How to GREP error log for a specific domain - Linux<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
How to get error log for specific domain in linux.</h2>
<div>
<br /></div>
<div>
<br /></div>
<div>
In many cases when you are bothering with error logs and bugging out whats bothering your server, you might end up with unable to see which domain is causing the problems, in such cases all you need is a simple command for one specific domain. </div>
<div>
<br /></div>
<div>
Use:</div>
<div>
<br /></div>
<div>
<pre style="background-image: -webkit-repeating-linear-gradient(top, rgba(0, 0, 0, 0.0117647) 0px, rgba(0, 0, 0, 0.0117647) 26px, rgba(0, 0, 0, 0.0431373) 26px, rgba(0, 0, 0, 0.0431373) 52px); background-position: center top; background-repeat: repeat-x; color: #555555; direction: ltr; font-family: Consolas, "Courier New", Courier, monospace; font-size: 10pt; line-height: 26px; max-height: 500px; min-height: 30px; overflow: auto; padding: 0px 15px; word-wrap: normal;"><b> grep domainname.com /usr/local/apache/logs/error_log</b></pre>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Similarly you can get access_log and so on for the required domain :) Hope it helps.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Good luck!!</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-42700990217523298292016-11-13T16:41:00.000+05:002016-11-13T16:41:26.618+05:00WordPress SSL - How To <div dir="ltr" style="text-align: left;" trbidi="on">
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
How to Setup WordPress to Use SSL and HTTPS</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you are starting a new site and/or want to use HTTPS everywhere on your site, then you need to update your site URL.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
You can do this by going to Settings » General and updating your WordPress and site URL address fields.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4YqfskUlKTPGZvlYoiM5J149faNc-9VeFf8_cEY0f5IpUAPAmzpF19Rgo8p-zvdMOqLRcau0vow6_fqtIjdRkCdRQz2beTJ4chhSbHnQKMYxjMyik0wMUaAnN-hqFvAI83xuIDyrC6sg/s1600/updating-urls.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4YqfskUlKTPGZvlYoiM5J149faNc-9VeFf8_cEY0f5IpUAPAmzpF19Rgo8p-zvdMOqLRcau0vow6_fqtIjdRkCdRQz2beTJ4chhSbHnQKMYxjMyik0wMUaAnN-hqFvAI83xuIDyrC6sg/s1600/updating-urls.png" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now if you’re adding SSL to your existing site, then you need to setup WordPress SSL redirect from HTTP to HTTPS.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
You can do this by adding the following code in your <a href="http://www.wpbeginner.com/beginners-guide/why-you-cant-find-htaccess-file-on-your-wordpress-site/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">.htaccess file</a>:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">1</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;"><IfModule mod_rewrite.c></code></div>
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">2</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">RewriteEngine On</code></div>
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">3</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">RewriteCond %{SERVER_PORT} 80</code></div>
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">4</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">RewriteRule ^(.*)$ https:</code></div>
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">5</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;"></IfModule></code></div>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Don’t forget to replace yoursite.com with your site URL.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you are on nginx servers (most users are not), you would add the following to redirect from HTTP to HTTPS:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">1</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">server {</code></div>
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">2</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">listen 80;</code></div>
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">3</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">server_name yoursite.com www.yoursite.com;</code></div>
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">4</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">return</code> <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">301 https:</code></div>
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">5</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">}</code></div>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
By following these steps, you will avoid the WordPress HTTPS not working error because all your site URL and content will be on SSL.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you want to add SSL and HTTPS on your WordPress multi-site admin area or login pages, then you need to configure SSL in wp-config.php file.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Simply add the following code above the “That’s all, stop editing!” line in your wp-config.php file:</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box;">
<code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">1</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">define(</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">'FORCE_SSL_ADMIN'</code><code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">, true);</code></div>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This wp-config.php SSL trick works for single sites as well as multi-sites.</div>
<h4 style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; line-height: 1.1em; margin-bottom: 0.7em; margin-top: 2em;">
Setup SSL and WordPress HTTPS on Exclusive Pages</h4>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Now if for some reason, you only want to add HTTPS and SSL on specific pages of your site, then you would need the plugin called <a href="https://wordpress.org/plugins/wordpress-https/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">WordPress HTTPS (SSL)</a>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
First thing you need to do is install and activate the WordPress HTTPS (SSL) plugin.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Please note that this plugin hasn’t been updated for a while, but it works fine and is safe to use. See our guide on <a href="http://www.wpbeginner.com/opinion/should-you-install-plugins-not-tested-with-your-wordpress-version/" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">installing plugins not tested with your WordPress version</a> for more information.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Upon activation the plugin will add a new menu item labeled HTTPS in your WordPress admin. You can click it to visit the plugin’s settings page.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4kifP4fbmb2noktEhL6YmBiJ6g1qmNhk7fBSKkEzuKNXtrHQu4zQ91Lff0vSpgcMETgrugY9hNnjg7LC9GlCMef0dcmmEL7dV4MStG1WtsB5K9t1Laq8YU5sP9bitcSAitvea0kuq2XQ/s1600/https-settings.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4kifP4fbmb2noktEhL6YmBiJ6g1qmNhk7fBSKkEzuKNXtrHQu4zQ91Lff0vSpgcMETgrugY9hNnjg7LC9GlCMef0dcmmEL7dV4MStG1WtsB5K9t1Laq8YU5sP9bitcSAitvea0kuq2XQ/s1600/https-settings.png" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The first option of the settings page asks you to enter your SSL host. Mostly it is your domain name. However, if you are configuring the site on a subdomain and the SSL certificate you got is for your main domain name, then you will enter the root domain. If your using a shared SSL certificate provided by your web host, then you will need to enter the host information they provided instead of your domain name.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
In some cases if you are using a non-traditional SSL host and need to use a different port, then you can add it in the port field.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Force SSL Administration setting forces WordPress to use HTTPs on all admin area pages. You need to check this box to make sure that all traffic to your WordPress admin area is secure.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
The next option is to use Force SSL Exclusively. Checking this box will only use SSL on pages where you have checked the Force SSL option. All other traffic will go to the normal HTTP url.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
This works if you only want to use SSL on specific pages like shopping cart, checkout, user account pages, etc.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Click on the save changes button to store your plugin settings.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you want to use HTTPS just for specific pages, then you need to edit those pages and check the Force SSL checkbox.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipNL9vTKPw7V97SW3kwB0Bi6y7ptytcAms_V021sr4Cn-ew4vi_2x72n2P1tIPLmjXeTKXwZlypeLiABG6vHEEs6jR6HoVlVEnVf-uj9qg2oZuBI3j9F50fxSNs_GnLATAz4ZRuxEHG8k/s1600/ssl-pages.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipNL9vTKPw7V97SW3kwB0Bi6y7ptytcAms_V021sr4Cn-ew4vi_2x72n2P1tIPLmjXeTKXwZlypeLiABG6vHEEs6jR6HoVlVEnVf-uj9qg2oZuBI3j9F50fxSNs_GnLATAz4ZRuxEHG8k/s1600/ssl-pages.png" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Once done, visit your page to ensure that you have all green light in Chrome and other browsers.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4m_JWLWEWE-bqKcyp-goMDCwK9bzFGwKtyN6zfgAUqHrf6CDwM9Eh6XhhDp8u4GUK3GZih9X4WeMFx-wlAMjLfdWOQgcINjM2ZZgtQBGZ0GcQsNuD5ZFauLBNX8fNq86IduREvQP8GJs/s1600/chromesslerror.gif" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4m_JWLWEWE-bqKcyp-goMDCwK9bzFGwKtyN6zfgAUqHrf6CDwM9Eh6XhhDp8u4GUK3GZih9X4WeMFx-wlAMjLfdWOQgcINjM2ZZgtQBGZ0GcQsNuD5ZFauLBNX8fNq86IduREvQP8GJs/s1600/chromesslerror.gif" /></a></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Good luck!!</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-756292245942249722016-11-13T16:35:00.001+05:002016-11-13T16:35:31.184+05:00How to flush data to browser but continue executing in PHP<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Edit: For me the best thing to do is to use SSE's , server-sided events, and they work just great!!</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
ob_flush writes the buffer. In other words, ob_flush tells PHP to give Apache (or nginx/lighttpd/whatever) the output and then for PHP to forget about it. Once Apache has the output, it does whatever it wants with it. (In other words, after ob_flush it's out of your control whether or not it gets immediately written to the browser).</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
So, short answer: There's no guaranteed way to do that.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Just a guess, you're likely looking for AJAX. Whenever people are trying to manipulate when page content loads as you're doing, AJAX is almost always the correct path.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you want to continue a task in the background, you can use ignore_user_abort, however, that is often not the optimal approach. You essentially lose control over that thread, and in my opinion, a web server thread is not where heavy processing belongs.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I would try to extract it out of the web facing stuff. This could mean a cron entry or just spawning a background process from inside of PHP (a process that though started from inside of script execution will not die with the script, and the script will not wait for it to finish before dying).</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
If you do go that route, it will mean that you can even make some kind of status system if necessary. Then you could monitor the execution and give the user periodic updates on the progress. (Technically you could make a status system with a ignore_user_abort-ed script too, but it doesn't seem as clean to me.)</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
I have done this in the past and this is how I solved it:</div>
<pre style="background-color: #fafafa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 20px; margin-top: 20px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code style="background: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; display: inline-block; font-size: inherit; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"><span style="box-sizing: border-box;">ob_start</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">/*
* Generate your output here
*/</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// Ignore connection-closing by the client/user</span><span style="box-sizing: border-box;">
ignore_user_abort</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">true</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// Set your timelimit to a length long enough for your script to run, </span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// but not so long it will bog down your server in case multiple versions run </span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// or this script get's in an endless loop.</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">if</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">!</span><span style="box-sizing: border-box;">ini_get</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'safe_mode'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">&&</span><span style="box-sizing: border-box;"> strpos</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">ini_get</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'disable_functions'</span><span style="box-sizing: border-box;">),</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">'set_time_limit'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">===</span><span style="box-sizing: border-box;"> FALSE
</span><span style="box-sizing: border-box;">){</span><span style="box-sizing: border-box;">
set_time_limit</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">60</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// Get your output and send it to the client</span><span style="box-sizing: border-box;">
$content </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> ob_get_contents</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Get the content of the output buffer</span><span style="box-sizing: border-box;">
ob_end_clean</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Close current output buffer</span><span style="box-sizing: border-box;">
$len </span><span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box;"> strlen</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$content</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Get the length</span><span style="box-sizing: border-box;">
header</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">'Connection: close'</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Tell the client to close connection</span><span style="box-sizing: border-box;">
header</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">"Content-Length: $len"</span><span style="box-sizing: border-box;">);</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Close connection after $len characters</span><span style="box-sizing: border-box;">
echo $content</span><span style="box-sizing: border-box;">;</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Output content</span><span style="box-sizing: border-box;">
flush</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">// Force php-output-cache to flush to browser.</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// See caveats below.</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">// Optional: kill all other output buffering</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">while</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">ob_get_level</span><span style="box-sizing: border-box;">()</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">></span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">0</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;"> </span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">
ob_end_clean</span><span style="box-sizing: border-box;">();</span><span style="box-sizing: border-box;">
</span><span style="box-sizing: border-box;">}</span></code></pre>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
As I said in a couple of comments before, you should watch out for gzipping your content, since that will alter the length of your content, but not change the header about it. It also can buffer your output, so it won't get send to the client instantly.<br style="box-sizing: border-box;" />You could try letting apache know to not gzip your content by using <code style="background: rgb(214, 234, 244); border-radius: 3px; box-sizing: border-box; color: #003659; display: inline; font-size: 0.8em; line-height: 1.2em; margin-bottom: 1px; margin-top: 1px; overflow: auto; padding: 2px 4px; white-space: nowrap; word-wrap: break-word;">apache_setenv('no-gzip', '1');</code>. But this will not work if you use rewrite-rules to go to your page, since then it will also modify those environment variables. At least, it did so for me.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
See more caveats about flushing your content to the user in the <a href="http://www.php.net/manual/en/function.flush.php" style="background: 0px 0px; box-sizing: border-box; color: #0074bf; text-decoration: none;">manual</a>.</div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; font-family: LyonText, Georgia, serif; font-size: 18px; margin-bottom: 1.5em; margin-top: 1.5em;">
Good luck!!</div>
</div>
ehsanhttp://www.blogger.com/profile/07560853553755173775noreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-56047859764179820622016-05-12T05:45:00.002+05:002016-05-12T05:45:55.266+05:00Best Linux Security Tools 2016<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<br />
<u>Always trying for a more robust tool to assist you in your work?</u><br />
<br />
If there's one web site UN agency is aware of what's happening within the field of security tools, it is ToolsWatch. The site covers new tools, and promotes existing projects once they unharness a brand new version.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqh4rWgGkJxSB6UVB272Xq8JbayGtdRfsiw23dFag_2SOvShbCGHyJXGOucAk_Mg18QOo1p90HD6wq1Wu6yN4gLdDNMSIX7kW62gICEfgNVnnSJfWaGcmxMh2LT7jRhgNakkdoHYoIOUk/s1600/dataprotection2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="139" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqh4rWgGkJxSB6UVB272Xq8JbayGtdRfsiw23dFag_2SOvShbCGHyJXGOucAk_Mg18QOo1p90HD6wq1Wu6yN4gLdDNMSIX7kW62gICEfgNVnnSJfWaGcmxMh2LT7jRhgNakkdoHYoIOUk/s200/dataprotection2.jpg" width="200" /></a></div>
<br />
<br />
Here is the list of Best Security Auditing Tools so far:<br />
<br />
<ul style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, Tahoma, Verdana, Arial; font-size: 16.0016px; font-stretch: inherit; line-height: 25.6026px; list-style: square inside; margin: 0px 0px 1.5em 2em; padding: 0px; vertical-align: baseline;">
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">01</strong> – <a href="https://www.owasp.org/index.php/OWASP_Zed_Attack_Proxy_Project" target="_blank">OWASP ZAP – Zed Attack Proxy Project</a> (+5↑)</li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">02</strong> – <span style="font-size: 16.0016px; line-height: 25.6026px;"><a href="http://beefproject.com/" target="_blank">BeEF – The Browser Exploitation Framework </a>(+7<span style="font-size: 16.0016px; line-height: 25.6026px;">↑</span>)</span></li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">03</strong> – <span style="font-size: 16.0016px; line-height: 25.6026px;"><a href="https://www.hex-rays.com/products/ida" target="_blank">IDA Pro</a> (NEW)</span></li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">04</strong> – <a href="https://www.faradaysec.com/" target="_blank">Faraday</a> (NEW)</li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">05</strong> –<span style="font-size: 16.0016px; line-height: 25.6026px;"><a href="https://cisofy.com/lynis/" target="_blank">Lynis</a> (+4</span><span style="font-family: inherit; font-size: 16.0016px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 25.6026px;">↑)</span><span style="font-family: inherit; font-size: 16.0016px; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 25.6026px;"> </span></li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">06</strong> – <a href="https://portswigger.net/burp/" target="_blank">Burp Suite </a>(NEW)</li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">07</strong> – <a href="https://www.winitor.com/" target="_blank">PeStudio</a> (-1↓)</li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">08</strong> – <a href="https://nmap.org/" target="_blank">Nmap</a> (+6↑)</li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">09</strong> – <span style="font-size: 16.0016px; line-height: 25.6026px;"><a href="https://github.com/haka-security/hak" target="_blank">Haka</a> (NEW)</span></li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><strong style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">10</strong> – <a href="https://www.owasp.org/index.php/OWASP_OWTF" target="_blank">OWASP Offensive (Web) Testing Framework</a> (-2↓)</li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><b>11</b><span style="font-weight: inherit;">– <a href="http://www.tenable.com/products/nessus-vulnerability-scanner" target="_blank">NESSUS Vulnerbility Scanner</a> (+10↓)</span></li>
<li style="border: 0px; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"><b>12</b><span style="font-weight: inherit;">–<a href="https://www.metasploit.com/" target="_blank"> METASploit Framework (Best so Far) </a>(+12↓)</span></li>
</ul>
<div>
<span style="color: #222222; font-family: "helvetica" , "tahoma" , "verdana" , "arial";"><span style="font-size: 16.0016px; line-height: 25.6026px;"><br /></span></span></div>
<div>
<span style="color: #222222; font-family: "helvetica" , "tahoma" , "verdana" , "arial";"><span style="font-size: 16.0016px; line-height: 25.6026px;">Got some more? </span></span><span style="color: #222222; font-family: "helvetica" , "tahoma" , "verdana" , "arial"; font-size: 16.0016px; line-height: 25.6026px;">Drop comments! Also let me know which tools you want me to write tutorials about, i will be writing detail tutorials on those tools :)</span></div>
<div>
<span style="color: #222222; font-family: "helvetica" , "tahoma" , "verdana" , "arial"; font-size: 16.0016px; line-height: 25.6026px;"><br /></span></div>
<div>
<span style="color: #222222; font-family: "helvetica" , "tahoma" , "verdana" , "arial"; font-size: 16.0016px; line-height: 25.6026px;"><br /></span></div>
<div>
<span style="color: #222222; font-family: "helvetica" , "tahoma" , "verdana" , "arial"; font-size: 16.0016px; line-height: 25.6026px;">Best</span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8999220862553390592.post-84850878263763610342012-10-11T15:18:00.000+05:002019-06-02T02:52:48.155+05:00How to Check if a URL is UP?<div dir="ltr" style="text-align: left;" trbidi="on">
So in this small quick tutorial i am going to tell you a very short way to determine weather a website is up or not? We will be using the PHP's cURL library in order to determine so.<br />
<br />
So lets start:<br />
<br />
<br />
<blockquote class="tr_bq">
<span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">function </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">urlUP</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$url</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #88ff88; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; font-weight: bold; line-height: 15px;">{</span><br />
<span class="whitespace" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;"> </span><span class="js-comment" style="color: #aa7700; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">//Validate the URL first!</span><br />
<span class="whitespace" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;"> </span><span class="js-keyword" style="color: #770088; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">if</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">filter_var</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$url</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">,</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">FILTER_VALIDATE_URL</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">{</span><br />
<span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$handle </span><span class="js-operator" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">= </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">curl_init</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">urldecode</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$url</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">curl_setopt</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$handle</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">, </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">CURLOPT_RETURNTRANSFER</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">, </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">TRUE</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$response </span><span class="js-operator" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">= </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">curl_exec</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$handle</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$httpCode </span><span class="js-operator" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">= </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">curl_getinfo</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$handle</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">, </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">CURLINFO_HTTP_CODE</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-keyword" style="color: #770088; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">if</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$httpCode </span><span class="js-operator" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">>= </span><span class="js-atom" style="color: #228811; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">200 </span><span class="js-operator" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">&& </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$httpCode </span><span class="js-operator" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">< </span><span class="js-atom" style="color: #228811; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">400</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">{</span><br />
<span class="js-keyword" style="color: #770088; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">return </span><span class="js-atom" style="color: #228811; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">true</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">}</span><span class="js-keyword" style="color: #770088; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">else</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">{</span><br />
<span class="js-keyword" style="color: #770088; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">return </span><span class="js-atom" style="color: #228811; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">false</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">}</span><br />
<span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">curl_close</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">$handle</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">}</span><br />
<span class="whitespace" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;"> </span><br />
<span class="whitespace" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;"> </span><span class="js-punctuation" style="color: #88ff88; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; font-weight: bold; line-height: 15px;">}</span></blockquote>
<br />
This is the function we will be actually using, Lets break it down!<br />
<br />
<ol style="text-align: left;">
<li>We validate the URL through filter_var().</li>
<li>Open the cURL handle for our URL.</li>
<li>Set option in cURL to receive the incoming.</li>
<li>Execute the handle.</li>
<li>Get the HTTP code response for the executed handle.</li>
<li>If HTTP Response code is greater than or equal to 200 (200 stands for Ok) or its shorter than 400, this means website is up, return true.</li>
<li>Else return false,so that means the website is down.</li>
<li>Close the cURL handle!</li>
</ol>
<div>
To use the function you have to do the following:</div>
<div>
<br /></div>
<div>
<br /></div>
<blockquote class="tr_bq">
<span class="js-keyword" style="color: #770088; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">if </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">urlUP</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">(</span><span class="js-string" style="color: #aa2222; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">"http://geeks-wiki.blogspot.com"</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">)</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">{</span><br />
<span class="whitespace" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;"> </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">echo </span><span class="js-string" style="color: #aa2222; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">"The website is currently UP!"</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">}</span><span class="js-keyword" style="color: #770088; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">else</span><span class="js-punctuation" style="color: #88ff88; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; font-weight: bold; line-height: 15px;">{</span><br />
<span class="whitespace" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;"> </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">echo </span><span class="js-string" style="color: #aa2222; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">"The website is Down!"</span><span class="js-punctuation" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;">;</span><br />
<span class="js-punctuation" style="color: #88ff88; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; font-weight: bold; line-height: 15px;">}</span><br />
<span class="whitespace" style="color: #666666; font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;"> </span><span class="js-variable" style="font-family: "inconsolata" , "monaco" , "consolas" , "andale mono" , "bitstream vera sans mono" , "courier new" , "courier" , monospace; font-size: 12px; line-height: 15px;"></span></blockquote>
<br />
Use the code freely and leave comments, additionally you can tweak the code as well, or you can also get the HTTP Codes, match them with an array of HTTP Codes and get the specific response as well!<br />
<br />
<br />
<div style="text-align: center;">
♥ Happy Coding! ♥<br />
<br />
Source: https://www.stackflair.com/curl-error0-in-php/</div>
<div style="text-align: center;">
<br /></div>
<div>
<br /></div>
</div>
Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-8999220862553390592.post-27323933250167628762012-10-06T23:38:00.001+05:002012-10-07T00:16:28.463+05:00Best way to Avoid MySQL injection in PHP<div dir="ltr" style="text-align: left;" trbidi="on">
SQL Injection, a commonly method used by majority of hackers and which is the cause for about 70% of the hack's all over the cyber warfare. Mostly hacker uses this (mysql injection) vulnerability to exploit the website and gain access to the server thus through Admin panel or obtain the sensitive information including Credit Cards etc as well.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0eRBIfRM7bRC992q4cxhkSN5EZpViWp3Sna1luZdlJTv6kNXX_XLVC252dJwgoQvkPUnEGpLijuDwC5QKU8qFxQlu-S1b9qg5aipTk_tse5mS-ZRcf2nnKduRP8Z5zEUsmatTV_wifgc/s1600/injection_blood.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="How to prevent mysql injection in PHP?" border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0eRBIfRM7bRC992q4cxhkSN5EZpViWp3Sna1luZdlJTv6kNXX_XLVC252dJwgoQvkPUnEGpLijuDwC5QKU8qFxQlu-S1b9qg5aipTk_tse5mS-ZRcf2nnKduRP8Z5zEUsmatTV_wifgc/s200/injection_blood.png" title="How to prevent mysql injection in PHP?" width="200" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">SQL Injections are Common nowadays!</td></tr>
</tbody></table>
<br />
So here i am going to explain you the best practices you should do while writing code!<br />
First of all keep it in mind, never ever trust a user's input, you are on to your own! So it is the very first thing to keep in mind that whatever a user sends, intentionally or unintentionally, if it is not properly sanitized and exploits a vulnerability which could be possibly a MySQL injection or even a XSS attack, could let hackers give a way to intrude.<br />
<br />
<br />
<b><span style="font-size: large;">Bullet Points:</span></b><br />
<b><span style="font-size: large;"><br /></span></b>
<br />
<ul style="text-align: left;">
<li>When you are going to embed strings in HTML for example when you "print" or when you "echo" you should default to escape the string using <b><span style="color: #444444;">htmlspecialchars();</span></b> .</li>
<li>Never trust user's input, always sanitize the user input by first sanitizing it properly and before performing any SQL query, this can be achieved by <span style="color: #444444; font-weight: bold;">mysql_real_escape_string(</span><span style="color: #783f04; font-weight: bold;">$parameter</span><span style="color: #444444; font-weight: bold;">).</span>Where $parameter is the input you are about to filter.</li>
<li><a href="http://www.php.net/manual/en/security.magicquotes.disabling.php" rel="nofollow" target="_blank">Disable Magic Quotes</a>.</li>
<li>Use prepared statements and parameterized queries, always filter the user inputs properly,thoroughly and strictly.</li>
<li>Don't through <b><span style="color: #444444;">mysql_error()</span></b> to the user,don't even let them know something happened out there,most of the people use it like, <span style="color: #444444;">die(mysql_error()); .</span> Instead of just throwing this error straight away to the user you can write your custom messages, prepare some log files or even create a <span style="color: #444444;">mail()</span> function so you get notified whenever someone tries to pull something off the routine.</li>
<li>Log,Log,Log, always log the errors and warning messages, turn off error reporting on live websites and enable only when debugging or during development process or testing phases.</li>
<li>Be Self-Aware, never give up, always try up-to-date security precautions, create difficult random passwords for database, encrypt all the user's information in the database and set a value of 666 to the files which contains configuration for mySQL access.</li>
<li>Keep your server patched and updated, use only latest software's, if you are on a shared hosting server, and find something which is not fine, notify the Administrator.</li>
<li>If you are using some software's like WordPress,Joomla,Drupal or any other free and open-source or even other software's,keep them updates, most of these products have built-in update notifiers in the Admin panel's and most of the time they update themselves with a single click.</li>
</ul>
<br />
<span style="font-size: large;"><b>Best way to Avoid MySQL Injections.</b></span><br />
<span style="font-size: large;"><b><br /></b></span>
<br />
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
You basically have two options to achieve this:</div>
<ol style="background-color: white; border: 0px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1em 30px; padding: 0px; vertical-align: baseline;">
<li style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;"><div style="background-color: transparent; border: 0px; clear: both; font-size: 13.63636302947998px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
Using <a href="http://php.net/manual/en/book.pdo.php" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: #4a6b82; cursor: pointer; font-size: 13.63636302947998px; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">PDO</a>:</div>
<pre class="default prettyprint" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">$stmt </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $pdo</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">prepare</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">'SELECT * FROM employees WHERE name = :name'</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
$stmt</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">execute</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">array</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">':name'</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">=></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $name</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">));</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: darkblue; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">foreach</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">$stmt </span><span class="kwd" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: darkblue; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">as</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $row</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: grey; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">// do something with $row</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span></code></pre>
</li>
<li style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;"><div style="background-color: transparent; border: 0px; clear: both; font-size: 13.63636302947998px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
Using <a href="http://php.net/manual/en/book.mysqli.php" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: #4a6b82; cursor: pointer; font-size: 13.63636302947998px; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">mysqli</a>:</div>
<pre class="default prettyprint" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">$stmt </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $dbConnection</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">prepare</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">'SELECT * FROM employees WHERE name = ?'</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
$stmt</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">bind_param</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">'s'</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $name</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
$stmt</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">execute</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
$result </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $stmt</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">get_result</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: darkblue; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">while</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">$row </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $result</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">fetch_assoc</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">())</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: grey; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">// do something with $row</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span></code></pre>
</li>
</ol>
<h2 style="background-color: white; border: 0px; font-family: 'Trebuchet MS', 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 19.09090805053711px; line-height: 1.3; margin: 0px 0px 1em; padding: 0px; vertical-align: baseline;">
PDO</h2>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
Note that when using <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">PDO</code> to access a MySQL database <em style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">real</em> prepared statements are <strong style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">not used by default</strong>. To fix this you have to disable the emulation of prepared statements. An example of creating a connection using PDO is:</div>
<pre class="default prettyprint" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">$dbConnection </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: darkblue; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> PDO</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">'mysql:dbname=dbtest;host=127.0.0.1;charset=utf8'</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">'user'</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">'pass'</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
$dbConnection</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">setAttribute</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">PDO</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">::</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">ATTR_EMULATE_PREPARES</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: darkblue; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">false</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
$dbConnection</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">setAttribute</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">PDO</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">::</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">ATTR_ERRMODE</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> PDO</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">::</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">ERRMODE_EXCEPTION</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span></code></pre>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
In the above example the error mode isn't strictly necessary, <strong style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">but it is advised to add it</strong>. This way the script will not stop with a <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">Fatal Error</code> when something goes wrong. And gives the developer the chance to <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">catch</code> any error(s) (which are <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">throw</code>ed as <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">PDOException</code>s.</div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
What is <strong style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">mandatory</strong> however is the <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">setAttribute()</code> line, which tells PDO to disable emulated prepared statements and use <em style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">real</em> prepared statements. This makes sure the statement and the values aren't parsed by PHP before sending it the the MySQL server (giving a possible attacker no chance to inject malicious SQL).</div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
Although you can set the <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">charset</code> in the options of the constructor it's important to note that 'older' versions of PHP (< 5.3.6) <a href="http://php.net/manual/en/ref.pdo-mysql.connection.php" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: #4a6b82; cursor: pointer; font-size: 13.63636302947998px; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">silently ignored the charset parameter</a> in the DSN.</div>
<h2 style="background-color: white; border: 0px; font-family: 'Trebuchet MS', 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 19.09090805053711px; line-height: 1.3; margin: 0px 0px 1em; padding: 0px; vertical-align: baseline;">
Explanation</h2>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
What happens is that the SQL statement you pass to <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">prepare</code> is parsed and compiled by the database server. By specifying parameters (either a <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">?</code> or a named parameter like <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">:name</code> in the example above) you tell the database engine where you want to filter on. Then when you call <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">execute</code> the prepared statement is combined with the parameter values you specify.</div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
The important thing here is that the parameter values are combined with the compiled statement, not a SQL string. SQL injection works by tricking the script into including malicious strings when it creates SQL to send to the database. So by sending the actual SQL separately from the parameters you limit the risk of ending up with something you didn't intend. Any parameters you send when using a prepared statement will just be treated as strings (although the database engine may do some optimization so parameters may end up as numbers too, of course). In the example above, if the <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">$name</code> variable contains <code style="background-color: #eeeeee; background-position: initial initial; background-repeat: initial initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 1px 5px; vertical-align: baseline;">'Sarah'; DELETE * FROM employees</code> the result would simply be a search for the string "'Sarah'; DELETE * FROM employees", and you will not end up with <a href="http://xkcd.com/327/" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: #4a6b82; cursor: pointer; font-size: 13.63636302947998px; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">an empty table</a>.</div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
Another benefit with using prepared statements is that if you execute the same statement many times in the same session it will only be parsed and compiled once, giving you some speed gains.</div>
<div style="background-color: white; border: 0px; clear: both; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 1em; padding: 0px; vertical-align: baseline; word-wrap: break-word;">
Oh, and since you asked about how to do it for an insert, here's an example (using PDO):</div>
<pre class="default prettyprint" style="background-color: #eeeeee; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; line-height: 17.27272605895996px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">$preparedStatement </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $db</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">prepare</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">'INSERT INTO table (column) VALUES (:column)'</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">
$preparedStatement</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">-></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">execute</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">array</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: maroon; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">':column'</span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">=></span><span class="pln" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;"> $unsafeValue</span><span class="pun" style="background-color: transparent; border: 0px; font-size: 13.63636302947998px; margin: 0px; padding: 0px; vertical-align: baseline;">));</span></code></pre>
<br />
<br />
Any comments are welcome :) Share your practices with me as well.<br />
<br />
<div style="text-align: center;">
♥ Happy Coding ♥</div>
</div>
Unknownnoreply@blogger.com0