How to create own Packagist mirror site on Ubuntu 18.04
Tags: packagist, mirror
Published: 2020-06-24 19:16:00

---
title: How to create own Packagist mirror site on Ubuntu 18.04
published: true
description: A tutorial about creating a Packagist mirror on Ubuntu 18.04.
tags: packagist,mirror 
cover_image: https://www.userlogos.org/files/packagist.gif
---

# Introduction

If you're a modern PHP developer, you may heard about the [packagist.org](https://packagist.org) and [composer](https://getcomposer.org/).

The Packagist is a PHP package repositories and it stores many composer-based packages.

What about Composer? It's a package manager for PHP and it can help PHP developers to be easy to manage all kinds of PHP packages via Composer.

And its functionalities are very similar with NPM (a Node.js package manager).

If you use a lot for installing PHP packages via Composer, you may know about this [issue](https://github.com/composer/composer/issues/9003).

This issue is about the main Packagist of Asia mirror has the bandwidth problem. (For now, it has been fixed.)

And it causes many PHP developers cannot install some PHP packages via `composer install`, `composer require` and `composer update` at that moment.

I also notice this issue and try to find another mirror to replace.

However, most of mirror locations are listed on official [Packagist Org](https://packagist.org/mirrors) and [this](https://github.com/webysther/packagist-mirror#-packagist-public-metadata-mirrors-observatory-around-the-world) are far away from me.

And sometimes these locations cannot save my time because it takes slow time  during composer package installation.

To resolve this issue, I want to setup my own Packagist mirror sites in my country.

And I also use [packagist-mirror](https://github.com/webysther/packagist-mirror) to accomplish my above issue because this mirror setup is recommended by [packagist.org](https://packagist.org/mirrors).

In this tutorial, I will show you how to use my customized Bash script to setup our own Packagist mirror site on Ubuntu 18.04 hosting system.

# Perquisites

Here are some required stuffs before we get started with setting up our amazing Packagist mirror site:

- One VPS (virtual private server) with at least one core CPU, 2GB RAM and a clean Ubuntu 18.04 operating system.
- A registered domain, it's nice to let developers be easy to know mirror site and use it.
- `wget` command is available now on your VPS.
- It's nice to have a `.env` file on current home directory.
- It's nice to have the [nginx-default](https://gist.githubusercontent.com/peter279k/cab67ba6207ec291747cc3cc6071a519/raw/e85bf920c3077236170da693ac21e21d8f86c015/nginx-default) and [nginx.conf](https://gist.githubusercontent.com/peter279k/cab67ba6207ec291747cc3cc6071a519/raw/e85bf920c3077236170da693ac21e21d8f86c015/nginx.conf) files are located on current user home directory.

# Installation

If you've looked at the [packagist-mirror](https://github.com/webysther/packagist-mirror) repository, you may find it has many kinds of ways to install the Packagist mirror site.

Including Docker container building, hosting setup and so on.

In this tutorial, I will use the hosting way to install the Packagist mirror site and I also provide a automated Bash script to help developers to setup Packagist mirror happily :).

## Step 1: Download a packagist_mirror_installer.sh script

It's easy to use following command to download a `packagist_mirror_installer.sh`:

```Bash
wget https://gist.githubusercontent.com/peter279k/cab67ba6207ec291747cc3cc6071a519/raw/46495d8dad37e769f4065f46925ff7b8d22b1e7e/packagist_mirror_installer.sh
```

After script downloading has been completed, it can change this Bash script permission into `755`.

That is, owner can read, write and execute this script, and group owner or other users can read and execute the script.

## Step 2: Run this packagist_mirror_installer.sh script

After setting above permission is successful, executing following command to start Packagist mirror site building:

```Bash
./packagist_mirror_installer.sh
```

And it will present following captured picture:

![update_upgrade](https://i.imgur.com/4R5H04N.png)

It's doing for update and installing required packages.

By default, the system locale will setup for `en_US.UTF-8`.

If developers want to change locale, it will set specific locale manually.

When installing required packages has been done, it will present following pictures to let developers select current timezone:

![timezone_area](https://i.imgur.com/nCjkSOM.png)

![timezone_country](https://i.imgur.com/OPvQQ2H.png)

After selecting timezone has been done, it will present following picture to tell developers setting a host name for this mirror site:

![ask_set_host_name](https://i.imgur.com/2uRrREf.png)

If developers don't set any host name and type enter, script will try to use `hostname` to get current host name on this hosting operating system. And ask you to ensure set this host/domain name.

If the host name is configured correctly, developers can type `Y` or `y` to continue the next step.

## Copy nginx.conf and nginx sites default files

After setting the host name is successful, the script will try to find the `nginx.conf` and `nginx-default` files on current user home directory.

If it doesn't find any Nginx configuration files, it will ask you to skip this copy work and do next step.

If developers type `Y` or `y`, it will skip above steps. Otherwise terminate this script with exit code `1`.

![nginx_conf_ask](https://i.imgur.com/Swemdxl.png)

After doing above steps about copying Nginx configuration files, it will present following picture to tell whether developers to delete all files/folders on `/var/www/html/*` directory.

If typing `Y` or `y`, it should delete all of the things on `/var/www/html` directory.

![ask_var_www_html](https://i.imgur.com/9vETWYp.png)

After manipulating the `/var/www/html` folder, it will ask developers whether updating Packagist repository:

![packagist_repo](https://i.imgur.com/cYv2hay.png)

If developers type `Y` or `y`, it will use `git pull origin master` command to update Packagist repository right now. Otherwise skipping this step.

After updating Packagist repositories step, it will present following picture:

![composer_nginx_start](https://i.imgur.com/A8dEoUC.png)

It will do following steps for above captured picture:

- Download Composer.
- Using `composer install` to install some packages for `packgist-mirror` repository.
- Asking developers whether executing the `composer update -n` to update dependencies for above repository.

After doing some steps, it will present following picture to tell developers whether syncing Packagist mirror:

![sync_mirror](https://i.imgur.com/TfAT8Zf.png)

If developers type `Y` or `y`, it will use `php bin/mirror create -vvv` command to update Packagist repository right now. Otherwise skipping this step.

After syncing or skip Packagist mirror syncing step, it will present following picture:

![install_certbot](https://i.imgur.com/JOnANzo.png)

It does some steps:

- Update package mirror for Ubuntu operating system and install required packages for `cert-bot`. This can help developers to install a `Let's Encrypt` SSL certificate to secure mirror website easily.
- Asking developers to determine setup SSL certificate with `cert-bot`:

![ask_certbot_setting](https://i.imgur.com/2fUbN1J.png)

If developers type `Y` or `y`, it will do `cert-bot` command interactively.

And help developers to setup and configure a SSL certificate quickly.

## Editing DotEnv file

Editing `.env` file is very simple, and developers can scratch this file from [.env.example](https://github.com/webysther/packagist-mirror/blob/master/.env.example) file.

And it's very clear for every developer to know each DotEnv values have the comment and let us know setting functionality easily.

## Customizing index page

If developers want to customize index page, it's easy to edit the [index.html.php](https://github.com/webysther/packagist-mirror/blob/master/resources/index.html.php) file.

## Summary

After doing above Bash script correctly, it will have this [page](https://packagist.tw).

Enjoy it and have your own Packagist mirror right now :)!