• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

Archives for March 2017

NextCloud Install

March 23, 2017

NextCloud is an open-source next-generation file sharing and file synching web-application that you can run and install on your own Linux server. It’s similar to Dropbox, the file-sharing cloud application. The big difference between Dropbox and NextCloud is that you get to run your own cloud storage on your own server. The following are instructions on how to install NextCloud on an Ubuntu Server 16.04 LTS server.

Run Updates First

$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get dist-upgrade -y

$ sudo apt-get update $ sudo apt-get upgrade -y $ sudo apt-get dist-upgrade -y

Install Apache

$ sudo apt-get install apache2

$ sudo apt-get install apache2

Install MySQL Server

$ sudo apt-get install mysql-server mysql-client
$ sudo mysql_secure_installation

$ sudo apt-get install mysql-server mysql-client $ sudo mysql_secure_installation

Install PHP

$ sudo apt-get install php libapache2-mod-php php-mbstring php-zip php-xml php-mysql php-gd
$ sudo apt-get install php-json php-curl php-intl php-mcrypt php-imagick php-dom unzip

$ sudo apt-get install php libapache2-mod-php php-mbstring php-zip php-xml php-mysql php-gd $ sudo apt-get install php-json php-curl php-intl php-mcrypt php-imagick php-dom unzip

Edit php.ini change the memory limit and upload and post file sizes to something bigger. Restart Apache.

$ sudo nano /etc/php/7.0/apache2/php.ini
memory_limit = 512M
upload_max_filesize = 500M
post_max_size = 500M
$ sudo service apache2 restart

$ sudo nano /etc/php/7.0/apache2/php.ini memory_limit = 512M upload_max_filesize = 500M post_max_size = 500M $ sudo service apache2 restart

Create Database and User. Flush privileges.

$ mysql -u root -p
mysql> create database nextcloud;
mysql> grant all privileges on nextcloud.* to 'ncuser'@'localhost' identified by 'password';
mysql> flush privileges;
mysql> exit;

$ mysql -u root -p mysql> create database nextcloud; mysql> grant all privileges on nextcloud.* to 'ncuser'@'localhost' identified by 'password'; mysql> flush privileges; mysql> exit;

Download and Install the latest Nextcloud. Give permissions to Apache.

$ cd /var/www/html
$ sudo wget https://download.nextcloud.com/server/releases/nextcloud-11.0.2.zip
$ sudo unzip nextcloud-11.0.2.zip
$ sudo chown -R www-data:www-data nextcloud

$ cd /var/www/html $ sudo wget https://download.nextcloud.com/server/releases/nextcloud-11.0.2.zip $ sudo unzip nextcloud-11.0.2.zip $ sudo chown -R www-data:www-data nextcloud

Setup Apache

$ sudo nano /etc/apache2/sites-available/nextcloud.conf
 
Alias / "/var/www/html/nextcloud/"
 
 Options +FollowSymlinks
 AllowOverride All
 
  Dav Off
 
 SetEnv HOME /var/www/html/nextcloud
 SetEnv HTTP_HOME /var/www/html/nextcloud

$ sudo nano /etc/apache2/sites-available/nextcloud.conf Alias / "/var/www/html/nextcloud/" Options +FollowSymlinks AllowOverride All Dav Off SetEnv HOME /var/www/html/nextcloud SetEnv HTTP_HOME /var/www/html/nextcloud

Enable Nextcloud. Disable default.

$ sudo a2ensite nextcloud.conf
$ sudo a2dissite 000-default.conf

$ sudo a2ensite nextcloud.conf $ sudo a2dissite 000-default.conf

Enable the following

$ sudo a2enmod rewrite
$ sudo a2enmod headers
$ sudo a2enmod env
$ sudo a2enmod dir
$ sudo a2enmod mime
$ sudo service apache2 restart

$ sudo a2enmod rewrite $ sudo a2enmod headers $ sudo a2enmod env $ sudo a2enmod dir $ sudo a2enmod mime $ sudo service apache2 restart

Open your browser and access your server via IP address or domain name to complete the NextCloud install.

You will be asked to enter a new username and password. Also enter your database credentials to complete the installation.

Filed Under: Cloud, Linux Tagged With: cloud storage, file sharing, file sync, nextcloud, ubuntu 16.04 lts

Quick Database Check

March 17, 2017

Here’s a quick PHP script to check if your web server can connect to your MySQL database. It’s a neat little script if you don’t have any application installed yet, and you just want to see if your web server setup can quickly connect to the database. All you have to do is create a new file called connect.php, and place the following code inside. Just change the username, password and database. Most likely the hostname will be localhost.

$ sudo nano /var/www/html/connect.php
$username = "user";
$password = "password";
$hostname = "hostname";
$database = "database";
 
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Successfully Connected to the MySQL Database.<br/>";
 
$selected = mysql_select_db("$dbname", $dbhandle) or die("Could not select database");
echo "Successfully Connected to Database called: $database";

