1、基於CentOS7源碼編譯安裝得lnmp
准備軟件:
nginx版本1.14.0
mysql版本5.7.20
php版本7.2.6
1.1 下載網絡yum源
[root@centos7 ~]# wget http://mirrors.aliyun.com/repo/Centos-7.repo -P /etc/yum.repos.d/ #安裝阿里的網絡源 [root@centos7 ~]# yum -y install epel-release [root@centos7 ~]# ls /etc/yum.repos.d/ bak Centos-7.repo CentOS-Media.repo epel.repo epel-testing.repo [root@centos7 ~]# yum clean all;yum makecache
2 源碼編譯安裝nginx
2.1 安裝依賴包:
[root@centos7 ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
2.2 創建nginx運行用戶
[root@centos7 ~]# useradd -M -s /sbin/nologin nginx [root@centos7 ~]# wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.zip [root@centos7 ~]# unzip pcre-8.42.zip -d /usr/local/src/
2.3 下載nginx源碼包並解壓
[root@centos7 ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz [root@centos7 ~]# tar zxf nginx-1.14.0.tar.gz -C /usr/local/src/ [root@centos7 ~]# cd /usr/local/src/nginx-1.14.0/ [root@centos7 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --user=nginx --group=nginx --with-pcre=/usr/local/src/pcre-8.42
注意:--with-pcre=/usr/local/src/pcre-8.42 #這個是可選項,如果yum安裝了依賴包這里也可以不用
2.4 編譯並安裝
[root@centos7 nginx-1.14.0]# make [root@centos7 nginx-1.14.0]# make install
2.5 修改配置文件
[root@centos7 nginx-1.14.0]# vim /usr/local/nginx/conf/nginx.conf user nginx nginx; #修改用戶和組(第一行) location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #修改路徑 include fastcgi_params; }
2.6 添加環境變量,優化nginx服務
[root@centos7 ~]# /usr/local/nginx/sbin/nginx -t #檢查nginx語法是否正確 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos7 ~]# /usr/local/nginx/sbin/nginx #安裝好的啟動路徑 [root@centos7 ~]# vim /etc/profile #添加環境變量 export PATH=$PATH:/usr/local/nginx/sbin [root@centos7 ~]# source /etc/profile [root@centos7 ~]# nginx [root@centos7 ~]# netstat -antup|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7417/nginx: master
[root@centos7 ~]# vim /etc/init.d/nginx #配置啟動腳本 #!/bin/bash # chkconfig: 2345 99 20 #description: nginx-server nginx=/usr/local/nginx/sbin/nginx case $1 in start) netstat -anptu | grep nginx if [ $? -eq 0 ] then echo "nginx service is already running" else echo "nginx Service started successfully " $nginx fi ;; stop) $nginx -s stop if [ $? -eq 0 ] then echo "nginx service closed successfully" else echo "nginx server stop fail,try again" fi ;; status) netstat -anlpt | grep nginx if [ $? -eq 0 ] then echo "nginx server is running" else echo "nginx service not started " fi ;; restart) $nginx -s reload if [ $? -eq 0 ] then echo "nginx service restart successfully " else echo "nginx server restart failed" fi ;; *) echo "please enter {start restart status stop}" ;; esac
加入啟動:
[root@centos7 ~]# chmod +x /etc/init.d/nginx [root@centos7 ~]# chkconfig --add nginx [root@centos7 ~]# chkconfig nginx on
配置nginx以守護進程方式啟動
[root@centos7 ~]# vim /lib/systemd/system/nginxd.service [Unit] Description=The Nginx HTTP Server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking EnvironmentFile=/usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop KillSignal=SIGCONT PrivateTmp=true [Install] WantedBy=multi-user.target [root@centos7 ~]# systemctl daemon-reload [root@centos7 ~]# systemctl restart nginxd.service [root@centos7 ~]# systemctl enabled nginxd.service
3、源碼安裝MySQL
3.1 卸載系統自帶的mariadb*
[root@centos7 ~]# yum -y remove mariadb* boost-*
3.2 安裝依賴包
[root@centos7 ~]# yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel
3.3 下載源碼包
[root@centos7 ~]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.20.tar.gz
3.4 解壓源碼包
[root@centos7 ~]# tar zxf mysql-boost-5.7.20.tar.gz -C /usr/local/src/
3.5 配置編譯並安裝
[root@centos7 ~]# cd /usr/local/src/mysql-5.7.20/ [root@centos7 mysql-5.7.20]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/mysql-5.7.20/boost/boost_1_59_0 -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLE_DTRACE=0 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_USER=mysql
編譯並安裝 [root@centos7 mysql-5.7.20]# make [root@centos7 mysql-5.7.20]# make install
3.6 創建數據庫用戶和數據目錄
[root@centos7 ~]# useradd -M -s /sbin/nologin -r mysql [root@centos7 ~]# mkdir -p /usr/local/mysql/data #創建數據存儲目錄 [root@centos7 ~]# chown -R mysql.mysql /usr/local/mysql/ #更改屬主數組為MySQL
3.7 配置my.cnf文件
[root@centos7 ~]# vim /etc/my.cnf #以下是簡單配置 [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data port=3306 socket=/usr/local/mysql/mysql.sock symbolic-links=0 character-set-server=utf8 pid-file=/usr/local/mysql/mysqld.pid log-error=/var/log/mysqld.log
3.8 配置MySQL啟動腳本
[root@centos7 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #復制啟動腳本到/etc/init.d [root@centos7 mysql-5.7.20]# ll /etc/init.d/mysqld #默認擁有執行權限 -rwxr-xr-x 1 root root 10576 Jun 7 19:27 /etc/init.d/mysqld [root@centos7 mysql]# chkconfig --add mysqld #添加到開機啟動項 [root@centos7 mysql]# chkconfig mysqld on #添加開機自啟動 [root@centos7 mysql]# vim /etc/init.d/mysqld #修改路徑 basedir=/usr/local/mysql datadir=/usr/local/mysql/data [root@centos7 mysql]# vim /etc/profile #配置環境變量 export PATH=$PATH:/usr/local/mysql/bin [root@centos7 mysql]# source /etc/profile #加載變量立即生效
配置MySQL啟動腳本 [root@centos7 system]# vim mysqld.service [Unit] Description=MySQL DBMS [Service] LimitNOFILE=10000 Type=simple User=mysql Group=mysql PIDFile=/usr/local/mysql/mysqld.pid ExecStart=/usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data ExecStop=/bin/kill -9 $MAINPID [Install] WantedBy=multi-user.target
[root@centos7 system]# chmod +x mysqld.service #添加執行權限 [root@centos7 system]# systemctl enable mysqld.service #設置開機啟動
3.9 安全初始化數據庫
[root@centos7_4 ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data #這樣初始化之后,數據庫是沒有密碼的
如果要想初始化之后分配臨時密碼,可以將--initialize-insecure 紅色部分去掉,初始化之后,可以分配到一個臨時密碼。
初如數據庫時有提示報錯: initialize specified but the data directory has files in it. Aborting 解決方法: vim /etc/my.cnf 查看文件,尋找datadir=... 查看到:datadir=/usr/local/mysql/data, 這個是data保存目錄。 進入/usr/local/mysql/data后,查看到確實有數據: 將/usr/local/mysql/data備份, cd /usr/local/mysql mv data data.bak 接着執行: # mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@centos7 ~]# /etc/init.d/mysqld start #啟動數據庫 Starting MySQL. SUCCESS! [root@centos7 ~]# mysql -uroot #登錄數據庫修改root用戶密碼 mysql> alter user 'root'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
4 源碼編譯安裝PHP
4.1 安裝依賴包
[root@centos7 ~]# yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-servers openldap-clients freetype-devel gmp-devel
4.2 下載PHP源碼包
[root@centos7 ~]# wget http://cn2.php.net/distributions/php-7.2.6.tar.gz
4.3 解壓壓縮包
[root@centos7 ~]# tar zxf php-7.2.6.tar.gz -C /usr/local/src/ [root@centos7 ~]# cd /usr/local/src/php-7.2.6/
4.4 生成配置文件
[root@centos7 php-7.2.6]# cp -frp /usr/lib64/libldap* /usr/lib/ [root@centos7 php-7.2.6]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli --with-pdo-mysql --with-mysql-sock=/usr/local/mysql/mysql.sock --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-curl --with-gd --with-gmp --with-zlib --with-xmlrpc --with-openssl --without-pear --with-snmp --with-gettext --with-libxml-dir=/usr --with-ldap --with-ldap-sasl --with-fpm-user=nginx --with-fpm-group=nginx --enable-xml --enable-fpm --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-sockets --enable-inline-optimization --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-pcntl --enable-zip --disable-fileinfo --disable-rpath --enable-libxml --enable-opcache --enable-mysqlnd 注意:--with-ldap --with-ldap-sasl #如果不添加這兩項,要是安裝zabbix監控時候,會有提示還得需要再次編譯,如果不安裝zabbix,也可以忽略
4.5 編譯並安裝
[root@centos7 php-7.2.6]# vim Makefile #在以EXTRA_LIBS開頭的一行結尾添加‘-llber’ EXTRA_LIBS = -lcrypt -lz -lresolv -lcrypt -lrt -lldap -lgmp -lpng -lz -ljpeg -lz -lrt -lm -ldl -lnsl -lpthread -lxml2 -lz -lm -ldl -lssl -lcrypto -lcurl -lxml2 -lz -lm -ldl -lssl -lcrypto -lfreetype -lxml2 -lz -lm -ldl -lnetsnmp -lssl -lssl -lcrypto -lm -lxml2 -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lssl -lcrypto -lcrypt -llber
[root@centos7 php-7.2.6]# make ;make install
4.6 配置php配置文件
[root@centos7 php-7.2.6]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf #移動php配置文件的位置,並修改名稱 [root@centos7 php-7.2.6]# cp /usr/local/src/php-7.2.6/php.ini-production /usr/local/php/etc/php.ini #復制php.ini文件
4.7 復制php啟動腳本到/etc/init.d/
[root@centos7 php-7.2.6]# cp /usr/local/src/php-7.2.6/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 添加執行權限,添加到啟動項並設置卡機啟動 [root@centos7 php-7.2.6]# chmod +x /etc/init.d/php-fpm [root@centos7 php-7.2.6]# chkconfig --add php-fpm [root@centos7 php-7.2.6]# chkconfig php-fpm on 啟動php-fpm [root@centos7 ~]# /etc/init.d/php-fpm start Starting php-fpm done
[root@centos7_4 ~]# vim /usr/local/nginx/conf/nginx.conf 43 location / { 44 root html; 45 index index.php index.html index.htm; 46 } [root@centos7_4 ~]# service nginx restart #重啟nginx服務 編寫php探測文件 [root@centos7_4 ~]# vim /usr/local/nginx/html/index.php <?php phpinfo(); ?> [root@centos7_4 ~]# netstat -antup|grep php-fpm tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 128974/php-fpm: mas
瀏覽器測試:
http://ip/index.php 正常會看到php的主頁面。