Process records for deploying web projects, including configuration processes for laravel, redis, mongodb, workerman

Keywords: PHP sudo MongoDB Redis

The basic environment of the system LAMP has been built. The process of deploying the web project online is recorded as follows, which is convenient for future deployment of new projects.

laravel project
ubuntu 16.04
php 7.0

(1) We use your@email.com account to git projects on the server

Add your@email.com member to the project to be deployed in gitlab.

(2) Create ssh key pairs for your@email.com account on the server.

If the key pair of the account already exists in the server and the ssh key has been placed in the SSH Key gitlab settings of the account, you can skip this step.

$ sudo ssh-keygen -C  "your@email.com" -t rsa
// Follow the next step directly. If you want to change the name of the key pair, you can do it in this step.
$ sudo su
$ cd ~/.ssh
$ cat id_rsa.pub
// Copy the public key and fill it in the SSH Key of gitlab
(3) Enter the / var/www directory, clone the project, and change the relevant file permissions
$ cd /var/www
// Cut back to ordinary account
$ su ubuntu
$ sudo git clone git@yourprojecturl.com/yourproject.git 
$ cd yourproject
// laravel needs to change the storage folder permissions and bootstrap permissions
$ sudo chmod -R 777 storage/ 
$ cd bootstrap/
$ sudo chmod -R 777 cache
(4) Change apache's Document Root to use / var/www as the access directory
$ cd /etc/apache2/sites-available
$ sudo vi 000-default.conf
// Modify "Document Root/var/www/html" to "Document Root/var/www"
// Restart apache after saving
$ sudo service apache2 restart
(5) Next, enter the server's ip address in the browser and find that the server can display directory files. It needs to disable directory lists and allow url redirection.
$ sudo vi /etc/apache2/sites-available/000-default.conf

//Increase the list of disabled directories and allow url rewirte support. Add the following configuration under Document Root
<Directory "/var/www">
    Options +Includes -Indexes
    AllowOverride All
    Order Deny,Allow
    Allow from all
</Directory> 

//Open apache rewrite Module
$ sudo a2enmod rewrite

// Restart apache
$ sudo service apache2 restart
(6) Because redis is used in the project, redis needs to be installed.

Because redis is used as a message queue, we need to open the listening message queue later, which will be explained later.

// Install redis
$ sudo apt-get install redis-server
// Check to see if the installation was successful
$ ps aux | grep redis

//Configure remote login. The default redis does not allow remote login. We need to configure it.
//But if the application server and redis are installed on the same server, this step is not required.

//Edit redis configuration file
$ sudo vi /etc/redis/redis.conf
//Remark the following line.
# bind 127.0.0.1

//Configure password login

//Edit redis configuration file
$ sudo vi /etc/redis/redis.conf
//Find the following line and remove the comments
# requirepass foobared #Before modification
//After modification, 123456 is the redis password set
requirepass 123456

//Restart redis server
sudo service redis-server restart
(7) Mongodb is used in the project, and mongodb and mongo extensions need to be installed
// Import MongoDB public GPG Key
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

// Create a list file for MongoDB
$ echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

//Reload local package database
$ sudo apt-get update

//Install the MongoDB packages
$ sudo apt-get install -y mongodb-org

The ubuntu version of the system is 16.04, and other versions can be viewed.
https://docs.mongodb.com/manu...
Other configuration methods in

After installation, mongodb's log files and configuration files are in the following paths

/var/log/mongodb

/etc/mongod.conf

Start by opening mongodb without verifying the account password, and create tables and users in mongodb

//Check to see if the installation was successful
$ mongod --version
//Query whether to start mongodb
$ ps aux | grep mongod
//If there is no corresponding process, it starts
$ sudo mongod --logpath=/var/log/mongodb/mongod.log --fork 

//If the boot is unsuccessful, check the log and find that exception in initAndListen: 29 Data directory/data/db is not found.
//Let's create this directory
$ sudo mkdir -p /data/db
//Restart mongo

We need to create database tables and users

