Beruflich Dokumente
Kultur Dokumente
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.
# Run these 2 steps if you want php 5.4, rather than 5.3
$ sudo add-apt-repository ppa:ondrej/php5
# 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.
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.
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 user
# (Log in and make sure this sudo user does indeed have the sudo permissiosn)
# 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
Here are security tweaks. We'll turn off how much information about the server is returned in the
HTTP headers.
> ServerSignature Of
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
# Change to 8M
> upload_max_filesize = 8M
# Change from 2M
> max_file_uploads = 5
> expose_php = of
# Change from 20
# Change fron 'On'
# Web-root permissions
$ sudo chown -R www-data:www-data /var/www # make sure same owner:group
$ sudo chmod -R go-rwx /var/www
Virtual Hosts
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).
$ 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.
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.
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.
4. Restart Server
Your server should restart Apache automatically after the installation of both
MySQL and PHP. If it doesn't, execute this command.
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 .
1.
2.
3.
4.
5.
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
Class.forName("oracle.jdbc.driver.OracleDriver");
1.
2.
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
Statement stmt=con.createStatement();
con.close();
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.
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.
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
The prompt will ask you for your current root password.
Type it in.
Enter current password for root (enter for none):
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.
go a bit smoother.
production environment.
This
ensures that someone cannot guess at the root password from the network.
By default, MySQL comes with a database named 'test' that anyone can
access.
Reloading the privilege tables will ensure that all changes made so far
Cleaning up...
Once you're done with that you can finish up by installing PHP.
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>
</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
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!
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
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.