how to upload php application to ubuntu LAMP droplet and connect app with mysql?

August 10, 2017 298 views
Applications Ubuntu

I created a preset droplet call ubuntu LAMP.

I can access to this droplet via ssh. And that's all. I don't know what to do with the following steps

Where in the droplet should I upload my PHP app to?

How can I config droplet's mysql so that my app can connect to mysql?

How can I upload my data to droplet's mysql ?

7 Answers

In order to get your php app to work you are going to need to follow a number of steps all on your server's command line.

  1. Upload your application and set ownerships on your files.
  2. Configure Apache to serve your application.
    See steps 1,2,4,5 from this tutorial:
    https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04

  3. Create a database.

  4. Import in your existing db into the new database.
    See the importing section of this tutorial:
    https://www.digitalocean.com/community/tutorials/how-to-import-and-export-databases-in-mysql-or-mariadb

After you have created a new database and imported your data you should create a new mysql user and give them access to the db. You need to log into the mysql server and run the following command:

 GRANT ALL ON database.* TO user@localhost IDENTIFIED BY 'password';

Replace "database" with the name of the db you created.
Replace "user" with a new username.
Replace "password" with a password.

  1. Configure your php app to use the database. Somewhere in you php app there will be a file where the database access information is held. This will require three pieces of information:
    1. Location
    2. User
    3. Password

The location will be localhost and the username and password will be the ones you set in the "GRANT ALL..." command you ran.

The Apache web server is the most popular way to serve web content on the internet. Apache has the ability to serve multiple domains from a single server by using a mechanism called "virtual hosts". If a virtual host is configured correctly for each domain, the web server can correctly route traffic to the appropriate files based on the domain name requested. In this guide, we'll demonstrate how to configure Apache virtual hosts on an Ubuntu 16.04 server.

Upload your application and set ownerships on your files

upload to where?

Say I connect to server using this command

ssh root@174.138.65.31

Then what should I do next to upload ?

The command:

ssh root@174.138.65.31

will log you into a terminal on your server it will not upload any data. The easiest way to upload data is via the SFTP protocol as your server already supports this via the SSH server.

How you do this that depends on what OS you are running on your laptop. If you are running Windows then I recommend WinSCP:

https://winscp.net/eng/index.php

If you are using OSX then Cyberduck:

https://cyberduck.io/

And if you are running Linux locally, you can either use SCP from the command prompt or use your file manager (Files on Gnome or Dolphin on KDE) to upload the files.

There is no single correct location. You can put it anywhere you like, however, the standard location for a website or web app is under:

/var/www

If your app is called SuperApp then:

/var/www/SuperApp

Would be a good location.

When I try to copy a file from local to www folder I got this error

cp: cannot create directory ‘/var/www/config’: Permission denied

This has happened because /var/www is owned by root and you are attempting to create a new directory as a non-root user.

If you have logged into the server via SSH as a non-root user then you will need to log out and log back in again as root and follow the instructions here to create a sudo user:

https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04

If you are attempting to create the directory via an SFTP program like WinSCP then you will need to use your root account to log in.

When you start a new server, there are a few steps that you should take every time to add some basic security and give you a solid foundation. In this guide, we'll walk you through the basic steps necessary to hit the ground running with Ubuntu 16.04.
  • what would be the password to my SFTP account ?

    Is the password to ftp the same the password I use to login digitial ocean online app ?

    And is my sftp username is root?

Have another answer? Share your knowledge.