Sie sind auf Seite 1von 19

Ubuntu 12.

04 LTS LAMP
Server Setup

2 YEARS AGO

ubuntu

server

cli

This will go over getting an Ubuntu 12.04 LTS server up and running for production use. Note: This
does not pretend to be an exhaustive resource, particularly around security. Additionally, if your
environment is more complex (Separate DB servers, servers across data-centers, different access
levels, etc), then your setup can and will vary.

Setup
This will install some basic packages, including php 5.4.

$ sudo apt-get update


$ sudo apt-get install -y vim # Everyone likes vim, right?
$ sudo apt-get install -y build-essential
$ sudo apt-get install -y python-software-properties

# Run these 2 steps if you want php 5.4, rather than 5.3
$ sudo add-apt-repository ppa:ondrej/php5

$ sudo apt-get update

# Install the LAMP components


$ sudo apt-get install -y php5
$ sudo apt-get install -y apache2
$ sudo apt-get install -y libapache2-mod-php5
$ sudo apt-get install -y mysql-server
$ sudo apt-get install -y php5-mysql
$ sudo apt-get install -y php5-curl
$ sudo apt-get install -y php5-gd
$ sudo apt-get install -y php5-mcrypt

# Set your server name (Avoid error message on reload/restart of Apache)


$ echo 'ServerName localhost' | sudo tee /etc/apache2/httpd.conf

# Enable mod-rewrite
$ sudo a2enmod rewrite

Git
You may need Git on your server depending on your deployment strategy, or to support package
managers such as Composer.

$ sudo apt-get install -y git-core

Composer
If you use Composer, you should also have it on your production server to pull in dependencies.
Note: For production use, you should lock in your dependency version numbers. That way you won't
get any surprises when you update composer packages on your live server.

# Install composer globally


$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

Tweaks/Security
Here we'll make some light performance tweaks, and adjust some settings for security.
First, some providers allow root login via SSH. We want to turn that off. I suggest opening a new SSH
connection immediately after creating a sudo user (in a separate Terminal session/window) before
doing this, just in case you lock yourself out by accident.
If your provider gives you a login other than "root", then you likely have a sudo user already and can
skip the step of creating a sudo user. However, you should still ensure that you cannot log in as root.

# Create a new sudo user


$ adduser mysudouser

# Create user

$ usermod -G sudo mysudouser

# Make user a sudo user (sudoer)

# (Log in and make sure this sudo user does indeed have the sudo permissiosn)

# Don't let root login in via ssh


$ sudo vim /etc/ssh/sshd_config
> PermitRootLogin no

# Change from yes

$ sudo reload ssh


I typically also create a user for deployment. This user will share the same primary group as apache
(www-data), and so will be able to read/write the web-server files. This is not a sudo user.

# Deploy user
$ adduser mydeployuser
$ usermod -g www-data mydeployuser
Here are some performance tweaks for Apache. We'll decrease the timeout time, and allow more
keep-alive requests.

# Apache tweaks
$ sudo vim /etc/apache2/apache2.conf
> Timeout 45
> MaxKeepAliveRequests 200

# Change from 300


# Change from 100

Here are security tweaks. We'll turn off how much information about the server is returned in the
HTTP headers.

$ sudo nano /etc/apache2/conf.d/security

> ServerTokens Prod

# Change from 'OS' or any other

> ServerSignature Of

# Change from 'On'

These are general PHP settings to tweak. Bump up the file size for file uploads, but decrease how
many can be uploaded at once. As

a security tweak, also turn off the display of


which PHP version is being used.
$ sudo nano /etc/php5/apache2/php.ini
> post_max_size = 8M

# Change to 8M

> upload_max_filesize = 8M

# Change from 2M

> max_file_uploads = 5
> expose_php = of

# Change from 20
# Change fron 'On'

$ sudo service apache2 restart


The directory /var/www is the main web-root. The following will give everything in the web root the
Apache user and group. This way Apache and the 'deploy' user are the only ones who can read/write
web files. Note: This makes use of group permissions. The following is saying "Users and Groups
can read and write these files, but other users can only read them".

# Web-root permissions
$ sudo chown -R www-data:www-data /var/www # make sure same owner:group
$ sudo chmod -R go-rwx /var/www

# Remove all group/other permissions

$ sudo chmod -R g+rw /var/www

# Add group read/write

$ sudo chmod -R o+r /var/www

Virtual Hosts

# Allow other to read only

This is a command-line tool I created for generating a virtual host within Apache (Ubuntu specific).
This will enable the use of .htaccess files and turn off index listings by default. It also sets up log files
per virtual host.

# vhosts
$ curl
https://gist.github.com/fideloper/2710970/raw/6b5fd9de45f75e613178d296e87f586ca5b612
20/vhost.sh > /usr/local/bin/vhost
$ chmod guo+x /usr/local/bin/vhost
$ sudo vhost -h # See the available options

