lnmp

本文详细介绍了如何配置LNMP环境,包括nginx、mysql和php的配置步骤,最后展示了生成测试界面的过程。

配置nginx

//创建系统用户nginx
[root@server ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@server ~]#  yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@server ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@server ~]# mkdir -p /var/log/nginx
[root@server ~]# chown -R nginx.nginx /var/log/nginx
//编译安装
[root@server ~]# tar xf nginx-1.14.2.tar.gz 
[root@server ~]# cd nginx-1.14.2
[root@server nginx-1.14.2]#  ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@server nginx-1.14.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@server nginx-1.14.2]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@server ~]# . /etc/profile.d/nginx.sh
[root@server ~]#  nginx
[root@server ~]#  ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128               *:111                           *:*                  
LISTEN      0      128               *:80                            *:*                  
LISTEN      0      128               *:57330                         *:*                  
LISTEN      0      128               *:22                            *:*                  
LISTEN      0      100       127.0.0.1:25                            *:*                  
LISTEN      0      128              :::111                          :::*                  
LISTEN      0      128              :::22                           :::*                  
LISTEN      0      100             ::1:25                           :::*                  
LISTEN      0      128              :::49181                        :::*                  
//修改配置文件
[root@server ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.php index.html index.htm;
        }

 location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.params;
        }

配置mysql

//安装依赖包和下载二进制mysql包
[root@server ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
[root@server ~]# groupadd -r -g 306 mysql
[root@server ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql
[root@server ~]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
//解压配置mysql
[root@server ~]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@server ~]# cd /usr/local/
[root@server local]# ls
apache  apr-util  etc    include  lib64    mysql-5.7.22-linux-glibc2.12-x86_64  share
apr     bin       games  lib      libexec  sbin                                 src
[root@server local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
[root@server local]# ls
apache  apr-util  etc    include  lib64    mysql                                sbin   src
apr     bin       games  lib      libexec  mysql-5.7.22-linux-glibc2.12-x86_64  share
[root@server local]# chown -R mysql.mysql /usr/local/mysql
[root@server local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 12月 19 10:59 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
[root@server local]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@server local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@server local]#  . /etc/profile.d/mysql.sh
[root@server local]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@server local]# mkdir /opt/data
[root@server local]#  chown -R mysql.mysql /opt/data/
[root@server local]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 12月 19 11:00 data
//初始化数据库
[root@server ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/2019-12-19T03:01:35.862161Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-12-19T03:01:36.673480Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-12-19T03:01:36.882420Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-12-19T03:01:37.307046Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: df361822-220b-11ea-ba15-000c296e029d.
2019-12-19T03:01:37.308394Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-12-19T03:01:37.309741Z 1 [Note] A temporary password is generated for root@localhost: qK(iyJrAB4SP
[root@server ~]# echo 'qK(iyJrAB4SP' > pass
[root@server ~]# cat pass 
qK(iyJrAB4SP
//配置mysql
[root@server ~]#  ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@server ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@server ~]# ldconfig  
[root@server ~]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF
//启动mysql
[root@server ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@server ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@server ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
[root@server ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/server.err'.
 SUCCESS! 
[root@server ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128               *:111                           *:*                  
LISTEN      0      128               *:57330                         *:*                  
LISTEN      0      128               *:22                            *:*                  
LISTEN      0      100       127.0.0.1:25                            *:*                  
LISTEN      0      128              :::111                          :::*                  
LISTEN      0      128              :::80                           :::*                  
LISTEN      0      128              :::22                           :::*                  
LISTEN      0      100             ::1:25                           :::*                  
LISTEN      0      128              :::49181                        :::*                  
LISTEN      0      80               :::3306                         :::* 
//修改mysql密码
[root@server ~]# cat pass 
qK(iyJrAB4SP
[root@server ~]# mysql -uroot -p'qK(iyJrAB4SP'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('wangqing123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

配置php

//配置php源
[root@server ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@server ~]# rpm -Uvh remi-release-7.rpm
[root@server ~]# yum makecache --enablerepo=remi
//安装依赖包
[root@server ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php72-php-mysqlnd
//下载php
[root@server ~]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
//编译安装php
[root@server ~]# tar xf php-7.2.8.tar.xz
[root@server ~]# cd php-7.2.8
[root@server php-7.2.8]# ./configure --prefix=/usr/local/php7  \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@server php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@server php-7.2.8]# make install
//安装后配置
[root@server ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@server ~]# source /etc/profile.d/php7.sh
[root@server php-7.2.8]# which php
/usr/local/php7/bin/php
[root@server php-7.2.8]# php -v
PHP 7.2.8 (cli) (built: Aug 16 2018 13:27:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
//配置php-fpm
[root@server php-7.2.8]# cp php.ini-production /etc/php.ini
[root@server php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@server php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@server php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@server php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:
[root@server ~]# vim /usr/local/php7/etc/php-fpm.conf
.....
.....
pm.max_children = 50    ;最多同时提供50个进程提供50个并发服务
pm.start_servers = 5    ;启动时启动5个进程
pm.min_spare_servers = 2    ;最小空闲进程数
pm.max_spare_servers = 8    ;最大空闲进程数

[root@server ~]# tail /usr/local/php7/etc/php-fpm.conf
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p argument)
;  - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8



//启动php-fpm
[root@server ~]# service php-fpm start
Starting php-fpm  done

//默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@server ~]# ss -antl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port
LISTEN     0      128                          *:22                                       *:*
LISTEN     0      100                  127.0.0.1:25                                       *:*
LISTEN     0      128                  127.0.0.1:9000                                     *:*
LISTEN     0      128                         :::80                                      :::*
LISTEN     0      128                         :::22                                      :::*
LISTEN     0      100                        ::1:25                                      :::*
LISTEN     0      80                          :::3306                                    :::*

[root@server ~]# ps -ef|grep php
root      81070      1  0 14:13 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    81071  81070  0 14:13 ?        00:00:00 php-fpm: pool www
nobody    81072  81070  0 14:13 ?        00:00:00 php-fpm: pool www
nobody    81073  81070  0 14:13 ?        00:00:00 php-fpm: pool www
nobody    81074  81070  0 14:13 ?        00:00:00 php-fpm: pool www
nobody    81075  81070  0 14:13 ?        00:00:00 php-fpm: pool www
root      81079  83354  0 14:15 pts/1    00:00:00 grep --color=auto php
root@server php]# /bin/systemctl start php-fpm.service
[root@server php]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128               *:111                           *:*                  
LISTEN      0      128               *:57330                         *:*                  
LISTEN      0      128               *:22                            *:*                  
LISTEN      0      100       127.0.0.1:25                            *:*                  
LISTEN      0      128       127.0.0.1:9000                          *:*                  
LISTEN      0      128              :::111                          :::*                  
LISTEN      0      128              :::80                           :::*                  
LISTEN      0      128              :::22                           :::*                  
LISTEN      0      100             ::1:25                           :::*                  
LISTEN      0      128              :::49181                        :::*                  
LISTEN      0      80               :::3306                         :::*

生成测试界面

[root@server ~]# vim /usr/local/nginx/html/index.php
[root@server ~]# cat /usr/local/nginx/html//index.php
<?
	phpinfo()
?>
//关闭防火墙
[root@server ~]# systemctl stop firewalld
[root@server ~]# setenforce 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值