Pulling GitLab Code To DigitalOcean Server

https://premium.wpmudev.org/blog/improve-wordpress-development-workflow-local-server/

Create the empty Git repository on the production server

4. From your local command line create an ssh connection to the remote server:

$ ssh username@your_ip_address

Assuming Git is already installed, configure global settings with same info used to connect your pc/laptop with GitLab:

$ git config --global user.name "NewUser"
$ git config --global user.email [email protected]

you can check to make sure they are added:

$ git config --list

5. Generate a ssh keypair

First to verify you do not already have an ssh key pair:

cat ~/.ssh/id_rsa.pub

if you do not have a keypair, you will see something like:

cat: /home/... ... No such file or directory

Next, generate a new keypair:

$ ssh-keygen -t rsa -C "GitLab" -b 4096

you will be prompted to input a file path to save your key pair to – you can just hit ‘enter'. Using the suggested path will allow your SSH client to automatically use the key pair with no additional configuration.

Once you have input a file path you will be prompted to input a password to secure your SSH key pair. It is a best practice to use a password for an SSH key pair, but it is not required and you can skip creating a password by pressing enter. I will skip it for now. If you want to change the password of your key, you can use

ssh-keygen -p <keyname>.

Using WinSCP, go to the directory where the new keypair is installed (‘/home/username/.ssh'), open the public keypair (id_rsa.pub) and copy all of the contents.

Inside your project folder in GitLab under the section ‘Deploy Keys'. Press the ‘New Deploy Key' button and copy the contents inside the ‘Key' section and fill in a title. After this, the machine that uses the corresponding private key has read-only access to the project.

If you want to add the same key to another project, please enable it in the list that says ‘Deploy keys from projects available to you'. All the deploy keys of all the projects you have access to are available. This project access can happen through being a direct member of the project, or through a group. Deploy keys can be shared between projects, you just need to add them to each project.

https://gitlab.com/help/ssh/README

1. Create a new database manually on the production server (using phpmyadmin or from the command line).

2. Make a copy of your local wp-config file and change the database details as necessary.

3. Upload it one level above the WordPress root directory with WinSCP or a FTP client. In my case it will be at ‘/var/www/site.com/' – just before the html folder where all of the rest of the files will go.

WordPress will automatically find the wp-config file there, so there is no additional setup required to make this work, and it probably increases your site’s security.

This approach has the added benefit of keeping sensitive information from the wp-config in your git repo. This means you can use a public repo and not worry about exposing your database information or security salts during regular Git pulls.

Navigate to the directory where WordPress will be installed on the production server. In my case:

$ cd /var/www/site.com/html

Next, you need to tell git that you want to use this current directory as a git environment.

$ git init

ERRORS & SOLUTIONS:
There are a number of errors that may popup when you run ‘git init'.

Error 1.

/var/www/site.com/html/.git: Permission denied

I got this error when trying to use a username other tha ‘root'. When I logged out of my username, logged back in as ‘root', and then ran ‘git init' – SUCCESS.

However, another method would be to change the folder permissions and add your username to the group. Logout and login as ‘root'. Substitute ‘username' below with your username:

$ cd /var/www/site.com
$ chown -R www-data:www-data
$ usermod -a -G www-data username .

Logout of ‘root' and back in with your username and you should be able to run ‘git init' using sudo (possibly without sudo?):

sudo git init

Other git commands such as ‘git status', etc should work fine but I had to use sudo to clone a repo:

$ sudo git clone https://gitlab.com/username/repo.git

Error 2.

Also, when cloning a repo using the command above, it will clone the folder and files inside of the directory that you are in, for example:

$ cd /var/www/site.com/html
$ sudo git clone https://gitlab.com/username/repo.git

The production server directory server will look like:

/var/www/site.com/html/repo/

which is not what we want as we want all of the repo files to be in ‘/var/www/site.com/html/'. We could move the files to the correct folder after cloning but this is not optimal:

$ mv /var/www/site.com/html/repo/* /var/www/site.com/html`
$ mv /var/www/site.com/html/repo/.* /var/www/site.com/html`
$ rmdir /var/www/sites/site.com/html/repo

http://stackoverflow.com/questions/17581379/git-clone-without-project-folder

Error 2, Solution 1:

http://stackoverflow.com/questions/6224626/clone-contents-of-a-github-repository-without-the-folder-itself

http://stackoverflow.com/questions/651038/how-do-you-clone-a-git-repository-into-a-specific-folder

If the current directory is empty, just add a ‘.' (period) after the remote url. If you already ran ‘git init' in this folder' (as we did above), then just remove the .git folder before cloning:

$ git clone [email protected]:username/repo.git .

But by using the method above, if in the future we need to pull more content into this folder, we can't do it because clone only works if the folder is empty. We would have to delete or move the folder first and then re-clone from the repo which wouldn't be good in a live server environment.

Error 2, Solution 2:

http://stackoverflow.com/questions/6224626/clone-contents-of-a-github-repository-without-the-folder-itself

$ git remote add origin https://github.com/username/repo.git
$ git pull origin master
$ cd /var/www/site.com/html
$ sudo git remote add origin https://gitlab.com/username/repo.git
$ sudo git add .
$ sudo git commit
$ sudo git pull origin master

SUCCESS


Not sure if we need to do these next steps each time we pull from the repo!

Navigate to the directory where we copied wp-config.php:

$ cd /var/www/site.com

change permissions:

$ sudo chown -R www-data:www-data .

NO WE DO NOT NEED TO CHANGE PERMISSIONS AGAIN!


7. Sync the database

From your local development server, login to phpmyadmin and export the database tables.

Login to phpmyadmin on your server and import the database.

To update the database on the live server – in phpmyadmin, go to SQL tab and enter (make sure you select the correct database):

SELECT * FROM wp_options WHERE option_name = "home" OR option_name = "siteurl";
UPDATE wp_options SET option_value = "https://yourdomain.com/" WHERE option_name = "home" OR option_name = "siteurl"

There should be updates to 2 tables. Then run this sql query to update posts:

UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://127.0.0.1/mywebsitefolder', 'https://yourdomain.com/');

At this point we still probably can't log in to our WordPress control panel because we haven't set up the site and encryption for NGINX.

https://codex.wordpress.org/Running_a_Development_Copy_of_WordPress

http://www.wpbeginner.com/wp-tutorials/how-to-move-wordpress-from-local-server-to-live-site/

https://codex.wordpress.org/Changing_The_Site_URL

Using FTP, upload the folder ‘/wp-content/uploads' to the server (this folder was included in our ‘.gitignore' file.


NGINX settings

Many of these settings are taken from How to Host Multiple Sites Using NGINX and slightly modified for this setup.

Next, I copied the contents from one of the sites (not the default) at ‘/etc/nginx/sites-enabled/' and created a new file named with the site name, for example site.com.

Then create symbolic links from this file to the sites-enabled directory, which Nginx reads during startup (replace ‘site2.com' with your own domain name):

$ sudo ln -s /etc/nginx/sites-available/site2.com /etc/nginx/sites-enabled/site2.com

Install the LetsEncrypt certificate (replace site.com with your domain name):

$ sudo letsencrypt certonly -a webroot --webroot-path=/var/www/site.com/html -d site.com -d www.site.com

Enter an email address and accept the license. If everything works OK, you should see something like this:

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/yourdomain.com/fullchain.pem. Your cert
will expire on 2017-01-22. To obtain a new version of the
certificate in the future, simply run Let's Encrypt again.
- If you like Let's Encrypt, please consider supporting our work by:
 
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

Create a configuration file with our SSL parameters (replace site.com with your domain name):

$ sudo nano /etc/nginx/snippets/ssl-site.com.conf

Then, add this code into the file (replace site.com with your domain name):

ssl_certificate /etc/letsencrypt/live/site.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/site.com/privkey.pem;

CTRL-X to save, then ‘Y’ and ENTER to exit.

Check to make sure the NGINX syntax is correct:

$ sudo nginx -t

then, restart NGINX:

$ sudo service nginx restart

IMPORTANT NOTE FROM How to Host Multiple Sites Using NGINX

When i first did all of the steps above, everytime I tried to load the new site (site2.com) into a browser, I was redirected to the default site1.com. When searching for solutions, I found that this is a very common issue with nginx when trying to add multiple sites to the same server. The solution wasn't really a solution at all – I just came back and tried to load the site again (about 12-24 hours later) and it worked! My guess is that the DNS wasn't completely resolving from the nameserver changes that we did above in DigitalOcean and our domain name provider. It just needed to take more time to resolve (your domain name provider will generally state it may take 24-48 hours but in my experience it only takes a few minutes or less) – or it was just the browser cache or the server cache setup in our previous nginx setup :).

Now you can visit your domain in a web browser and complete the installation by following the WordPress prompts. Note the database name, user and password that you created earlier.


At this point, we check to make sure the site is up and running.

Login to your WordPress admin panel, and go to Settings » General. Click save Options. This will ensure that the site url is corrected anywhere else that needs to be.

Then go to Settings » Permalink and click Save to ensure that all post links are working fine.

Now we want to install WordPress plugins and setup email on our local server so that we can push these changes to GitLab and finally our live server. This keeps everything in sync.

Install the Nginx Helper plugin.

After logging in to the WP admin panel, go to Plugins > Add New, and in the ‘Search Plugins’ box enter ‘nginx plugin helper’. Click ‘Install Now’ and once completed, click ‘Activate’.

7.2 Go to Settings > Nginx Helper from the WP dashboard and check the box to “Enable Purge.” The default settings should be fine. Click ‘Save All Changes’.

7.3 From Step 1 above, we will use the Mailgun for WordPress plugin.

Go to Plugins > Add New, and in the ‘Search Plugins’ box enter ‘mailgun’ where you should see “Mailgun for WordPress”. Click ‘Install Now’ and once completed, click ‘Activate’.

Then, go to Settings > Mailgun in the WP dashboard and copy/paste in your Mailgun domain name and API key, then click ‘Save Changes’. Click ‘Test Configuration’ to make sure it is working. You can also use the Check Email plugin just to make sure that emails are being sent correctly.

Inside of your Mailgun dashboard, you may have to go to Domains > yourDomainName, go down the page ‘Security Settings For Outgoing Mail’, click ‘Certificate Verification’ and select ‘Not Required’ (‘TLS Connection’ should already be set to ‘Opportunistic’).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.