Setup Hosted Network on Ubuntu

Setup wireless card:

https://askubuntu.com/questions/55868/installing-broadcom-wireless-drivers

 

Share Wi-Fi with wired connection

http://ubuntuhandbook.org/index.php/2014/09/3-ways-create-wifi-hotspot-ubuntu/

Advertisements

How To Install and Use Composer on Ubuntu 14.04

Introduction

Composer is a popular dependency management tool for PHP, created mainly to facilitate installation and updates for project dependencies. It will check which other packages a specific project depends on and install them for you, using the appropriate versions according to the project requirements.

This tutorial will show how to install and get started with Composer on an Ubuntu 14.04 server.

Prerequisites

For this tutorial, you will need:

  • A server running Ubuntu 14.04
  • Access to the server as a regular user with sudo permission

Step 1 — Installing the Dependencies

Before we download and install Composer, we need to make sure our server has all dependencies installed.

First, update the package manager cache by running:

  • sudo apt-get update

Now, let’s install the dependencies. We’ll need curl in order to download Composer and php5-cli for installing and running it. git is used by Composer for downloading project dependencies. Everything can be installed with the following command:

  • sudo apt-get install curl php5-cli git

You can now proceed to the next step.

Step 2 — Downloading and Installing Composer

Composer installation is really simple and can be done with a single command:

This will download and install Composer as a system-wide command named composer, under /usr/local/bin. The output should look like this:

Output
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

To test your installation, run:

  • composer

And you should get output similar to this:

Output
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.0-dev (9859859f1082d94e546aa75746867df127aa0d9e) 2015-08-17 14:57:00

Usage:
 command [options] [arguments]

Options:
 --help (-h)           Display this help message
 --quiet (-q)          Do not output any message
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 --version (-V)        Display this application version
 --ansi                Force ANSI output
 --no-ansi             Disable ANSI output
 --no-interaction (-n) Do not ask any interactive question
 --profile             Display timing and memory usage information
 --working-dir (-d)    If specified, use the given directory as working directory.

. . .

This means Composer was succesfully installed on your system.

If you prefer to have separate Composer executables for each project you might host on this server, you can simply install it locally, on a per-project basis. This method is also useful when your system user doesn’t have permission to install software system-wide. In this case, installation can be done with curl -sS https://getcomposer.org/installer | php – this will generate a composer.phar file in your current directory, which can be executed with php composer.phar [command].

Step 3 — Generating the composer.json File

In order to use Composer in your project, you’ll need a composer.json file. The composer.json file basically tells Composer which dependencies it needs to download for your project, and which versions of each package are allowed to be installed. This is extremelly important to keep your project consistent and avoid installing unstable versions that could potentially cause backwards compatibility issues.

You don’t need to create this file manually – it’s easy to run into syntax errors when you do so. Composer auto-generates the composer.json file when you add a dependency to your project using the requirecommand. Additional dependencies can also be added in the same way, without the need to manually edit this file.

The process of using Composer to install a package as dependency in a project usually involves the following steps:

  • Identify what kind of library the application needs
  • Research a suitable open source library on Packagist.org, the official repository for Composer
  • Choose the package you want to depend on
  • Run composer require to include the dependency in the composer.json file and install the package

We’ll see how this works in practice with a simple demo application.

The goal of this application is to transform a given sentence into a URL-friendly string – a slug. This is commonly used to convert page titles to URL paths (like the final portion of the URL for this tutorial).

Let’s start by creating a directory for our project. We’ll call it slugify:

  • cd ~
  • mkdir slugify
  • cd slugify

Searching for Packages on Packagist

Now it’s time to search Packagist.org for a package that can help us generating slugs. If you search for the term “slug” on Packagist, you’ll get a result similar to this:

Packagist Search: easy-slug/easy-slug, muffin/slug, ddd/slug, zelenin/slug, webcastle/slug, anomaly/slug-field_type