//Log in to mongodb
$ mongo 127.0.0.1:27017
//Let's first create an account in admin database
> use admin
//Create an Administrator Account
> db.createUser({user:"admin",pwd:"123456","customData":{description:"Administrator User"},roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
//You can use show users to see if the creation was successful
> show users

//Our system uses yourdb database, switching yourdb database, adding accounts for yourdb
> use yourdb
> db.createUser({user:"youruser",pwd:"12345678","customData":{description:"Administrator User"},roles:[{role:"dbOwner",db:"yourdb"}]})

//Create data tables (collections)
> db.createCollection("erp_records") 
//Check to see if the creation was successful
> show collections

For security, we need to turn on mongodb user authentication, bind the specified ip, and not allow all IP connections

// Edit the mongodb configuration file
$ sudo vi /etc/mongod.conf
//Change the corresponding content in the file, 121.232.33.33 This is the ip you want to bind yourself.
bindIp: [127.0.0.1,121.232.33.33]
authorization:enabled
//Save the configuration file

//Close the mongod process and re-open it in a verifiable manner
$ sudo killall -9 mongod
$ sudo mongod --logpath=/var/log/mongodb/mongod.log --auth --fork

//At this point, we will test the login again.
$ mongo 127.0.0.1:27017
> use yourdb
> show collections
// If the prompt is not validated, then the verification has been started, then verify that the account password set before is correct.
> db.auth("youruser","12345678")
// If 1 is displayed, the representative validation passes
//Logout
> exit

Next, we're going to install mongodb's php extension

//We are servers or php7. If you are not sure what extensions you can install, you can use the following commands to view them
$ sudo apt-cache search php7
$ sudo apt-get install php-mongodb
//Restart Apache 2
$ sudo service apache2 restart
(8) The project uses workerman, which is responsible for the websocket connection to listen for push messages.

The requirements of the program for php environment are as follows:

PHP cli >= 5.4, you can run the command php-v to view the version

linux system requires php to install posix and pcntl extensions.

You can use curl-Ss http://www.workerman.net/chec... php to check whether the current environment meets the requirements. If not, you need to install the necessary extensions as prompted.

If you support a larger number of concurrent connections, it is recommended to install event extensions or libevent extensions (both serve the same purpose, one of two can be chosen).

Our php version is 7.0, installing event

//If there is no pecl, you need to install it first
$ sudo apt-get install php-pear php7.0-dev libevent-dev
//Tip ERROR: `phpize'failed
$ pecl install event

// When Include libevent OpenSSL support [yes]: appears, enter no

//Switch to the root user and add event.so to the php-ini file of php-cli.
$ sudo su

$ echo extension=event.so > /etc/php/7.0/cli/conf.d/event.ini

//Switch back to the normal user, switch to the directory where you want to save the project, clone web-msg-sender project
$ su ubuntu

$ cd /var/www

$ git clone https://github.com/walkor/web-msg-sender.git

Use composer to install. If you do not install composer, install it first.

// Download composer

$ curl -sS https://getcomposer.org/installer | php

// Setting global

$ sudo mv composer.phar /usr/local/bin/composer

// Check if the installation is successful, and if version information is displayed, the installation is successful.

$ composer -v

// Update

$ composer self-update

// Go into the web-msg-sender project and install it using composer

$ cd /var/www/web-msg-sender/

$ composer install

Open Server Monitor

Enter the project file and start the service (in daemon mode)

$ php start.php start -d

Out of Service

$ php start.php stop

Service state

$ php start.php status

=== Note the connection ip of the front-end websoket and the service that curl workerman monitors
Change to the server ip corresponding to workerman ==

(9) Open message queue listening

Our queue name is GetAllPlatform Order

//Get under the project
$ cd /var/www/yourproject

$ php artisan queue:work --queue=GetAllPlatformOrder  --daemon

queue:work --daemon can always accept requests as long as it runs. The difference is that in this mode of operation, when new requests come, it does not reload the entire framework, but fire s directly.

But it can't be a daemon. It needs some process control tools, such as Supervisor.

(9) Update the database online.

Subsequently, we supplement Supervisor to explain the daemon queue monitoring process and git push automatic deployment to the server.

Posted by acroporas on Tue, 18 Dec 2018 05:03:04 -0800