$ sudo nano /var/www/html/connect.php $username = "user"; $password = "password"; $hostname = "hostname"; $database = "database"; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Successfully Connected to the MySQL Database.<br/>"; $selected = mysql_select_db("$dbname", $dbhandle) or die("Could not select database"); echo "Successfully Connected to Database called: $database";

It’s also available on my Github gist.

Filed Under: PHP Tagged With: connect, mysql

Laravel 5.4 Install

March 16, 2017

This is the installation of Laravel 5.4 on an Ubuntu Server 16.04 LTS. Laravel requires that you run Apache, MySQL and PHP. In this installation, I’m using the following versions: Apache 2.4.18, MySQL 5.7.17 and PHP 7.0.15. The installation of Laravel has changed over the years. I wished the installation was simpler. You can try Forge, but it’s not free. It also requires that you have an account at Linode, Digital Ocean or AWS. Forge costs $15 per month and Forge Plus is $30 per month. If you don’t want to pay, I’m afraid you are going to have to install Laravel yourself. Laravel installation may vary based on the distro you’re using. These instructions are for the Ubuntu Server 16.04 LTS.

Run Update

sudo apt-get update
sudo apt-get upgrade

sudo apt-get update sudo apt-get upgrade

Install Apache

sudo apt-get install apache2
sudo a2enmod rewrite
sudo service apache2 restart

sudo apt-get install apache2 sudo a2enmod rewrite sudo service apache2 restart

Install MySQL

sudo apt-get install mysql-server
sudo mysql_secure_installation

sudo apt-get install mysql-server sudo mysql_secure_installation

Install PHP

sudo apt-get install php libapache2-mod-php php-mbstring php-zip php-xml php-mysql

sudo apt-get install php libapache2-mod-php php-mbstring php-zip php-xml php-mysql

Install Composer

sudo curl -sS https://getcomposer.org/installer | sudo php — —install-dir=/usr/local/bin —filename=composer
# you may have to move composer manually if it doesn't work
sudo mv composer.phar /usr/local/bin/composer
# check if composer is accessible globally
composer -V

sudo curl -sS https://getcomposer.org/installer | sudo php — —install-dir=/usr/local/bin —filename=composer # you may have to move composer manually if it doesn't work sudo mv composer.phar /usr/local/bin/composer # check if composer is accessible globally composer -V

Install Laravel Installer

composer global require "laravel/installer"
# Using alias instead of $PATH
echo 'alias laravel="~/.composer/vendor/bin/laravel"' >> ~/.bashrc
source ~/.bashrc
# add source ~/.bashrc to .profile to load automatically
echo 'source ~/.bashrc' >> ~/.profile
# restart apache
sudo service apache2 restart
# check if laravel is accessible globally
laravel

composer global require "laravel/installer" # Using alias instead of $PATH echo 'alias laravel="~/.composer/vendor/bin/laravel"' >> ~/.bashrc source ~/.bashrc # add source ~/.bashrc to .profile to load automatically echo 'source ~/.bashrc' >> ~/.profile # restart apache sudo service apache2 restart # check if laravel is accessible globally laravel

Install New Laravel Project

# set permissions
cd /var/www/
sudo chown -R ubuntu:ubuntu /var/www/html
sudo chmod -R 755 /var/www/html
# install new project
cd /var/www/html
laravel new project
# give apache permissions 
sudo chgrp -R www-data /var/www/html/project/storage
sudo chgrp -R www-data /var/www/html/project/bootstrap/cache

# set permissions cd /var/www/ sudo chown -R ubuntu:ubuntu /var/www/html sudo chmod -R 755 /var/www/html # install new project cd /var/www/html laravel new project # give apache permissions sudo chgrp -R www-data /var/www/html/project/storage sudo chgrp -R www-data /var/www/html/project/bootstrap/cache

Configure Apache

cd /etc/apache2/sites-available
sudo cp 000-default.conf project.conf
sudo nano project.conf
#
<VirtualHost *:80>
  ServerName local.project.com
  ServerAdmin admin@project.com
  DocumentRoot /var/www/html/project/public
  <Directory "/var/www/html/project">
    AllowOverride All
  </Directory>
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
#
sudo a2ensite project.conf
sudo a2dissite 000-default.conf
sudo service apache2 restart

cd /etc/apache2/sites-available sudo cp 000-default.conf project.conf sudo nano project.conf # <VirtualHost *:80> ServerName local.project.com ServerAdmin admin@project.com DocumentRoot /var/www/html/project/public <Directory "/var/www/html/project"> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> # sudo a2ensite project.conf sudo a2dissite 000-default.conf sudo service apache2 restart

Finally, you can now access Laravel from the browser from the server IP address or domain.

Filed Under: Linux, PHP Tagged With: 5.4, install, laravel

Working With AWS LightSail

March 14, 2017

Just a quick demo how to work with AWS LightSail. LightSail is AWS lightweight VPS at a fixed price point. You can install just the OS. You can choose either Amazon Linux or Ubuntu OS. If you prefer, you can install any of the several ready-made applications provided by AWS in conjunction with Bitnami. In this example, I installed Ubuntu and Apache just demonstrate how to easy it is to work with LightSail.