You’ll see two numbers on the right side of each package in the list. The number on the top represents how many times the package was installed, and the number on the bottom shows how many times a package was starred on GitHub. You can reorder the search results based on these numbers (look for the two icons on the right side of the search bar). Generally speaking, packages with more installations and more stars tend to be more stable, since so many people are using them. It’s also important to check the package description for relevance – is that really what you are looking for?

What we need is a simple string-to-slug converter. From the search results, the package cocur/slugifyseems to be a good match, with a reasonable amount of installations and stars. (The package is a bit further down the page than the screenshot shows.)

You will notice that the packages on Packagist have a vendor name and a package name. Each package has a unique identifier (a namespace) in the same format Github uses for its repositories: vendor/package. The library we want to install uses the namespace cocur/slugify The namespace is what we need in order to require the package in our project.

Requiring a Package

Now that we know exactly which package we want to install, we can run composer require to include it as a dependency and also generate the composer.json file for the project:

  • composer require cocur/slugify
Output
Using version ^1.3 for cocur/slugify
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing cocur/slugify (v1.3)
    Downloading: 100%         

Writing lock file
Generating autoload files

As you can see from the output, Composer automatically decided which version of the package should be used. If you check your project’s directory now, it will contain two new files: composer.json and composer.lock, and a vendor directory:

  • ls -l
Output
total 12
-rw-rw-r-- 1 sammy sammy   59 Sep  9 16:22 composer.json
-rw-rw-r-- 1 sammy sammy 2835 Sep  9 16:22 composer.lock
drwxrwxr-x 4 sammy sammy 4096 Sep  9 16:22 vendor

The composer.lock file is used to store information about which versions of each package are installed, and make sure the same versions are used if someone else clones your project and installs its dependencies. The vendor directory is where the project dependencies are located. The vendor folder should not be committed into version control – you only need to include the composer.json and composer.lock files.

When installing a project that already contains a composer.json file, you need to run composer install in order to download the project’s dependencies.

Understanding Version Constraints

If you check the contents of your composer.json file, you’ll see something like this:

  • cat composer.json
composer.json
  • {
  • require“: {
  • cocur/slugify“: “^1.3”
  • }
  • }

You might notice the special character ^ before the version number on composer.json. Composer supports several different constraints and formats for defining the required package version, in order to provide flexibility while also keeping your project stable. The caret (^) operator used by the auto-generated composer.json file is the recommended operator for maximum interoperability, following semantic versioning. In this case, it defines 1.3 as the minimum compatible version, and allows updates to any future version below 2.0.

Generally speaking, you won’t need to tamper with version constraints in your composer.json file. However, some situations might require that you manually edit the constraints – for instance, when a major new version of your required library is released and you want to upgrade, or when the library you want to use doesn’t follow semantic versioning.

Here are some examples to give you a better understanding of how Composer version constraints work:

Constraint Meaning Example Versions Allowed
^1.0 >= 1.0 < 2.0 1.0, 1.2.3, 1.9.9
^1.1.0 >= 1.1.0 < 2.0 1.1.0, 1.5.6, 1.9.9
~1.0 >= 1.0 < 2.0.0 1.0, 1.4.1, 1.9.9
~1.0.0 >= 1.0.0 < 1.1 1.0.0, 1.0.4, 1.0.9
1.2.1 1.2.1 1.2.1
1.* >= 1.0 < 2.0 1.0.0, 1.4.5, 1.9.9
1.2.* >= 1.2 < 1.3 1.2.0, 1.2.3, 1.2.9

For a more in-depth view of Composer version constraints, check their official documentation.

Step 4 — Including the Autoload Script

Composer also provides an autoload script that you can include in your project to get autoloading for free. This makes it much easier to work with your dependencies and define your own namespaces.

The only thing you need to do is include the vendor/autoload.php file in your PHP scripts, before any class instantiation.

Let’s come back to the slugify example application. We’ll create a test.php script where we’ll use the cocur/slugify library:

  • vim test.php
