Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Build LNMP environment by docker

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

First, prepare the mirror image

After installing docker, replace the mirror source (the installation of docker is very simple and you can use Baidu on your own)

Open the daemon.json under the / etc/docker directory

Add the following

{

"registry-mirrors": ["https:// 's own address .mirror.aliyuncs.com"]

}

The basis of docker work is the image, which can be thought of as an independent virtual machine composed of applications.

Restart the docker service

Service docker restart

Docker pull nginx:alpine

Docker pull php:7-fpm-alpine

Docker pull postgres:alpine

You can use docker image ls to view downloaded images at any time.

2. Start nginx

Start the container

Sudo docker run-- rm-d-p 80:80-- name nginx nginx:alpine

Enter the ip of your virtual machine in the browser address bar and you can see the default welcome interface of nginx.

Docker stop is followed by the name of the container or id to stop the container

For example, the name of my container is nginx.

Then enter the command

Docker stop nginx

You can stop it.

3. Start LNMP

Since LNMP consists of three containers, a single startup is too troublesome, so it is recommended to use docker-compose to manage and start them.

Install docker-compose

Docker-compose is a multiple service deployment tool for docker to easily start multiple containers at the same time.

You can easily install it using the following command

Sudo apt-get install-y python-pip & & sudo pip install docker-compose

Edit the configuration file for docker-compose

For every project you want to start with docker-compose, you should configure docker-compose.yml in the project's directory.

Edit docker-compose.yml

Version: "3"

Services:

Nginx:

Image: nginx:alpine

Ports:

80:80

Volumes:./web:/usr/share/nginx/html:ro./nginx.conf:/etc/nginx/conf.d/default.conf:ro

PHP:

Image: undefined01/php:7-fpm-alpine

Volumes:./web:/var/www/html:rw

Database:

Image: postgres:alpine

Environment:

POSTGRES_USER: "postgres"

POSTGRES_PASSWORD: "rootroot"

Volumes:./data:/var/lib/postgresql/data:rw

Edit the configuration file for nginx

In order for nginx to successfully transfer the request to php for processing, we need to change the configuration file of nginx

Edit nginx.conf

Server {

Listen 80

Server_name localhost

Location / {

Root / usr/share/nginx/html

Index index.php index.html index.htm

}

Error_page 500 502 503 504 / 50x.html

Location = / 50x.html {

Root / usr/share/nginx/html

}

Location ~ .php ${

Fastcgi_pass PHP:9000

Fastcgi_index index.php

Fastcgi_param SCRIPT_FILENAME / var/www/html/$fastcgi_script_name

Include fastcgi_params

}

}

Start the service using docker-compose

Sudo docker-compose up-d

Test the LNMP environment

By the way, the above commands all use root permissions, so it may not be convenient to modify the contents of them. You can use the following command to obtain editing permissions.

Sudo chmod-R 777. / data. / web

Test php

Edit web/index.php

If all goes well, you can see the relevant information about php on http://193.112.46.97/index.php.

Test PostgreSQL

Do you remember the password? As I mentioned, it is configured in docker-compose. Therefore, in the actual environment, we must pay attention to the access rights of the file.

As for the address of the PostgreSQL database, I also mentioned that it is the name of the container (Database in this case). Just think of it as a domain name.

Edit web/test.php

? php

$dbconn = pg_connect ('host=Database user=postgres password=rootroot')

Or die ('Could not connect:'. Pg_last_error ()

Pg_query ('CREATE TABLE IF NOT EXISTS test (tester INT)')

Pg_query ('INSERT INTO test VALUES (0)')

$res = pg_query ('SELECT * FROM test') or die (' Query failed:'. Pg_last_error ()

$num = pg_num_rows ($res)

Echo "You have visited this site $num times"

Pg_free_result ($res)

Pg_close ($dbconn)

? >

If all goes well, you can see a page counter on http://193.112.46.97/test.php. Keep refreshing it and try it.

Using docker to build LNMP environment is not so easy!

Stop the service with docker-compose

You can use the following command to stop the service, which automatically destroys the appropriate container:

Sudo docker-compose down

What? Destroy? Don't worry, your database has been saved in the data folder in the current directory. Migrate to any new host, just pack the directory and take it with you!

You don't believe me? You can restart the above example to see if the database has been saved.

……

I'm not born strong, I'm just born strong!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report