CentOS7.X安装LMMP环境Nginx+PHP+Mysql详解

阅读目录 一、编译环境的安装:GCC,make,C++编译器 二、类库的安装:PCRE库、Zlib库、openssl 三、Nginx的安装 四、PHP安装 五、Nginx服务器解析PHP文件之Nginx.conf简易配置 六、Mysql安装 七、设置nginx和php-fpm的开机自启动 八、常见问题解决办法 前言: 作为PHP开发者,我们常用的线上环境就是LNMP,合理的搭建也是必须掌握的技能,下面就利用源码的方式详细介绍下LNMP环境Nginx+PHP+Mysql的详细搭建步骤: 版本说明: Nginx:nginx-1.14.1(最新版本) PHP:php-7.2.12(最新版本) Mysql:mysql-5.7.24 回到顶部 一、编译环境的安装:GCC,make,C++编译器 复制代码 yum -y install gcc automake autoconf libtool make yum install gcc gcc-c++ 复制代码 回到顶部 二、类库的安装:PCRE库、Zlib库、openssl 1.选定源码下载目录 可以是任何目录,建议目录为/usr/local/src cd /usr/local/src 2.安装PCRE库,官方地址:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 复制代码 cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz tar -zxvf pcre-8.42.tar.gz cd pcre-8.42 ./configure make && make install 复制代码 3.安装Zlib库,官方地址:http://www.zlib.net/ 复制代码 cd /usr/local/src wget http://www.zlib.net/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make && make install 复制代码 4.安装openssl官方地址:https://www.openssl.org/source/ 复制代码 cd /usr/local/src wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz tar -zxvf openssl-1.1.1a.tar.gz cd openssl-1.1.1 ./configure make && make install 复制代码 回到顶部 三、Nginx的安装 官方地址:http://nginx.org/en/download.html 1.下载Nginx官方稳定包并解压: 复制代码 cd /usr/local/src wget http://nginx.org/download/nginx-1.14.1.tar.gz tar -zxvf nginx-1.14.1.tar.gz cd nginx-1.14.1 复制代码 2.添加nginx用户和用户组 复制代码 groupadd -r nginx useradd -r -g nginx nginx 复制代码 3.配置nginx安装参数 复制代码 ./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre=/usr/local/src/pcre-8.39 \ --with-zlib=/usr/local/src/zlib-1.2.11 \ --with-openssl=/usr/local/src/openssl-1.1.1 复制代码 注意:当前所在目录为/usr/local/src/nginx-1.14.1    --with-pcre=/usr/local/src/pcre-8.42 指的是pcre-8.42 的源码路径。    --with-zlib=/usr/local/src/zlib-1.2.11 指的是zlib-1.2.11 的源码路径。    --with-openssl=/usr/local/src/openssl-1.1.1 指的是openssl-1.1.1的源码路径。 4.编译安装 make && make install 5.启动 /usr/local/nginx/sbin/nginx 注意:若mkdir /var/tmp/nginx/client failed的提示,请手动创建: mkdir -p /var/tmp/nginx/client 打开浏览器访问此服务器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功! 回到顶部 四、PHP安装 官方地址:http://www.php.net/downloads.php 1.添加C、C++编译器 yum -y install gcc gcc-c++ glibc 2.添加PHP扩展依赖 复制代码 yum -y install libmcrypt-devel mhash-devel libxslt-devel \ libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \ zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \ ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \ krb5 krb5-devel libidn libidn-devel openssl openssl-devel \ --setopt=protected_multilib=false zlib 复制代码 3.下载PHP官方稳定包并解压: 复制代码 cd /usr/local/src wget http://php.net/distributions/php-7.2.12.tar.gz tar -zvxf php-7.2.12.tar.gz cd php-7.2.12 复制代码 4.添加nginx用户和用户组 复制代码 groupadd -r www useradd -r -g www www 复制代码 5.配置PHP安装参数 复制代码 ./configure --prefix=/usr/local/php --enable-fpm \ --enable-mbstring --enable-pdo --with-curl --disable-debug --disable-rpath \ --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \ --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \ --with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli \ --with-gd --with-jpeg-dir --with-freetype-dir --enable-calendar 复制代码 注意:不同PHP版本,配置参数有部分不同,仅供参考 6.编译安装 make && make install 7.为php-fpm提供配置文件 复制代码 cd /usr/local/php/etc/ cp php-fpm.conf.default php-fpm.conf cd /usr/local/php/etc/php-fpm.d/ cp www.conf.default www.conf 复制代码 8.启动 /usr/local/php/sbin/php-fpm 验证是否启动成功(如果此命令输出有几个php-fpm进程就说明启动成功了) ps aux | grep php-fpm 回到顶部 五、Nginx服务器解析PHP文件之Nginx.conf简易配置 1.编辑Nginx.conf vim /usr/local/nginx/nginx.conf 复制代码 #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; location / { root /var/local/www; index index.php index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/local/www; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /var/local/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 复制代码 注意: 红色标记部分为修改部分; 原文件目录存放默认位置为:/usr/local/nginx/html,现将文件存放目录修改为:/var/local/www; 2.重启Nginx服务器 /usr/loca/nginx/sbin/nginx -s reload 3.测试php文件 创建index.php文件 vi /var/local/www/index.php 4.输入内容,保存 复制代码 复制代码 5.浏览器访问http://服务器ip/index.php,看到如下信息即表示成功。 回到顶部 六、Mysql安装 官方地址:https://dev.mysql.com/downloads/mysql/ 1.下载源码包,并解压 复制代码 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 复制代码 2.添加Mysql用户和用户组 复制代码 groupadd mysql useradd -r -g mysql mysql 复制代码 3.复制 cp -r mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql 4.修改Mysql目录所属者 复制代码 cd /usr/local/mysql chown -R mysql:mysql ./ 复制代码 5.初始化,安装Mysql数据库,并生成临时密码(注意:版本不同可能格式不同) 复制代码 bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 复制代码 如图: 输入以上命令可能出现的问题:bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory 原因:缺少numactl软件包 解决:安装numactl软件包 yum -y install numactl 再次执行初始化命令,即可成功; 6.配置my.cnf(简易配置) vi /etc/my.cnf 复制代码 [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/tmp/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd character_set_server=utf8 init_connect='SET NAMES utf8 lower_case_table_names = 1 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/usr/local/mysql/data/mysqld.pid # # include all files from the config directory # !includedir /etc/my.cnf.d 复制代码 注意:红色为修改部分; 7.添加开机启动 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 8.修改 vim /etc/init.d/mysqld vim /etc/init.d/mysqld 在46行 修改代码: basedir=/usr/local/mysql datadir=/usr/local/mysql/data 9.启动mysql服务 service mysqld start 10.通过以下脚本可实现对nginx服务的管理 /etc/init.d/mysqld start /etc/init.d/mysqld stop /etc/init.d/mysqld restart /etc/init.d/mysqld reload /etc/init.d/mysqld status 11.将mysqld服务加入chkconfig管理列表: chkconfig --add /etc/init.d/mysqld 12.设置终端模式开机启动: chkconfig nginx on 13.执行完上述命令后,就可以使用service对nginx服务的管理 service nginx start service nginx stop service nginx restart service nginx reload service nginx status 14.根据生成的原始密码,登陆Mysql mysql -u root -p 输入以上登陆Mysql命令可能出现的问题:-bash: mysql: command not found 解决:添加软连接 ln -s /usr/local/mysql/bin/mysql /usr/bin 15.修改原密码为root,并刷新权限(注意:要在登陆Mysql情况下修改) 复制代码 alter user 'root'@'localhost' identified by 'root'; flush privileges; //刷新权限 复制代码 至此全部成功安装了😊 回到顶部 七、设置nginx和php-fpm的开机自启动 具体实现步骤,请点击查看:   1.设置nginx的开机自启动方法   2.设置php-fpm的开机自启动方法 回到顶部 八、常见问题解决办法 1.CentOS下如何完全卸载php文件 注意:当我们使用命令:yum remove php,是无法完全卸载php文件的,输入查看php版本命令:php -v,还是可以看到php版本。 正确做法如下:(强制删除全部php软件包) a.查看全部php软件包 rpm -qa|grep php php-cli-5.3.3-22.el6.x86_64 php-pdo-5.3.3-22.el6.x86_64 php-gd-5.3.3-22.el6.x86_64 php-fpm-5.3.3-22.el6.x86_64 php-common-5.3.3-22.el6.x86_64 php-5.3.3-22.el6.x86_64 php-xml-5.3.3-22.el6.x86_64 php-pear-1.9.4-4.el6.noarch b.以此卸载软件包(注意:卸载要先卸载没有依赖的) 复制代码 rpm -e php-fpm-5.3.3-22.el6.x86_64 rpm-e php-pdo-5.3.3-22.el6.x86_64 rpm -e php-pear-1.9.4-4.el6.noarch rpm-e php-cli-5.3.3-22.el6.x86_64 rpm -e php-5.3.3-22.el6.x86_64 rpm-e php-xml-5.3.3-22.el6.x86_64 rpm -e php-gd-5.3.3-22.el6.x86_64 rpm-e php-common-5.3.3-22.el6.x86_64 复制代码 再次输入查看php版本命令:php -v,版本信息已经没有提示,卸载成功。 2.CentOS 7.x更改Hostname a.查看当前的Hostname hostnamectl status b.设置新的Hostname hostnamectl set-hostname XXX //XXX为新设置的Hostname c.重启,运行a命令查看即修改成功 作者:申文哲 出处:http://www.cnblogs.com/wenzheshen/ 如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!❤ 如果希望更容易地发现我的新博客,记得在左下角点个“关注我”哦。❤(如有错误之处,还请指正!) 分类: CentOS,Linux 好文要顶 关注我 收藏该文 申文哲 关注 - 3 粉丝 - 12 +加关注 1 0 « 上一篇:CentOS7.X中设置nginx和php-fpm的开机自启动 posted @ 2018-11-23 19:05 申文哲 阅读(78) 评论(0) 编辑 收藏 https://www.cnblogs.com/wenzheshen/p/10003894.html
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信