test.php
  • <?php
  • require __DIR__ . ‘/vendor/autoload.php’;
  • use Cocur\Slugify\Slugify;
  • $slugify = new Slugify();
  • echo $slugify->slugify(‘Hello World, this is a long sentence and I need to make a slug from it!’);

You can run the script in the command line with:

  • php test.php

This should produce the output hello-world-this-is-a-long-sentence-and-i-need-to-make-a-slug-from-it.

Step 5 — Updating the Project Dependencies

Whenever you want to update your project dependencies, you just need to run the update command:

  • composer update

This will check for newer versions of the libraries you required in your project. If a newer version is found and it’s compatible with the version constraint defined in the composer.json file, it will replace the previous version installed. The composer.lock file will be updated to reflect these changes.

You can also update one or more specific libraries by running:

  • composer update vendor/package vendor2/package2

Conclusion

Composer is a powerful tool every PHP developer should have in their utility belt.

Beyond providing an easy and reliable way for managing project dependencies, it also establishes a new de facto standard for sharing and discovering PHP packages created by the community.

This tutorial covered the essentials for getting started with Composer on Ubuntu 14.04.

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 14.04

Introduction

A “LAMP” stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Apache web server. The site data is stored in a MySQL database, and dynamic content is processed by PHP.

In this guide, we’ll get a LAMP stack installed on an Ubuntu 14.04 Droplet. Ubuntu will fulfill our first requirement: a Linux operating system.

Note: The LAMP stack can be installed automatically on your Droplet by adding this script to its User Data when launching it. Check out this tutorial to learn more about Droplet User Data.

Prerequisites

Before you begin with this guide, you should have a separate, non-root user account set up on your server. You can learn how to do this by completing steps 1-4 in the initial server setup for Ubuntu 14.04.

Step 1: Install Apache

The Apache web server is currently the most popular web server in the world, which makes it a great default choice for hosting a website.

We can install Apache easily using Ubuntu’s package manager, apt. A package manager allows us to install most software pain-free from a repository maintained by Ubuntu. You can learn more about how to use apt here.

For our purposes, we can get started by typing these commands:

sudo apt-get update
sudo apt-get install apache2

Since we are using a sudo command, these operations get executed with root privileges. It will ask you for your regular user’s password to verify your intentions.

Afterwards, your web server is installed.

You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):

http://your_server_IP_address

You will see the default Ubuntu 14.04 Apache web page, which is there for informational and testing purposes. It should look something like this:

Ubuntu 14.04 Apache default

If you see this page, then your web server is now correctly installed.

How To Find your Server’s Public IP Address

If you do not know what your server’s public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to your server through SSH.

From the command line, you can find this a few ways. First, you can use the iproute2 tools to get your address by typing this:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

This will give you one or two lines back. They are both correct addresses, but your computer may only be able to use one of them, so feel free to try each one.

An alternative method is to use an outside party to tell you how it sees your server. You can do this by asking a specific server what your IP address is:

curl http://icanhazip.com

Regardless of the method you use to get your IP address, you can type it into your web browser’s address bar to get to your server.

Step 2: Install MySQL

Now that we have our web server up and running, it is time to install MySQL. MySQL is a database management system. Basically, it will organize and provide access to databases where our site can store information.

Again, we can use apt to acquire and install our software. This time, we’ll also install some other “helper” packages that will assist us in getting our components to communicate with each other:

sudo apt-get install mysql-server php5-mysql

Note: In this case, you do not have to run sudo apt-get update prior to the command. This is because we recently ran it in the commands above to install Apache. The package index on our computer should already be up-to-date.

During the installation, your server will ask you to select and confirm a password for the MySQL “root” user. This is an administrative account in MySQL that has increased privileges. Think of it as being similar to the root account for the server itself (the one you are configuring now is a MySQL-specific account however).

When the installation is complete, we need to run some additional commands to get our MySQL environment set up securely.

First, we need to tell MySQL to create its database directory structure where it will store its information. You can do this by typing:

sudo mysql_install_db

