VOOZH about

URL: https://rubychallenger.blogspot.com/search/label/solved

⇱ The Ruby Challenger: solved


Showing posts with label solved. Show all posts
Showing posts with label solved. Show all posts

Sunday, May 31, 2020

Domain serving SSL certificate of a subdomain

Today one of my sites was serving a SSL certificate of a subdomain instead of the domain's one, after configuring the SSL certificate for the subdomain with certbot for Apache.

It took me some hours of investigating and tries.

The problem is that certbot changed the domain.conf file in /etc/apache2/sites-available folder to serve the subdomain's SSL certificate. Editing the file and reloading Apache solved it.

Wednesday, January 8, 2020

Time adjustment for DigitalOcean droplets

Hello!

I have some droplets on DigitalOcean, but I noted that the system time (in all of them) is not correctly synced with NTP servers.

The solution that worked for me is to include the following line at the end of /etc/crontab:

* * * * * root /sbin/hwclock -s

That command instructs the system to copy time from hardware (which is more precise) every minute. That solved my problem.

Friday, December 27, 2019

Troubleshooting Ruby 2.7 compiler errors

So Ruby 2.7 was released at Xmas'19 and I got some errors when compiling it in Linux Mint 19.1. Some libraries were not configured. The errors ocurred at the linking phase of make. Five libraries got errors, and I'll walk across their solutions:

  • openssl:
    sudo apt install libssl-dev
  • gdbm:
    sudo apt install libgdbm-dev
  • dbm:
    sudo apt install libqdbm-dev
  • readline:
    sudo apt install libedit-dev
  • zlib:
    sudo apt install libz-dev
    cd /lib/x86_64-linux-gnu/
    sudo ln -s libz.so.1 liblibz.so

Enjoy!

Saturday, October 27, 2018

Net::HTTP error sending GET request with parameters

If you're getting trouble with Net::HTTP#send_request when sending a GET request with parameters, one possible cause is that parameters must be part of path as a query string, and not as data (3rd parameter of #send_request).

For example (assuming that parameters is not empty), instead of

http = Net::HTTP.new('server.com')
http.send_request('GET', path, parameters, headers)

try:

http = Net::HTTP.new('server.com')
http.send_request('GET', path + '?' + parameters, '', headers)

Net::HTTP error 400 (bad request) with HTTPS

Hello, friends!

Yesterday I got stuck when trying to connect to an HTTPS site using Net::HTTP:

      http = Net::HTTP.new('server.com', 443)
      response = http.send_request('GET', path, parameters, headers)

I was getting error 400 in response.code.

What is not well documented is that when accessing a server via HTTPS protocol it's not enough to set port to 443. You must also set use_ssl to true:

http = Net::HTTP.new('server.com', 443)
http.use_ssl = true
      response = http.send_request('GET', path, parameters, headers)


Wednesday, August 31, 2016

How to install mysql2 gem on Ubuntu

If you are having trouble to install the mysql2 gem, try the following:

# apt-get install build-essential ruby-dev libmysqlclient-dev

Monday, April 21, 2014

Installing Ruby 2.1.1 in Ubuntu 14.04

The apt-get way don't install the latest stable Ruby (2.1.1), even on Ubuntu 14.04. Instead, I got 1.9.3 that way. RVM can be easy, but to work with Apache I'd need a system-wide installation independent of the RVM environment. So I've decided to install Ruby from source. I had problems around readline, but after seeing this hint, the solution that worked for me is:

./configure --with-readline-dir=/usr/lib/x86_64-linux-gnu/libreadline.so
make
make install

on the directory of the unpacked source of Ruby 2.1.1, as root. I suspect there is a more elegant solution out there, but this is enough for me at this time.

Friday, March 7, 2014

LOAD DATA INFILE tips

If you're having troubles with LOAD DATA INFILE ("Errcode 13"):
  • copy the CSV file to the DB folder (/var/lib/mysql/DATABASE), and
  • use the filename only, not the full path.


If you're having troubles with LOAD DATA LOCAL INFILE ("The used command is not allowed with this MySQL version"):
  • temporary solution: mysql -uUSER -p --local-infile DATABASE
  • permanent solution: sudo nano /etc/mysql/my.cnf and insert local-infile under [mysql]; save the file, close the editor and type sudo service mysql restart.

Note: LOAD DATA LOCAL INFILE uses IGNORE. If you want to know why some records were skipped, you must use LOAD DATA INFILE (without LOCAL). (Reference)

Tuesday, September 24, 2013

How to change the default mode for php files in Kate Editor

Whenever I open a PHP file within the excellent Kate Editor (which I prefer due to its multi-document interface), the default mode for editing the file is PHP (HTML) instead of PHP/PHP. I use much more the indentation of the last one. I always can change the mode thru Tools -> Mode -> Scripts -> PHP/PHP, but it's a pain to do it every time I open a PHP file.

But you can set the default mode to PHP/PHP by changing its priority compared to PHP (HTML)'s.

That's how I did it:

  • Go to Settings -> Configure Kate -> Open/Save -> Modes & Filetypes;
  • Select Scripts/PHP (HTML) Filetype;
  • Change Priority to 4;
  • Select the File Extensions field and copy it;
  • Select Scripts/PHP/PHP Filetype;
  • Go to the File Extensions and paste the content copied;
  • Hit Apply and Ok;
  • Close and reopen Kate.


I was hard to find that solution (see reference); I hope this can be useful.

Wednesday, February 27, 2013

SQLite3 in Ruby 1.9: be sure your Ruby data is UTF-8

SQLite3 is not finding your records? SQLite3 is inserting binary data instead of plain-text strings? If you're using Ruby 1.9 or later, you must be sure your Ruby data is encoded as UTF-8 before submit it to SQLite3.