Filed Under: Cloud, Linux Tagged With: aws, lightsail

Where Featured Images Are Stored

March 10, 2017

The WordPress Genesis theme uses featured images on its posts on several of its themes. If you’re curious as to where the Genesis theme stores the image links on the database, you’ll need to go to a couple of WordPress tables to find them. First and foremost, you’ll need to get the ID of your WordPress post. You can easily find this by hovering over the edit link and checking the ID number.

Let’s pretend the post ID is 110. You will then need to find the meta_value in the wp_postmeta table.

Table: wp_postmeta

meta_id     post_id   meta_key         meta_value
544         110       _thumbnail_id    45

meta_id post_id meta_key meta_value 544 110 _thumbnail_id 45

SELECT meta_value FROM wp_postmeta WHERE meta_key='_thumbnail_id' and post_id=110;

SELECT meta_value FROM wp_postmeta WHERE meta_key='_thumbnail_id' and post_id=110;

The result is 45. Now that you have the meta_value, you can then search for the link from the wp_posts table.

Table: wp_posts

ID      post_type       guid
45      attachment      http://yourdomain.com/wp-content/uploads/2017/03/featured-image.jpg

ID post_type guid 45 attachment http://yourdomain.com/wp-content/uploads/2017/03/featured-image.jpg

SELECT guid FROM wp_posts WHERE post_type='attachment' and id=45;

SELECT guid FROM wp_posts WHERE post_type='attachment' and id=45;

The result will be the URL of the image, e.g. http://yourdomain.com/wp-content/uploads/2017/03/featured-image.jpg.

It’s an odd place to store the image link, but that’s where it is.

Filed Under: WP Tagged With: featured images, genesis, mysql

Mass Find and Replace

March 9, 2017

There are situations when you have to do a mass find and replace on your database. Let’s say you’re really, really bad speller. You just recently found out that you’ve been spelling a word for wrongly for years. To avoid embarrassment, you decide to do a search and replace. Unfortunately, you found out that there are hundreds of posts that contains that misspelled word. Instead of editing each post which is time consuming, you decide to run a mass find and replace against the database. In this example, we will replace the word ‘dawg’ with ‘dog.’

UPDATE wp_posts SET post_content = REPLACE ( post_content, 'dawg', 'dog');

UPDATE wp_posts SET post_content = REPLACE ( post_content, 'dawg', 'dog');

I highly recommend that you back up your database first. I also recommend that you run it on a test database first before running it on a production environment, because a mass update is quite dangerous if you’re not sure what you are doing. If you’re a WordPress user, there’s a plugin called Search and Replace that will do the same thing. That might be easier for most people who don’t have access to MySQL command line or PHPMyAdmin.

Filed Under: PHP, WP Tagged With: mysql, replace

WordPress Mobile Detect

March 9, 2017

I found this little nugget in the WordPress Codex called wp_is_mobile(). It will detect the user browser’s agent and will return a true or a false if it’s a mobile device. This is helpful if you would like to customized code specifically for a mobile device. Let’s say you have a media player and you want it to have a specific width for desktops and another width for mobile devices. So, here’s the code to detect if it’s a mobile device.

if ( wp_is_mobile() ) :
  $player_width = '270';
else :
  $player_width = '400';
endif;

if ( wp_is_mobile() ) : $player_width = '270'; else : $player_width = '400'; endif;

You can then use the variable $player_width in your media player configuration to set the width of the player. However, if you read the Codex, it says that tablets are considered as mobile devices, which is perfectly fine for my purposes here. It’s ok to use this tiny little function in your WordPress templates, but it’s not recommended for media queries.

Filed Under: PHP, WP Tagged With: mobile

Not Marked For Formatting

March 1, 2017

I have a Linux box with a 1TB drive. I divided it into 4 different partitions at 250GB each. It’s currently running Linux Mint, Gitlab, Nextcloud and the Unifi Cloud Controller on each partition. Obviously, I can’t run all 4 partitions at the same time. I have to choose one from a selection in the Grub, a multi-boot loader. It would have been easier to run Virtualbox on just one Linux install, but it’s older CPU. I don’t think it could handle multiple OS running at the same time.

Unfortunately, one of the partitions got corrupted. I tried running the utility called badblocks to figure out if there are any bad sectors on the drive that can be marked bad, but the test result turned out to be surprisingly good. There were absolutely no errors and no bad blocks. Next, I tried using the recovery process, but that didn’t work either. It seems the only way to fix the problem was to reinstall Linux. A bummer.

During the install, I ran into the problem with the partition not being properly marked for formatting. It seems the only way to get around that issue was to delete the partition and to repartition it. Nevertheless, it would have been an interesting dilemma if I had any important data on that partition. It’s a good thing that I didn’t. So, if you ever get into a problem where it says “your partition is not marked for formatting”, you may have to go through the process of deleting the partition and then recreating one, in order to proceed with a fresh install.

Filed Under: Linux Tagged With: formatting, partition

  • Home
  • About
  • Archives

Copyright © 2023