Afterwards, we want to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit. Start the interactive script by running:

sudo mysql_secure_installation

You will be asked to enter the password you set for the MySQL root account. Next, it will ask you if you want to change that password. If you are happy with your current password, type “n” for “no” at the prompt.

For the rest of the questions, you should simply hit the “ENTER” key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

At this point, your database system is now set up and we can move on.

Step 3: Install PHP

PHP is the component of our setup that will process code to display dynamic content. It can run scripts, connect to our MySQL databases to get information, and hand the processed content over to our web server to display.

We can once again leverage the apt system to install our components. We’re going to include some helper packages as well:

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

This should install PHP without any problems. We’ll test this in a moment.

In most cases, we’ll want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell our web server to prefer PHP files, so we’ll make Apache look for an index.php file first.

To do this, type this command to open the dir.conf file in a text editor with root privileges:

sudo nano /etc/apache2/mods-enabled/dir.conf

It will look like this:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

We want to move the PHP index file highlighted above to the first position after the DirectoryIndexspecification, like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

When you are finished, save and close the file by pressing “CTRL-X”. You’ll have to confirm the save by typing “Y” and then hit “ENTER” to confirm the file save location.

After this, we need to restart the Apache web server in order for our changes to be recognized. You can do this by typing this:

sudo service apache2 restart

Install PHP Modules

To enhance the functionality of PHP, we can optionally install some additional modules.

To see the available options for PHP modules and libraries, you can type this into your system:

apt-cache search php5-

The results are all optional components that you can install. It will give you a short description for each:

php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-gd - GD module for php5
. . .

To get more information about what each module does, you can either search the internet, or you can look at the long description in the package by typing:

apt-cache show package_name

There will be a lot of output, with one field called Description-en which will have a longer explanation of the functionality that the module provides.

For example, to find out what the php5-cli module does, we could type this:

apt-cache show php5-cli

Along with a large amount of other information, you’ll find something that looks like this:

. . .
SHA256: 91cfdbda65df65c9a4a5bd3478d6e7d3e92c53efcddf3436bbe9bbe27eca409d
Description-en: command-line interpreter for the php5 scripting language
 This package provides the /usr/bin/php5 command interpreter, useful for
 testing PHP scripts from a shell or performing general shell scripting tasks.
 .
 The following extensions are built in: bcmath bz2 calendar Core ctype date
 dba dom ereg exif fileinfo filter ftp gettext hash iconv libxml mbstring
 mhash openssl pcntl pcre Phar posix Reflection session shmop SimpleXML soap
 sockets SPL standard sysvmsg sysvsem sysvshm tokenizer wddx xml xmlreader
 xmlwriter zip zlib.
 .
 PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
 open source general-purpose scripting language that is especially suited
 for web development and can be embedded into HTML.
Description-md5: f8450d3b28653dcf1a4615f3b1d4e347
Homepage: http://www.php.net/
. . .

If, after researching, you decide you would like to install a package, you can do so by using the apt-get install command like we have been doing for our other software.

If we decided that php5-cli is something that we need, we could type:

sudo apt-get install php5-cli

If you want to install more than one module, you can do that by listing each one, separated by a space, following the apt-get install command, like this:

sudo apt-get install package1 package2 ...

At this point, your LAMP stack is installed and configured. We should still test out our PHP though.

Step 4: Test PHP Processing on your Web Server

In order to test that our system is configured properly for PHP, we can create a very basic PHP script.

We will call this script info.php. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root”.

In Ubuntu 14.04, this directory is located at /var/www/html/. We can create the file at that location by typing:

sudo nano /var/www/html/info.php

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:

<?php
phpinfo();
?>

When you are finished, save and close the file.

Now we can test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit this page in our web browser. You’ll need your server’s public IP address again.

The address you want to visit will be:

http://your_server_IP_address/info.php

The page that you come to should look something like this:

Ubuntu 14.04 default PHP info

This page basically gives you information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly.

If this was successful, then your PHP is working as expected.