Text data in SQLite is UTF-8. Before Ruby 1.9, Strings had no encoding; so that was not a issue; all data get same handling. From Ruby 1.9, Strings must be encoded as UTF-8 to be compatible with SQLite data; otherwise your text will be considered binary content to SQLite, and binary content won't match plain-text content, even when they are the very same bytes.

Maybe the source of your text data encodes them as other than UTF-8. To make sure your data is UTF-8, try using String#encode or String#force_encoding before submit it to SQLite 3. Cheers!

Thursday, January 31, 2013

Sequel blocking SQLite3?

I was often getting SQLite::BusyException (reraised as Sequel::DatabaseError) or Sequel::PoolTimeout exceptions when using Sequel to handle SQLite3 databases in a program with little concurrency, in a way that that exceptions are not expected (specially on reads). I rewrote my code to use the SQLite3 gem instead of Sequel, just for test, and I had no more exceptions.

There are things to be investigated, but this tip can be useful to someone as a quick (temporary?) solution.

Monday, July 23, 2012

Problems with permission in Apache

If you're getting Error 403 Forbidden in your site, and the owner and the filesystem's permissions of your files are correctly set, maybe you must check your Apache configuration files to see whether your DirectoryIndex directive includes the file that you want as index, and whether the Directory directive of your project's path is allowing the requests.

Saturday, July 14, 2012

RVM + Apache + CGI Scripts

Hello!

I've configured a new server on Ubuntu 12.04 and I started to use RVM, an excellent version manager which permits to have multiple versions of Ruby installed on a single server (and many versions of the gems - see gemsets), and it makes easy to switch among them.

I've installed RVM under my user (as myself, not as root with sudo) by following the Ryan Bigg's guide, with no previous system-wide installed Ruby. So, I didn't have any Ruby under /usr/bin. My first task then was to replace the shebang line of all my CGI scripts, from

!#/usr/bin/ruby

to

!#/usr/bin/env ruby
# encoding: utf-8

(The second line is needed to define the encoding of the string literals in my code, for Ruby 1.9.)

However my scripts didn't run under Apache. In the terminal I could run them (by typing ./index.cgi, for example), but not over a browser. A relevant note: in both the user is the same, i.e., the Apache user is the same as the one logged on terminal. Through php tests, I've checked the RVM enviroment was not loaded under Apache. (If anyone can solve that, please let me know.)

I saw this tip for running CGI scripts with RVM, which suggests to put the complete path of specific version of Ruby in the shebang line. That can be useful if you have scripts which run on different versions of Ruby. But that solution doesn't work for me, because my scripts must run on different machines, with different users, different ruby versions and different paths.

The solution which works for me is to put a symlink of the desired Ruby version under /usr/bin:

sudo ln -s /home/sony/.rvm/rubies/ruby-1.8.7-p370/bin/ruby /usr/bin/ruby

(Notes: sony is my username and I chose 1.8.7 because by now my scripts aren't 1.9-compliant yet.)

Therefore I didn't need to have changed the shebang lines. :) But I guess that will be useful in the future.

Tuesday, May 22, 2012

How to UPDATE a table using data from another row

The Problem: I have a table with name, num, and diff. The unusual case here is that the diff must be updated with the difference between two sibling nums for the same name. So I'll need to know the num of the next row (for the same name) to update the current one. AND I want to do that in SQL with a single UPDATE.

Here I'll assume the rows must be sorted by num, but it would be sorted by another field, like id or timestamp.

The solution in MySQL is to use a inline temporary table to get the num of next row and associate it with current id, and use that table in the UPDATE statement.

Here is the code. Enjoy!
 
-- the table used for test (MySQL syntax)
CREATE TABLE `test` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(30) NOT NULL,
 `num` int(11) NOT NULL,
 `diff` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

-- some values to play on
INSERT INTO `test` (`id`, `name`, `num`, `diff`) VALUES
(1, 'a', 10, NULL),
(2, 'b', 8, NULL),
(3, 'a', 18, NULL),
(4, 'a', 21, NULL),
(5, 'b', 14, NULL),
(6, 'a', 32, NULL),
(7, 'b', 20, NULL),
(8, 'b', 21, NULL);

-- a select to test the ability to retrieve the desired diff value
select id, name, num, (select min(num) from test where name = t1.name and num > t1.num)-num
from test t1
where diff is null
order by name, num;

-- updating test with the calculated diff using another row on same table.
update test t2, (
 select id, (select min(num) from test where name = t1.name and num > t1.num)-num as diff
 from test t1
) t3
set t2.diff = t3.diff
where t2.id = t3.id
and t2.diff is null
 
-- an alternative query to get more than one column from t2
select t1.id, t1.name, t1.num, t2.name, t2.num
from test t1, test t2
where t2.id = (select id from test where name = t1.aome and num > t1.num order by num limit 1)
order by t1.name, t1.num 

See this post in portuguese.

Tuesday, March 6, 2012

How to install sqlite3-ruby gem on linux

If you are having trouble to install the sqlite3-ruby gem, try the following:

In systems with apt-get:

# apt-get install libsqlite3-dev

In systems with yum:

# yum install sqlite-devel

How to install bson_ext gem on linux

MongoDB requires the bson_ext gem to increase performance. However, it's common to be not ready to install it.

In ubuntu (10.04 32bit) I had to run:

# apt-get install ruby1.8-dev

In systems with yum I had to run:

# yum install gcc
# yum install make
# yum install ruby-devel


Finally in both systems I could run, with no errors:

# gem install bson_ext
Subscribe to: Posts (Atom)