Saturday, October 21, 2017

Running Ubuntu on Windows 10 Subsystem for Linux


To install Ubuntu directly to Windows 10, it becomes a desktop app, you need to complete following steps:
  1. From Windows Setting, select "Update and Security"
  2. Select "For Developers" in Update and Security
  3. Then select Developer mode, and pick yes to install the package
  4. Reboot the system
  5. Type "Windows Features" at Task Bar, then select "Windows Subsystem for Linux (Beta)". Windows will update necessary files
  6. Reboot the system
  7. Type "bash" at Task Bar to bring up a terminal, then type y at prompt to install Ubuntu
  8. Create new user and password at prompt
  9. Type bash in Task Bar to start up Ubuntu

Saturday, October 7, 2017

Use Systemd for Running Meteor on Ubuntu 16.04

For myapp, create a service file myapp.service at /etc/systemd/system:

[Unit]
Description=Myapp
After=network.target

[Service]
WorkingDirectory=/home/pwong/myapp
ExecStart=/usr/local/bin/meteor --production
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=Myapp
User=pwong
Group=pwong
Environment=NODE_ENV=production
Environment=PWD=/home/pwong/myapp
Environment=PORT=3000
Environment=HTTP_FORWARDED_COUNT=1
Environment=ROOT_URL=http://myapp.com
Environment=MONGO_URL=mongodb://user:pwd@mongodb0,mongodb1,mongodb2:27017/myapp?replcaSet=rs0
Environment=MONGO_OPLOG_URL=mongodb://user:pwd@mongodb0,mongodb1,mongodb2:27017/local?replcaSet=rs0&authSource=admin

[Install]
WantedBy=multi-user.target

-----------------------------------------------------------------
Note:

1. The above configuration is used for replcation set rs0 and remote mongodb server
2. Without the Install section, the service won't startup during the boot time. This section force the system to create a symbolic link for startup.
3. The local database won't accept user creation, so the user is created at admin. Therefore authSource has to be defined in this case.

Perform the following to enable the service and check the status of the service:

$ sudo systemctl daemon-reload
$ sudo systemctl enable myapp
$ sudo systemctl start myapp
$ sudo systemctl status myapp

Saturday, December 10, 2016

remove old mysql binary logs

The binary logs can use lots of storage space. Following are 2 ways to clean them up, by name and by time. 

To remove all binary logs up to mysql-bin.003456:

mysql> PURGE BINARY LOGS TO 'mysql-bin.003456';

To remove all binary logs by date/time (Before December 8th, 2016 @6:00):

mysql> PURGE BINARY LOGS BEFORE '2016-12-08 00:06:00';

Search for MySQL binary logs, error logs, temporary files:

# lsof -nc mysqld | grep -vE '(.so(..*)?$|.frm|.MY?|.ibd|ib_logfile|ibdata|TCP)'

Tuesday, December 6, 2016

Enable https for Nginx

Modify listen line in config file for example /etc/nginx/sites-enabled/default.

listen 80 default_server;

and add listen 443 and certificate/key path. Following example is for Let's Encrypt installation.

listen 80;
listen 443 default ssl;
ssl_certificate /etc/letsencrypt/live/domainname/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/domanname/privkey.pem;

To force http to https, add following in the server block say just after above entries. Myserver.com is used as server_name in following example.

server_name myserver.com;
if ($scheme = http) {
      return 301 https://$server_name$request_uri;
}

Check syntax error in config file:

nginx -t

Reload or restart Nginx server.

/etc/init.d/nginx reload|restart

Saturday, December 3, 2016

Reset MySQL root password


Following is a way to reset MySQL root password when you forgot.
  1. Stop MySQL: /etc/init.d/mysqld stop
  2. Startup MySQL without password prompt: mysqld_safe --skip-grant-tables &
  3. Login to MySQL: mysql --user=root mysql
  4. Reset password: mysql> update user set Password=PASSWORD('new-password') where user='root';
  5. mysql> flush privileges;
  6. mysql> exit;
  7. Kill running mysql daemon: kill mysql-pid
  8. Start MySQL: /etc/init.d/mysqld start

Saturday, November 26, 2016

Troubleshooting logrotate and cron on Ubuntu server

My client's logrotate was not rotate the logs for months. However, manually run logrotate is fine. use debug, verbose, even force options can't find anything funny. The content of /var/lib/logrotate/status looks normal. Finally, check the cron job to see if anything wrong there.
  1. Tail /var/log/cron.log and see "Authentication token is no longer valid; new one required".
  2. Run "chage -l root" to check the aging information of root account, and "password must be changed" message shown up.
  3. Generate a new password from https://strongpasswordgenerator.com, then run passwd command to change to the new passwd for root. 
  4. Check /var/log/cron.log again after a cron job is executed, and the cron.log seems OK. The problem fixed.
Conclusion: Since the cron job is essential for automatic log rotation. If cron job has any problem, the log files don't get rotated even manual run has no problem at all.