微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

lnmp环境手动部署

目录

lnmp环境手动部署

环境说明

系统 IP 需要安装的服务
centos7 192.168.32.125 Nginx MysqL-5.7 PHP-7.4.9 PHP-MysqL

关闭防火墙和selinux,并配置好了yum源,包括epel源

1. 安装Nginx

#创建系统用户Nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin Nginx

#安装依赖包
[root@localhost ~]#  yum -y install pcre-devel openssl openssl-devel gd gd-devel gcc gcc-c++
......
[root@localhost ~]# yum -y groups mark install 'Development Tools'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Marked install: Development Tools


#创建日志存放目录
[root@localhost ~]#  mkdir -p /var/log/Nginx
[root@localhost ~]# chown -R Nginx.Nginx /var/log/Nginx

#下载Nginx
[root@localhost ~]# wget http://Nginx.org/download/Nginx-1.18.0.tar.gz

#编译安装
[root@localhost ~]# tar xf Nginx-1.18.0.tar.gz 
[root@localhost ~]# cd Nginx-1.18.0
[root@localhost Nginx-1.18.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost Nginx-1.18.0]# ./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@localhost Nginx-1.18.0]# make -j `nproc`
[root@localhost Nginx-1.18.0]# make install



#配置环境变量
[root@localhost Nginx-1.18.0]# echo 'export PATH=/usr/local/Nginx/sbin:$PATH' > /etc/profile.d/Nginx.sh
[root@localhost Nginx-1.18.0]# . /etc/profile.d/Nginx.sh
[root@localhost Nginx-1.18.0]# echo $PATH 
/usr/local/Nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin


#启动Nginx
[root@localhost Nginx-1.18.0]# Nginx 
[root@localhost Nginx-1.18.0]# ss -antl
State      Recv-Q Send-Q                 Local Address:Port                                Peer Address:Port              
LISTEN     0      100                        127.0.0.1:25                                             *:*                  
LISTEN     0      128                                *:80                                             *:*                  
LISTEN     0      128                                *:22                                             *:*                  
LISTEN     0      100                            [::1]:25                                          [::]:*                  
LISTEN     0      128                             [::]:22                                          [::]:*  

2. 安装MysqL

#安装依赖包
[root@localhost ~]# yum -y install libaio ncurses-devel openssl-devel openssl cmake mariadb-devel

#创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin MysqL

#下载二进制格式的MysqL软件包
[root@localhost ~]# wget https://dev.MysqL.com/get/Downloads/MysqL-5.7/MysqL-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# tar xf MysqL-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# mv MysqL-5.7.30-linux-glibc2.12-x86_64 /usr/local/MysqL

#修改目录/usr/local/MysqL的属主属组
[root@localhost ~]# chown -R MysqL.MysqL /usr/local/MysqL
[root@localhost ~]# ll /usr/local/MysqL -d
drwxr-xr-x 9 MysqL MysqL 129 Aug  6 10:04 /usr/local/MysqL

#添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/MysqL/bin:$PATH' > /etc/profile.d/MysqL.sh
[root@localhost ~]# . /etc/profile.d/MysqL.sh 
[root@localhost ~]# echo $PATH
/usr/local/MysqL/bin:/usr/local/Nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin


#建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R MysqL.MysqL /opt/data/

#初始化数据库
[root@localhost ~]# /usr/local/MysqL/bin/MysqLd --initialize-insecure --user=MysqL --datadir=/opt/data/
2020-08-06T14:18:46.668749Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-06T14:18:46.887930Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-06T14:18:46.923311Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-06T14:18:46.998617Z 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: bdd14729-d7ef-11ea-8207-000c29f66cbc.
2020-08-06T14:18:47.013567Z 0 [Warning] Gtid table is not ready to be used. Table 'MysqL.gtid_executed' cannot be opened.
2020-08-06T14:18:47.452070Z 0 [Warning] CA certificate ca.pem is self signed.
2020-08-06T14:18:47.906673Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

#配置MysqL
[root@localhost ~]# 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