Firewall
This is exactly as per here. It will allow port 22 (or current ssh port), 80 and 443 (ssh, web traffic, ssl
web traffic respectively). It also gives loopback access, important if your server is virtualized (chances
are, it is).

# Run as root or use sudo


$ sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
$ sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT
$ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
$ sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
$ sudo iptables -A INPUT -j DROP
$ sudo iptables -I INPUT 1 -i lo -j ACCEPT

# Install so firewalls are saved through restarts


$ sudo apt-get install -y iptables-persistent
$ sudo service iptables-persistent start

Add MySQL user


This will create a MySQL user for your application to use. Note the example below will
grant allpermissions. You should be as restrictive as possible, giving only what's necessary. In many
instances, MySQL users will will be OK with simply having SELECT, UPDATE, DELETE, and INSERT
privileges, but there are many more privileges to choose from. Additionally, any user who will be
used to mysqldumpwill need the LOCK TABLES privilege.
Also note that if you have a MySQL database on a separate server, you'll need to change localhost to
the IP or host name of the server connecting to the database. There is also some more work to allow
remote connections on MySQL (Editing my.cnf bind-address and using firewalls to only allow MySQL
connections on the same local network is one strategy).

$ mysql -u root -p
> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
> GRANT ALL PRIVILEGES ON database.* TO 'user'@'localhost';

Add SSL
If you need to install an SSL certificate, this has worked for me when installing a (non-premium) SSL.
This assumes a 1-year SSL, with 2048 encryption. YMMV.

$ sudo a2enmod ssl

# Enable loading of SSL module

$ sudo service apache2 restart


$ sudo mkdir /etc/apache2/ssl
$ cd /etc/apache2/ssl

# Change the domain from "yourdomain.com" to what you need


$ sudo openssl req -new -days 365 -nodes -newkey rsa:2048 -keyout yourdomain.com.key
-out yourdomain.com.csr
$ sudo chmod 400 yourdomain.com.key
add csr, get key back
$ sudo vim /etc/apache2/sites-available/your_vhost.conf
> SSLEngine on
> SSLCertificateFile /etc/apache2/ssl/yourdomain.com.crt
> SSLCertificateKeyFile /etc/apache2/ssl/yourdomain.com.key
> SSLCertificateChainFile /etc/apache2/ssl/sf_bundle.crt
And that's it - These steps will get you started on having a usable Ubuntu 12.04 server.

LAMP Server on UBUNTU


1. Install Apache
To install Apache you must install the Metapackage apache2. This can be done by
searching for and installing in the Software Centre, or by running the following
command.

sudo apt-get install apache2

2. Install MySQL
To install MySQL you must install the Metapackage mysql-server. This can be done
by searching for and installing in the Software Centre, or by running the following
command.

sudo apt-get install mysql-server

3. Install PHP
To install PHP you must install the Metapackages php5 and libapache2-mod-php5.
This can be done by searching for and installing in the Software Centre, or by
running the following command.

sudo apt-get install php5 libapache2-mod-php5

4. Restart Server
Your server should restart Apache automatically after the installation of both
MySQL and PHP. If it doesn't, execute this command.

sudo /etc/init.d/apache2 restart

5. Check Apache
Open a web browser and navigate tohttp://localhost/. You should see a message
saying It works!

6. Check PHP
You can check your PHP by executing any PHP file from within /var/www/.
Alternatively you can execute the following command, which will make PHP run
the code without the need for creating a file .

php -r 'echo "\n\nYour PHP installation is working fine.\n\n\n";'

Congratulations, you have just Installed a Ubuntu


LAMP Server!

1. 5 Steps to connect to the database in java


1.

5 Steps to connect to the database in java

1.

Register the driver class

2.

Create the connection object

3.

Create the Statement object

4.

Execute the query

5.

Close the connection object

There are 5 steps to connect any java application with the database in java using JDBC. They are as follows:
Register the driver class
Creating connection
Creating statement
Executing queries
Closing connection

1) Register the driver class


The forName() method of Class class is used to register the driver class. This method is used to dynamically
load the driver class.

Syntax of forName() method


1.

public static void forName(String className)throws ClassNotFoundException

Example to register the OracleDriver class


1.

Class.forName("oracle.jdbc.driver.OracleDriver");

2) Create the connection object


The getConnection() method of DriverManager class is used to establish connection with the database.

Syntax of getConnection() method


1.
2.
3.

1) public static Connection getConnection(String url)throws SQLException


2) public static Connection getConnection(String url,String name,String password)
throws SQLException

Example to establish connection with the Oracle database

1.
2.

Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");

3) Create the Statement object


The createStatement() method of Connection interface is used to create statement. The object of statement is
responsible to execute queries with the database.

Syntax of createStatement() method


1.

public Statement createStatement()throws SQLException

Example to create the statement object


1.

Statement stmt=con.createStatement();

4) Execute the query


The executeQuery() method of Statement interface is used to execute queries to the database. This method
returns the object of ResultSet that can be used to get all the records of a table.

