group_5622_0

In the past two days, I have looked through the previous technical documents, and found an article worth sharing with you: how to build an environment for a new Ali cloud server — to deploy PHP projects, Node.js projects, etc.

Let’s get started:

1. Use Docker to make Centos server 2. 4. Install composer 5. Install Node.js 6. Install Git and SVN

Make Centos server with Docker

Because I do not own Ali cloud server, so I can not build the environment on the real server; But we can use Docker to simulate it.

First, you need a systemd integrated Centos image.

The contents of the Dockerfile are as follows:

FROM daocloud.io/centos:7

MAINTAINER "yemeishu" <[email protected]>
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f/lib/systemd/system/multi-user.target.wants/*; \ rm-f/etc/systemd/system/*.wants/*; \ rm-f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f/lib/systemd/system/basic.target.wants/*; \ rm-f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]Copy the code

Then you can start the container, but to avoid the “run Docker with systemctl error” problem, the solution is: Mount the /sys/fs/cgroup directory to the /sys/fs/cgroup directory of the container and configure the read-only permission. However, the docker run command can only use the -d parameter, not the it parameter.

docker run -d -v /sys/fs/cgroup:/sys/fs/cgroup:ro local/centos7-systemdCopy the code

Reference: github.com/docker-libr…

Run container:

When we run Vim and want to open and edit a file, we find that Vim is not installed, at this time we can directly install in the container, but in order to simulate more thoroughly, we can modify the Dockerfile file and make the image with Vim:

FROM daocloud.io/centos:7

MAINTAINER "yemeishu" <[email protected]>
ENV container docker
RUN yum -y install vim-enhanced

RUN yum -y install vim*

RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f/lib/systemd/system/multi-user.target.wants/*; \ rm-f/etc/systemd/system/*.wants/*; \ rm-f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f/lib/systemd/system/basic.target.wants/*; \ rm-f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]Copy the code

Make image, generate container, run container, verify whether VIM is installed successfully, directly look at the picture:

Installation tools

With “centos Server” in place, we are now configuring the PHP (Laravel/Lumen) environment.

Install Nginx

Before installing Nginx, we need to modify the image of the yum source.

Install wget first:

yum install wget -yCopy the code
# CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

# CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

# CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repoCopy the code

yum clean all
yum -y install epel-release
yum install nginx -y
systemctl start nginx
systemctl enableNginx // Starts automatically upon startupCopy the code

Installing PHP 7

Add the source:

rpm  -Uvh  https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm  -Uvh  https://mirror.webtatic.com/yum/el7/webtatic-release.rpmCopy the code

Installation:

yum  install  php70w -yCopy the code

Install the usual PHP plugins:

yum install php70w-json php70w-xml php70w-mbstring php70w-mysqlnd php70w-fpm -yCopy the code

Install the composer

Composer is a dependency management tool for PHP. It allows you to declare code libraries that your project depends on, and it will install them for you in your project. Composer is not a package manager. Yes, it involves “packages” and “libraries”, but it manages on a project-by-project basis, installing in a directory (such as Vendor) for your project. It doesn’t install anything globally by default. Therefore, this is simply a dependency management.

This idea is not new; Composer is strongly inspired by Node’s NPM and Ruby’s Bundler. There was no such thing in PHP at the time.

Composer will solve problems for you like this:

A) You have a project that depends on several libraries.

B) Some of these libraries depend on others.

C) You declare what you depend on.

D) Composer will figure out which versions of packages need to be installed and install them (download them into your project). Reference: docs.phpcomposer.com/00-intro.ht…

1. Run the PHP command to install the software

# Download the installation script - composer-setup.php - to the current directory.
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

Execute the installation process.
php composer-setup.php

Delete the installation script.
php -r "unlink('composer-setup.php');"

# Open the command line window and run the following command to move the previously downloaded composer. Phar file to the /usr/local/bin/directory
mv composer.phar /usr/local/bin/composerCopy the code

More view: getcomposer.org/download/

2. Use Packagist/Composer to accelerate Chinese full mirroring

Modifying the global configuration file of Composer (recommended)

composer config -g repo.packagist composer https://packagist.phpcomposer.comCopy the code

See more: pkg.phpcomposer.com/

3. Parallel composer install, let download if god helps

composer global require hirak/prestissimoCopy the code

4. Live updates

Tip: Don’t forget to perform Composer selfupdate often to keep composer up to date!

Installation Node. Js

yum -y install nodejs npm --enablerepo=epelCopy the code

Install domestic mirror:

npm install -g cnpm --registry=https://registry.npm.taobao.orgCopy the code

Install n to manage and select nodeJS versions // keep them consistent with existing server versions

cnpm install -g nCopy the code

Install the latest stable version:

n stableCopy the code

Or install the specified version, such as N 0.8.20

Install Git and SVN

yum -y install git
yum -y install subversionCopy the code

test

Now that we have the basic tools and environment in place, let’s try it out by installing a Lumen project:


Configure nginx. Create blog.conf in /etc/nginx/conf.d/

server {
    listen       80;
    server_name  blog.xxx.com;
    #index index.php index.html;

    location / {
        root /www/html/blog/public;
        try_files $uri $uri/ /index.php?$query_string;
        index index.php index.html index.htm;
    }

    location ~ \.php$ {
        #try_files $uri = 404;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME /www/html/blog/public$fastcgi_script_name; include fastcgi_params; Fastcgi_pass 127.0.0.1:9000; }}Copy the code

$curl = $curl $curl = $curl $curl = $curl

Lumen (5.5.0) (Laravel Components 5.5.*) indicates that our nginx and PHP-FPM are up and configured successfully.

conclusion

In this article, we continue to configure the environment by simply learning the Docker simulation Centos server.

If you have an Ali cloud server or a Tencent cloud server running on Centos 7, you should be able to go through the above steps and install the necessary tools to deploy regular PHP or Node.js projects. Now you can deploy your normal operations tools and protection tools.

Here are some questions and knowledge points:

1. Centos7 Docker container reported docker Failed to get D-bus connection error

This error is reported because Dbus-Daemon did not start. Systemctl is not unusable. Set CMD or entrypoint to /usr/sbin/init. The Docker container automatically starts services like DBUS. As follows:

docker run --privileged -ti --name test  docker.io/centos:7  /usr/sbin/initCopy the code

2. Error: XZ compression is not available

yum remove epel-release
# Sometimes that is not enough, you need to remove the cache as well by:

rm -rf /var/cache/yum/x86_64/6/epel
# Then you can install the epel-release again

yum -y install epel-release

# yum clean all is doing great for cleaning the cacheCopy the code

3. What is the relationship between FastCgi and phP-FPM?

Can refer to this post in detail: segmentfault.com/q/101000000…

“Finished”


Coding01 looks forward to your attention

qrcode


Thank you for seeing this

qrcode