#配置服务启动脚本
[root@localhost ~]# cp -a /usr/local/MysqL/support-files/MysqL.server /etc/init.d/MysqLd
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/MysqL#g' /etc/init.d/MysqLd
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/MysqLd
[root@localhost ~]# chkconfig --add MysqLd
[root@localhost ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

MysqLd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off



#启动MysqL
[root@localhost ~]# service MysqLd start
Starting MysqL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS! 
[root@localhost ~]# ss -tanl
State      Recv-Q Send-Q                  Local Address:Port                                 Peer Address:Port              
LISTEN     0      100                         127.0.0.1:25                                              *:*                  
LISTEN     0      128                                 *:80                                              *:*                  
LISTEN     0      128                                 *:22                                              *:*                  
LISTEN     0      100                             [::1]:25                                           [::]:*                  
LISTEN     0      80                               [::]:3306                                         [::]:*                  
LISTEN     0      128                              [::]:22                                           [::]:*   


#修改MysqL密码
[root@localhost ~]# MysqL
Welcome to the MysqL monitor.  Commands end with ; or \g.
Your MysqL connection id is 2
Server version: 5.7.30 MysqL Community Server (GPL)

copyright (c) 2000, 2020, 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('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

MysqL> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MysqL> exit
Bye





3. 安装PHP

#配置PHP的源
[root@localhost ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@localhost ~]# yum -y localinstall remi-release-7.rpm 
[root@localhost ~]# yum makecache --enablerepo=remi-PHP74

#安装依赖包
[root@localhost ~]# 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 sqlite-devel oniguruma-devel PHP74-PHP-MysqLnd
[root@localhost ~]# yum -y install https://mirrors.aliyun.com/centos/8.2.2004/AppStream/x86_64/os/Packages/libzip-1.5.2-1.module_el8.2.0%2B314%2B53b99e08.x86_64.rpm
[root@localhost ~]# yum -y install https://mirrors.aliyun.com/centos/8.2.2004/AppStream/x86_64/os/Packages/libzip-devel-1.5.2-1.module_el8.2.0%2B314%2B53b99e08.x86_64.rpm


#下载PHP,安装
[root@localhost ~]# wget https://www.PHP.net/distributions/PHP-7.4.9.tar.gz
[root@localhost ~]# tar xf PHP-7.4.9.tar.gz 
[root@localhost ~]# cd PHP-7.4.9
[root@localhost PHP-7.4.9]# ./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 \
 --enable-gd \
 --with-jpeg \
 --with-zlib-dir \
 --with-freetype \
 --with-gettext \
 --enable-json \
 --enable-mbstring \
 --enable-pdo \
 --with-MysqLi=MysqLnd \
 --with-pdo-MysqL=MysqLnd \
 --with-readline \
 --enable-shmop \
 --enable-simplexml \
 --enable-sockets \
 --with-zip \
 --enable-MysqLnd-compression-support \
 --with-pear \
 --enable-pcntl \
 --enable-posix

[root@localhost PHP-7.4.9]# make -j `nproc` && make install
......

#安装后配置环境变量
[root@localhost PHP-7.4.9]# echo 'export PATH=/usr/local/PHP7/bin:$PATH' > /etc/profile.d/PHP7.sh
[root@localhost PHP-7.4.9]# source /etc/profile.d/PHP7.sh
[root@localhost PHP-7.4.9]# PHP -v
PHP 7.4.9 (cli) (built: Aug  6 2020 11:11:43) ( NTS )
copyright (c) The PHP Group
Zend Engine v3.4.0, copyright (c) Zend Technologies


#配置PHP-fpm
[root@localhost PHP-7.4.9]# cp PHP.ini-production /etc/PHP.ini
[root@localhost PHP-7.4.9]# cp sapi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm
[root@localhost PHP-7.4.9]# chmod +x /etc/rc.d/init.d/PHP-fpm

[root@localhost PHP-7.4.9]# cp /usr/local/PHP7/etc/PHP-fpm.conf.default /usr/local/PHP7/etc/PHP-fpm.conf
[root@localhost PHP-7.4.9]# 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@localhost ~]# 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 


#启动PHP-fpm,设置开机自启
[root@localhost PHP-7.4.9]# service PHP-fpm start
Starting PHP-fpm  done
[root@localhost PHP-7.4.9]# chkconfig --add PHP-fpm
[root@localhost PHP-7.4.9]# ss -tanl
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
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                                     [::]:*                  
LISTEN      0      128                          [::]:22                                       [::]:*   

[root@localhost PHP-7.4.9]# vim /usr/local/PHP7/etc/PHP-fpm.d/www.conf
#修改listen = 127.0.0.1:9000为listen = 0.0.0.0:9000
[root@localhost PHP-7.4.9]# service PHP-fpm restart
Gracefully shutting down PHP-fpm . done
Starting PHP-fpm  done
[root@localhost PHP-7.4.9]# ss -antl
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
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                                     [::]:*                  
LISTEN      0      128                          [::]:22                                       [::]:*    



4. 配置Nginx

#在server大括号内配置
[root@localhost ~]# 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  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

#生成测试页面
[root@localhost ~]# cat > /usr/local/Nginx/html/index.PHP <<EOF
<?PHP
   PHPinfo();
?>
EOF


#重载Nginx配置文件
[root@localhost ~]# Nginx -s reload
[root@localhost ~]# ss -tanl
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
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                                     [::]:*                  
LISTEN      0      128                          [::]:22                                       [::]:*        

5. 访问测试页面

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