Syntax of executeQuery() method


1.

public ResultSet executeQuery(String sql)throws SQLException

Example to execute query


1.
2.
3.
4.
5.

ResultSet rs=stmt.executeQuery("select * from emp");


while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

5) Close the connection object


By closing connection object statement and ResultSet will be closed automatically. The close() method of
Connection interface is used to close the connection.

Syntax of close() method


1.

public void close()throws SQLException

Example to close connection


1.

con.close();

How To Install Linux, Apache, MySQL,


PHP (LAMP) stack on Ubuntu
Tags: LAMP Stack, Apache, MySQL, PHP Distribution: Ubuntu

For Ubuntu 12.04 - see this updated tutorial for Ubuntu 14.04.

About LAMP
LAMP stack is a group of open source software used to get web servers up and running. The acronym
stands for Linux, Apache, MySQL, and PHP. Since the virtual private server is already running Ubuntu,
the linux part is taken care of. Here is how to install the rest.

Set Up
The steps in this tutorial require the user to have root privileges on your VPS. You can see how to set that
up in the Initial Server Setup in steps 3 and 4.

Step OneInstall Apache


Apache is a free open source software which runs over 50% of the worlds web servers.
To install apache, open terminal and type in these commands:
sudo apt-get update
sudo apt-get install apache2

Thats it. To check if Apache is installed, direct your browser to your servers IP address (eg.
http://12.34.56.789). The page should display the words It works!" like this.

How to Find your Servers IP address


You can run the following command to reveal your servers IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'

Step TwoInstall MySQL


MySQL is a powerful database management system used for organizing and retrieving data
To install MySQL, open terminal and type in these commands:
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

During the installation, MySQL will ask you to set a root password. If you miss the chance to set the
password while the program is installing, it is very easy to set the password later from within the MySQL
shell.
Once you have installed MySQL, we should activate it with this command:
sudo mysql_install_db

Finish up by running the MySQL set up script:


sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password.
Type it in.
Enter current password for root (enter for none):

OK, successfully used password, moving on...

Then the prompt will ask you if you want to change the root password. Go ahead and choose N
and move on to the next steps.
Its easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new
changes.
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.

This is intended only for testing, and to make the installation

go a bit smoother.

You should remove them before moving into a

production environment.

Remove anonymous users? [Y/n] y


... Success!

Normally, root should only be allowed to connect from 'localhost'.

This

ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y


... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.

This is also intended only for testing, and should be removed

before moving into a production environment.

Remove test database and access to it? [Y/n] y


- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.

Reload privilege tables now? [Y/n] y


... Success!

Cleaning up...

Once you're done with that you can finish up by installing PHP.

Step ThreeInstall PHP


PHP is an open source web scripting language that is widely use to build dynamic webpages.
To install PHP, open terminal and type in this command.
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

After you answer yes to the prompt twice, PHP will install itself.
It may also be useful to add php to the directory index, to serve the relevant php index files:
sudo nano /etc/apache2/mods-enabled/dir.conf

Add index.php to the beginning of index files. The page should now look like this:
<IfModule mod_dir.c>

DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml


index.htm

</IfModule>

PHP Modules
PHP also has a variety of useful libraries and modules that you can add onto your virtual server. You can
see the libraries that are available.
apt-cache search php5-

Terminal will then display the list of possible modules. The beginning looks like this:
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
php5-gmp - GMP module for php5
php5-ldap - LDAP module for php5
php5-mysql - MySQL module for php5
php5-odbc - ODBC module for php5
php5-pgsql - PostgreSQL module for php5
php5-pspell - pspell module for php5
php5-recode - recode module for php5
php5-snmp - SNMP module for php5
php5-sqlite - SQLite module for php5
php5-tidy - tidy module for php5
php5-xmlrpc - XML-RPC module for php5

php5-xsl - XSL module for php5


php5-adodb - Extension optimising the ADOdb database abstraction library
php5-auth-pam - A PHP5 extension for PAM authentication
[...]

Once you decide to install the module, type:


sudo apt-get install name of the module

You can install multiple libraries at once by separating the name of each module with a space.
Congratulations! You now have LAMP stack on your droplet!

Step FourRESULTS: See PHP on your Server


Although LAMP is installed, we can still take a look and see the components online by creating a quick
php info page
To set this up, first create a new file:
sudo nano /var/www/info.php

Add in the following line:


<?php
phpinfo();
?>

Then Save and Exit.


Restart apache so that all of the changes take effect:
sudo service apache2 restart

Finish up by visiting your php info page (make sure you replace the example ip address with your correct
one): http://12.34.56.789/info.php

It should look similar to this.

See More
After installing LAMP, you can Set Up phpMyAdmin, Install WordPress, go on to do more with MySQL (A
Basic MySQL Tutorial), Create an SSL Certificate, or Install an FTP Server.

Das könnte Ihnen auch gefallen