You probably want to remove this file after this test because it could actually give information about your server to unauthorized users. To do this, you can type this:

sudo rm /var/www/html/info.php

You can always recreate this page if you need to access the information again later.

Conclusion

Now that you have a LAMP stack installed, you have many choices for what to do next. Basically, you’ve installed a platform that will allow you to install most kinds of websites and web software on your server.

Ubuntu – Enable AP Mode on WiFi hotspot sharing

This tutorial is trying to help those who want to create AP wifi hotspot in Ubuntu Laptop but stuck at “Wifi Hotspot: Access Point Mode Is Not Supported By This Device” error message.

I’ve written three tutorials about how to turn Ubuntu Laptop into a wifi hotspot for Android, and Windows phones. Access Point Mode does not support issue is one of the most asked questions from my readers. Below I will show how to fix it using Broadcom bcm4313 as example.

1. Press Ctrl+Alt+T on keyboard to open terminal. When it opens, run the command below to tell your wireless chip information:

lspci | grep -i Network

In my case, it outputs:

02:00.0 Network controller: Broadcom Corporation BCM4313 802.11bgn Wireless Network Adapter (rev 01)

The chip id is BCM4313, manufactured by Broadcom.

2. Check the below table. Find out the driver that works for your wireless adapter and support AP mode.

For Broadcom, there are three drivers that support AP mode, but only brcmsmac(check link page for supported chips) support wifi chip BCM4313.

Driver Manufacturer Support AP Mode
adm8211 ADMtek/Infineon no
airo Aironet/Cisco ?
ar5523 Atheros no
at76c50x-usb Atmel no
ath5k Atheros yes
ath6kl Atheros no
ath9k Atheros yes
ath9k_htc Atheros yes
ath10k Atheros ?
atmel Atmel ?
b43 Broadcom yes
b43legacy Broadcom yes
brcmfmac Broadcom no
brcmsmac Broadcom yes
carl9170 ZyDAS/Atheros yes
cw1200 ST-Ericsson yes
hostap Intersil/Conexant ?
ipw2100 Intel no
ipw2200 Intel no
iwlegacy Intel no
 iwlwifi Intel yes
 libertas  Marvell  no
libertas_tf  Marvell yes
mac80211_hwsim  Jouni  yes
mwifiex  Marvell  yes
mwl8k  Marvell  yes
orinoco  Agere/Intersil/Symbol no
 p54pci  Intersil/Conexant  yes
p54spi  Conexant/ST-NXP  yes
p54usb  Intersil/Conexant  yes
rndis_wlan  Broadcom  no
rt61pci  Ralink  yes
rt73usb  Ralink  yes
rt2400pci  Ralink  yes
rt2500pci  Ralink  yes
rt2500usb  Ralink  yes
rt2800pci  Ralink  yes
rt2800usb  Ralink  yes
vt6656  VIA  yes
wil6210  Atheros  yes
 wl12xx Texas Instruments  yes
zd1211rw ZyDAS/Atheros  yes

3. Find out current driver running on your wifi adapter by running below command:

ethtool -i wlan0 | grep driver

If need, install ethtool from Ubuntu Software Center. The command outputs something like this:

driver: wl0

So in my case I need to switch wireless driver from wl0 to brcmsmac to get AP mode support.

4. Install brcmsmac driver.

sudo apt-get install firmware-b43-installer

5. After installed the new driver, try to enable it from Additional Drivers utility (search & open it from the Unity Dash), and finally restart.

6. Finally run:

iw list

Below section tells you AP mode is support now:

Supported interface modes:
* IBSS
* managed
* AP
* AP/VLAN
* monitor

For some wireless chips, run below command instead to check out supported interface modes:

sudo iwconfig wlan0 mode master

7 To make this work at next boot, go to /etc/modprobe.d directory in terminal:

cd /etc/modprobe.d/ && ls

Or in your file browser (open as root), find out and remove the line blacklist brcmsmac that blacklist the driver in any file in that directory.