· 7 years ago · Nov 15, 2018, 04:38 AM
1
2Exercise No: 10
3
4Date: 10/10/2018
5
6General Introduction & Linux OS Installation
7
8Aim:
9To introduce the fundamentals of Free/Open Source Software and to install Linux Operating System ÂFedora Ver. 24 from FOSS Lab Server.
10
11Description of the exercise:
12a) Theory session for three hours covering the following topics
13
14Introduction to FOSS
15
16FOSS Licenses
17
18Linux Distributions
19
20Packages – RPM based & Debian based – 2 or 3 samples chosen in tune with the syllabus
21
22Kernel – Kernel structure
23
24Reference:
25
26FOSS Book available for download at Âhttp://www.nrcfoss.auÂkbc.org.in/down/3/ (from internet) orunder 'Docs' in 'FOSS_BOOKS' of your FLS (from your LAN)
27
28b) Hands on session for Linux installation (Fedora) from FLS
29
30About Fedora ...
31
32Fedora is a fast, stable, and powerful operating system for everyday use built by a worldwide community of friends. It's completely free to use, study, and share. More details about Fedora is available under 'Docs' in 'Fedora_Document' of your FLS.
33
34A practical exercise for students to learn basic linux installation of a rpm based operating system from Foss Lab Server
35
36Requirements:
37Hardware:
38Minimum Requirements:
39-700 Mhz X86 Processor
40-384 MB of system memory (RAM)
41-40 GB of disk space
42-Graphics card capable of 1024*768 resolution
43-Sound Card
44
45Prerequisites
46
47Partition should already be done.
48
49PXE boot option enabled in BIOS setup
50
51Sufficient Free space available for installation of linux (Min 10 GB)
52
53Session supervised by a trained staff member
54
55
56Procedure with steps:
57
58Select Fedora by typing “fedora 24†near “boot :  “
59
60
61
62
63Choose a Language and then use TAB key to press OK and Press ENTER key.
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81Use the arrow keys on your keyboard to select an installation method. Here select the NFS directory and use TAB key to select the OK and press ENTER.
82
83
84
85Press OK
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109Enter the IP Address of the machine which is FLS in your LAN, in the place of “name.example.com†against NFS Server Name. The give the export directory as “/root/nfs/fed/fed†in the place of “/mirrors/r6â€.
110
111Then the installation proceed with the initialization of Anaconda the Fedora system installer. Just wait for Anaconda to initialize and give you the Fedora 12 graphical system installer.
112
113
114
115
116This is the start of Fedora 12 graphical installation setup . Click 'Next' button to proceed with the installation process
117
118
119
120
121Choose the language that you want to use during the installation process of Fedora 12
122
123
124
125
126
127
128
129
130
131
132On this installation window screen, select appropriate keyboard layout setting for your Fedora 24 system.
133
134
135
136Give the hostname for your Fedora 12 system which is to be used to identify your Fedora 12 system on the network.
137
138
139
140
141click 'Next' button to proceed
142To configure Fedora 12 time zone, just click on nearest city available on the world map on the box provided on this screen to set correct time zone for your Fedora 12 system, or just click on the drop down box and choose the correct time zone by selecting the nearest city available.
143
144
145Click 'Next' button to proceed
146
147Key in root password for your Fedora 12 system and click on 'Next' button to proceed
148Setup Fedora 12 partition. On this screen you need to choose the best partition option where to put Fedora 12 on your system hard disk.
149
150
151Installation requires partitioning of your hard drive. You can also choose to create your own custom layout.
152
153
154
155
156
157Click on the drop down menu and choose 'create custom layout'
158Create a root partition by clicking on the New Button and then create a swap partition.
159
160
161
162
163Select Standard Partition to create a partition
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188Mount point must be '/' for a root partition and file system type Ext 4 . Size defines the amount of hard disk space for the particular partition.
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224Similarly Create Swap partition also. Swap size can be double the size of RAM. Click next to continue
225
226Then the Fedora 12 prompt you with "Writing storage configuration to disk". What this box say is that the Fedora 12 system installation going to write partition and formatting your system hard disk as you configure on previous screen. and if you continue with write change to disk, all the data on
227the hard disk will be lost. Click on "Go back" button to reconfigure partition layout or if you really sure that you want to proceed to install Fedora 12 just click on "Write change to disk".
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242The screen show the Fedora 12 system installer creating filesystem on /dev/sda1 the first portion on the first hard disk on the system.
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269Then Fedora12 system installer display the screen where you want to place the Fedora 12 boot
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290loader. The default option usually place on the first system hard disk, you can specify or change to
291other location by click on "Change device" button.
292
293
294
295
296
297
298Leave default boot loader option as it is... or you can customize as you see fit. Click 'Next' button to proceed. Then Fedora 12 system installation wizard retrieve the installation repository from FLS server.
299
300
301
302To customize or adding more software packages that you want to install or include on your Fedora 12 installation, select customize now button and then click on left box window that show Desktop Environments, Applications, Development, Servers, Base System and Languages. Tick the check box on the right window to adding and include the selected package group software.
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317You can further customize the installation packages, by selecting optional packages in group package. To select all optional packages, click on right mouse button to open drop down menu option... the menu option include Select the package, Select all optional packages, DeÂselect packages and DeÂselect all optional packages. To install packages and all their optional packages. choose and click Select all optional packages as show on the screen shot below.
318
319Click on 'Next' button to proceed with the installation process
320
321After the package dependencies checking is done, the initial process of installation Fedora 12 operating system will start. The configuration process may take several minutes to complete, this is shown by the progress bar indication box.
322
323The actual process of installing software packages to your system's hard disk . The process may take time to complete based on your overall packages selection.
324
325
326
327Please wait for installation process to finish installing all packages.
328
329To finish up the installation process you need to click on the 'Reboot' button to reboot the system and proceed with the Fedora 12 basic system configuration.
330
331
332
333
334
335
336
337
338Screen shot below shows the Fedora 12 boot up screen–
339
340
341
342
343Here is the Fedora 12 First boot configuration welcome screen. This is First Boot Configuration screen kick in only on the first boot of Fedora 12 after the installation process completed. The Fedora 12 Setup Agent (First Boot Configuration) process, enable us to do some basic configuration of Fedora 12 system before we log in on the Fedora 12 system for the first time. The screen shot below just show first screen of Setup Agent First Boot Configuration process. click on the 'Forward' button to proceed with the Fedora 12 configuration process.
344
345This screen display the Fedora 12 License Information. License Information screen. No configuration or setting need to be done on this screen just click 'Forward' button to continue with the installation process.
346
347The Fedora 12 configuration continue with Creating User. This user account is non administrative user account (normal user account). You need to create new user account by filling the Username, Full Name, Password and Confirm Password on the text box provided on this screen.
348
349Next screen helps in the date and Time configuration. Then the Fedora 12 installation setup agent proceed with the Hardware profiler. Fedora 12 hardware profile detect your system hardware and make an profile out of it. This hardware profile contains all your system hardware information and information about the Linux Fedora 12 operating system installed. Please note that on this screen, Fedora uses molt as fedora 12 hardware profiler to enable you send your Fedora 12 system information profile anonymously.
350
351
352
353
354
355Click on 'Finish' button to proceed, please note that if you want to send your system hardware profile to the Fedora Project.. just click on the Send Profile radio button before you click on the 'Finish' button. Then just key in the user password and hit the Enter key or click on 'Log In' button to start log in to the system.
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383Basic Linx Commands
384
385Objective
386
387Getting started with Linux, learn basic commands and directory structure, execute file and directory operations
388
389
390File Commands
391
392Perform the following file related operations at the shell prompt.
393
394Create file named linux_distros.txt Add the names of the linux distributions you know.
395
396Display the content of the file linux_distros.txt.
397
398Copy the file linux_distros.txt to a new file list_of_linux_distributions.txt.
399
400Rename the file list_of_linux_distributions.txt to linux.txt.
401
402Delete the file linux.txt.
403
404$ cat > linux_distros.txt debian
405
406fedora ubuntu slackware centos OpenSuSE ^D
407
408$ cat linux_distros.txt debian
409
410fedora ubuntu slackware centos OpenSuSE
411
412$ cp linux_distros.txt list_of_linux_distributions.txt
413
414$ mv list_of_linux_distributions.txt linux.txt
415
416$ rm linux.txt
417
418
419
420Directory Commands
421
422Perform the following directory related operations at the shell prompt.
423
424Get a list of files in the current directory.
425
426Create a directory called foss.
427
428Delete the directory foss
429
430Create the following directory structure with a single command: one/two/three.
431
432Delete the directory one with all its sub directories.
433
434Print the path of current working directory.
435
436
437
438
439
440Hidden files
441
442Perform the following hidden files related operations at the shell prompt.
443
444Create a hidden file called .a.
445
446Create a hidden directory called .config.
447
448List all the files in the current directory including hidden files.
449
450$ touch .a
451
452$ mkdir .config
453
454$ ls -a
455
456. .. .a .config
457
458
459Multiple Files
460
461The directory pattern has files with names containing specific patterns. Perform the following operations on the files.
462
463Goto the directory pattern.
464
465Remove all files starting with a.
466
467Remove files ending with 1.txt.
468
469Remove all files start with doc and ending with .txt.
470
471Remove all files.
472
473$ cd pattern
474
475$ rm a*
476
477$ rm *1.txt
478
479$ rm doc*.txt
480
481$ rm *
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541Result:
542 Thus, the Fedora is installed and Linux OS is executed successfully.
543
544
545
546Exercise No.: 03
547
548Date: 18/07/2018
549
550XAMPP Installation
551
552
553
554Aim:
555 To install the XAMPP in Linux or Windows and to know about its usage.
556
557
558About XAMPP:
559
560XAMPP is a free and open source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB database, and interpreters for scripts written in the PHP and Perl programming languages. XAMPP stands for Cross-Platform (X), Apache (A), MariaDB (M), PHP (P) and Perl (P).
561It is a simple, lightweight Apache distribution that makes it extremely easy for developers to create a local web server for testing and deployment purposes. Everything needed to set up a web server – server application (Apache), database (MariaDB), and scripting language (PHP) – is included in an extractable file. XAMPP is also cross-platform, which means it works equally well on Linux, Mac and Windows. Since most actual web server deployments use the same components as XAMPP, it makes transitioning from a local test server to a live server extremely easy as well.
562
563Steps:
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674Result:
675 Thus, the XAMPP is installed and executed successfully.
676.
677
678Exercise No.: 05
679
680Date: 05/09/2018
681
682MYSQL INSTALLATION
683
684Aim:
685MySQL is the most popular Open Source Relational SQL database management system. MySQL is one of the best RDBMS being used for developing web-based software applications
686Description of Mysql:
687MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses. MySQL is developed, marketed, and supported by MySQL AB, which is a Swedish company. MySQL is becoming so popular because of many good reasons:
688MySQL is released under an open-source license. So you have nothing to pay to use it.
689MySQL is a very powerful program in its own right. It handles a large subset of the functionality of the most expensive and powerful database packages.
690MySQL uses a standard form of the well-known SQL data language.
691MySQL works on many operating systems and with many languages including PHP, PERL, C, C++, JAVA, etc.
692MySQL works very quickly and works well even with large data sets.
693MySQL is very friendly to PHP, the most appreciated language for web development.
694MySQL supports large databases, up to 50 million rows or more in a table. The default file size limit for a table is 4GB, but you can increase this (if your operating system can handle it) to a theoretical limit of 8 million terabytes (TB).
695MySQL is customizable. The open-source GPL license allows programmers to modify the MySQL software to fit their own specific environments.
696Steps for Installing
697[root@server root]# cd /root
698
699[root@server root]# mkdir /root/files /data /data/mysql [root@server root]# cd files
700[root@server root]# wget http://www.d9x.net/downloads/unix/mysql-4.0.12.tar.gz
701[root@server root]# tar xzvf mysql-4.0.12.tar.gz [root@server root]# cd mysql-4.0.12
702
703if your using FreeBSD use "adduser" instead of "useradd".
704
705
706[root@server root]# groupadd mysql [root@server root]# useradd -g mysql mysql
707
708[root@server root]# ./configure --prefix=/usr/local/mysql --datadir=/data/mysql
709
710DataDir is where your mysql databases will be stored including the database which contains all mysql user accounts,
711you can easily backup all databases/users by saving the contents of this folder. /data/mysql
712
713NOTE: If you see ERROR:No curses/termcap library found" find wherelibncurses.so.5 is installed on your system and add the following flag to your configure statement : --with-named-curses-libs=/usr/lib/libncurses.so.5 (or where ever it may be)
714
715if you get a "mod_auth_dbm" Error
716
717Copy the ndbm.h file from /usr/include to the /usr/include/db1 directory (or install db1-devel)
718if you get a "no acceptable C compiler found in $PATH" Error Install a c compiler, example: gcc
719
720if you get "libmysql.c:1349: warning: passing arg 5 of `gethostbyname_r' from incompatible pointer type" Error
721you need to install a c++ compiler, install: libstdc++-devel, gcc-c++ (same version as gcc)
722
723[root@server root]# make [root@server root]# make install
724
725[root@server root]# ./scripts/mysql_install_db
726
727[root@server root]# ln -s /usr/local/mysql/share/mysql/mysql.server /sbin/mysqld
728
729[root@server root]# chown -R mysql:mysql /data/mysql [root@server root]# chown -R mysql:mysql /usr/local/mysql
730
731
732Start, Stop & Restart MySQL
733
734----------------------------------------
735
736[root@server root]# mysqld start [root@server root]# mysqld stop [root@server root]# mysqld restart
737Export Database:
738
739[root@server root]# /usr/local/mysql/bin/mysqldump -u root -p database_name > database_name.sql
740
741Import Database:
742
743[root@server root]# /usr/local/mysql/bin/mysql -u root -p database_name < database_name.sql
744
745Set root password:
746
747[root@server root]# /usr/local/mysql/bin/mysqladmin -u root -p password NEW_PASS
748
749NOTE: When it prompts you to enter the password, just hit Enter if you don't see an error, the password was successfully changed.
750Uninstalling MySQL
751
752----------------------------------------
753
754[root@server root]# rm -rf /usr/local/mysql [root@server root]# rm -rf /data/mysql [root@server root]# rm -rf /sbin/mysqld
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790Result:
791 Thus, the MYSQL is installed and executed successfully.
792
793
794Exercise No.: 06
795
796Date: 05/09/2018
797
798MySQL Basic Commands
799
800Aim:
801 To write the basic SQL Commands such as Data Definition Language Commands like Create Database, Create Table.
802
803Program:
804
805mysql> USE mysql;
806mysql> UPDATE user SET Password=PASSWORD('new-password') WHERE user='root';
807mysql> FLUSH PRIVILEGES;
808
809
810Create a database: (Creates directory /var/lib/mysql/bedrock)
811[prompt]$ mysqladmin -h localhost -u root -ppassword create bedrock
812
813
814(or use SQL command: CREATE DATABASE bedrock;)
815Show all mysql databases: mysqlshow -u root -ppassword
816Add tables, data, etc:
817Connect to database and issue the following SQL commands:
818[prompt]$ mysql -h localhost -u root -ppassword
819...
820mysql> show databases; -- List all databases in MySQL.
821+----------+
822| Database |
823+----------+
824| bedrock |
825| mysql |
826| test |
827+----------+
828mysql> use bedrock; -- Specify database to connect to. Also refers to path: /var/lib/mysql/bedrock
829mysql> create table employee (Name char(20),Dept char(20),jobTitle char(20));
830mysql> DESCRIBE employee; -- View table just created. Same as "show columns from employee;"
831+----------+----------+------+-----+---------+-------+
832| Field | Type | Null | Key | Default | Extra |
833+----------+----------+------+-----+---------+-------+
834| Name | char(20) | YES | | NULL | |
835| Dept | char(20) | YES | | NULL | |
836| jobTitle | char(20) | YES | | NULL | |
837+----------+----------+------+-----+---------+-------+
8383 rows in set (0.03 sec)
839
840mysql> show tables;
841+-------------------+
842| Tables_in_bedrock |
843+-------------------+
844| employee |
845+-------------------+
846
847mysql> INSERT INTO employee VALUES ('Fred Flinstone','Quarry Worker','Rock Digger');
848mysql> INSERT INTO employee VALUES ('Wilma Flinstone','Finance','Analyst');
849mysql> INSERT into employee values ('Barney Rubble','Sales','Neighbor');
850mysql> INSERT INTO employee VALUES ('Betty Rubble','IT','Neighbor');
851
852
853Note: Data type used was CHAR. Other data types include:
854CHAR(M) : Fixed length string. Always stores M characters whether it is holding 2 or 20 characters. Where M can range 1 to 255 characters.
855VARCHAR(M) : Variable length. Stores only the string. If M is defined to be 200 but the string is 20 characters long, only 20 characters are stored. Slower than CHAR.
856INT : Ranging from -2147483648 to 2147483647 or unsigned 0 to 4294967295
857FLOAT(M,N) : FLOAT(4,2) - Four digits total of which 2 are after the decimal. i.e. 12.34 Values are rounded to fit format if they are too large.
858DATE, TEXT, BLOB, SET, ENUM
859
860Add a user. Use the MySQL SQL console to enter SQL commands. The command mysql with the correct login/password will connect you to the database. The admin tables are stored in the database "mysql".
861[prompt]$ mysql -h localhost -u root -ppassword
862Welcome to the MySQL monitor. Commands end with ; or \g.
863Your MySQL connection id is 1 to server version: 3.23.41
864
865Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
866
867mysql> USE mysql;
868mysql> SHOW TABLES;
869+-----------------+
870| Tables_in_mysql |
871+-----------------+
872| columns_priv |
873| db |
874| func |
875| host |
876| tables_priv |
877| user |
878+-----------------+
879mysql> INSERT INTO user (Host, User, Password, Select_priv)
880 VALUES ('', 'Dude1', password('supersecret'), 'Y');
881mysql> FLUSH PRIVILEGES; -- Required each time one makes a change to the GRANT table
882mysql> GRANT ALL PRIVILEGES ON bedrock.* TO Dude1;
883mysql> FLUSH PRIVILEGES; -- Required each time one makes a change to the GRANT table
884mysql> quit
885Note:
886There is NO space between the -p and the password! You can omit the password and you will be prompted for it.
887The SQL flush command is equivalent to issuing the command:
888[prompt]$ mysqladmin reload
889
890
891
892
893Test the database:
894mysql> SELECT * from employee;
895+-----------------+---------------+-------------+
896| Name | Dept | jobTitle |
897+-----------------+---------------+-------------+
898| Fred Flinstone | Quarry Worker | Rock Digger |
899| Wilma Flinstone | Finance | Analyst |
900| Barney Rubble | Sales | Neighbor |
901| Betty Rubble | IT | Neighbor |
902+-----------------+---------------+-------------+
9031 row in set (0.00 sec)
904
905mysql> SELECT name FROM employee WHERE dept='Sales';
906+---------------+
907| name |
908+---------------+
909| Barney Rubble |
910+---------------+
9111 row in set (0.00 sec)
912
913
914
915Quit from the SQL shell:
916[prompt]$ quit
917
918
919Shutting down the database:
920[prompt]$ mysqladmin -u root -ppassword shutdown - PREFERRED
921OR
922[prompt]$ /etc/rc.d/init.d/mysqld stop
923OR
924[prompt]$ service mysqld stop
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947Result:
948 Thus, the MYSQL basic command is executed and verified successfully.
949
950
951Exercise No.: 04
952
953Date: 18/08/2018
954
955APACHE INSTALLATION
956
957
958
959
960Introduction
961
962The Apache Web server is arguably the most popular Web server in use on the Internet today. Here are some of the reasons why Apache is so popular; you don't have to be running Windows to run Apache. It was developed on various Unix/Linux/BSD platforms, and then recently ported to Win32. Internet Information Server, a Web server made by Microsoft for the Windows NT platform, is made for use in the "Windows-only" world. While IIS has many features, it’s lack of portability limits it’s market share.
963
964Another reason for Apache's widespread acceptance is its overall stability. While you can slow down an Apache Web server, you can rarely, if ever, kill one. The Apache Web server service is near bulletproof. Lastly, it's relatively fast. I say "relatively" as it's relative to what you're doing with it. If you're hosting a pretty plain Web site with mostly static content, Apache is a fireball. If you throw tons of CGI scripts at it, while making database calls at the same time, you're going to slow it down. Though much of the slowdown will come from your scripts themselves, and not from Apache.
965
966Apache evolved out of the NCSA https project to its current status as one of the fastest, most efficient, and most functional web servers in existence today. Apache has been shown to be substantially faster, more stable, and more feature-full than many other web servers. Although certain commercial servers have claimed to surpass Apache's speed (it has not been demonstrated that any of these "benchmarks" are a good way of measuring WWW server speed at any rate). The developers of Apache feel that it is better to have a mostly-fast free server than an extremely fast server that costs thousands of dollars. Apache is run on sites that get millions of hits per day, and they have experienced no performance difficulties. Apache is run on over 10 million Internet servers (as of May 2002). It has been tested thoroughly by both developers and users. The Apache Group maintains rigorous standards before releasing new versions of their server, and their server runs without problems on over one half of all WWW servers available on the Internet. When bugs do show up, they release patches and new versions as soon as they are available.
967
968
969
970
971
972
973
974
975Installation Options
976
977I’ll cover three methods of installing Apache under Linux. The three methods are:
978
979·Binary installation
980
981·Using an RPM (Red Hat Package Manager) – recommended for people running Red Hat Linux
982
983·Building from source
984
985
986
987Apache Installation Guide
988
989We recommend using the RPM installation method. This is the easiest way to install Apache for people that are new to Linux.
990
991Download the Software
992
993You can download Apache from the Apache Software Foundation web site located at
994http://www.apache.org, in source and binary forms. While yourdownloading Apache, you may want to browse over the documentation.
995
996
997
998What you need
999
1000
1001To install Apache, you will need the following things:
1002
1003A computer running Linux
1004
1005Root access on this computer
1006
1007For binary and source installations, the tar and gunzip Unix utilities
1008
1009
1010
1011
1012
1013Binary Installation
1014
1015A binary is pre-configured, which means someone else has gone to the trouble of configuring and building the software for you. There are, however, a few things you should keep in mind:
1016
1017
10181. Binaries are compiled for a particular operating system. In other words, you must use a binary built specifically for FreeBSD on your FreeBSD machine and a Linux binary on your Linux machine. You need to be sure to grab the correct binary; if you don't see a binary for your particular operating system, you must choose a different method of installation.
1019
1020Apache binaries are usually a version or two behind the current source distribution. This means you don't reap the benefits of the latest bug fixes and feature enhancements.
1021
1022
1023Because binaries are pre-configured, you don't have much opportunity to alter the way the software works. If you're a newcomer, you may not care about this loss of flexibility. Fortunately most Apache binaries include a full source distribution, providing you with the best of both worlds -- play now, learn later.
1024
1025
1026Now let's install a binary. Point your browser at http://www.apache.org/dist/httpd/binaries/
1027
1028and download the binary for your operating system (in our case, Linux). You'll most likely be presented with a directory containing multiple versions of Apache in various compressed forms. For the purposes of this guide, I'll assume you've downloaded the gzip'd form of the latest 2.0.x Apache binary (currently that's httpd-2.0.35-i686-pc-linux-rh72.tar.gz). If there is a README associated with the file you're downloading, you may want to review it for any interesting installation tidbits or possible bugs.
1029If you can't find a binary for your operating system, choose either the RPM Installation (if you are running Red Hat Linux) or Build from Source method. Now let's uncompress the archive using the handy combination of
1030gunzip and tar. You should replace the "httpd-2.0.35-i686-pc-linux-rh72.tar.gz" text below with the name of the gzip'd file you downloaded.
1031
1032gunzip < httpd-2.0.35-i686-pc-linux-rh72.tar.gz | tar xvf –
1033
1034Some of you may be lucky enough to have a version of tar that is capable of taking care of both tasks.
1035tar xvzf httpd-2.0.35-i686-pc-linux-rh72.tar.gz
1036
1037
1038Either way, you should end up with an httpd-2.0.x directory, with x being the particular subversion of Apache you downloaded. Move into the newly created directory.
1039
1040
1041cd httpd-2.0.x
1042
1043
1044
1045As of Apache 1.3.11, binary distributions contain an install script called install.bindist.sh. If your binary does not seem to contain such an install script, take a look at the README.bindist and/or INSTALL.bindist documents for further information; if these documents don't outline a simple installation method, you'll probably want to use one of the other methods I cover in this guide.
1046If you've not already done so, you'll need to become root. You can become root by typing su, then the root password. Then go ahead and run the install script.
1047
1048
1049./install.bindist.sh
1050
1051This command should install the various bits of the Apache distribution into the appropriate locations; the default is usually to install everything under /usr/local/apache (confirm this by consulting your README.bindist and/or INSTALL.bindist documents). That's all there is to it on installing binaries.
1052
1053RPM Installation
1054
1055Those of you running Red Hat Linux may want to take advantage of Red Hat's RPM ("RedHat Package Manager") system. Almost identical to a binary, an RPM is further customized to play nicely with other RPMs and provide a consistent interface to installing, updating, and removing binaries. For Linux newcomers or when installing a small standard component, RPMs are simple and reliable. Bear in mind that an Apache RPM may already be installed on your system depending on how Linux was originally installed on your computer. To find out, at the shell prompt, type:
1056
1057rpm -qa | grep apache
1058
1059
1060
1061If you see something like apache-1.3.9xxx, an Apache RPM has already been installed. You can also type that command typing httpd instead of apache to see if it’s installed. If you don't have an Apache RPM, you must obtain one. Red Hat 7.3 ships apache-1.3.23- 11.src.rpm in the RedHat/RPMS directory on the installation CD. Or, point your browser at ftp://ftp.redhat.com/pub/redhat/redhat-7.3-en/os/i386/RedHat/RPMS and download it. If you've not already done so, you'll need to become root. Navigate to the same directory as the .rpm file you obtained, and then type the following command, substituting the name of the .rpm you're using for example: apache-1.3.23-11.src.rpm.
1062rpm -ivh apache-1.3.23-11.src.rpm
1063
1064RPM should grind away, displaying its progress with a primitive #######
1065
1066progress bar. Barring any errors, you're done.
1067
1068
1069
1070Build from Source
1071
1072Building Apache from source may seem like a daunting task to newcomers, but the Apache developers have done a wonderful job of making the task about as simple as could be. Just three more commands than a binary installation and you skip the arduous task of figuring out which binary is the right one for your particular operating system. Point your browser at http://www.apache.org/dist/httpd/ and download the gzip'd form of the current version of Apache (2.0.36 at the time of this writing). Now let's uncompress that archive using gunzip and tar. You should replace the httpd-2.0.36.tar.gz below with the name of the gzip'd file you downloaded.
1073
1074gunzip < httpd-2.0.36.tar.gz | tar xvf –
1075
1076You should end up with an httpd-2.0.x directory, x being the particular sub-version of Apache you downloaded. Move into the newly created directory.
1077
1078cd httpd-2.0.x
1079
1080Now we'll use the the configure and make commands to configure, make, and install Apache. If you've not already done so, now would be the time to become root.
1081./configure
1082
1083Your screen should look something like:
1084
1085
1086
1087# ./configure
1088
1089checking for chosen layout... Apache checking for working mkdir -p... yes
1090checking build system type... i686-pc-linux-gnu checking host system type... i686-pc-linux-gnu checking target system type... i686-pc-linux-gnu Configuring Apache Portable Runtime library ...
1091
1092...
1093
1094config.status: executing default commands
1095
1096Unless errors were reported (not warnings), your Apache installation is now configured and we can move on. This is where things get a bit ugly. Make'ing Apache produces screenfulls of output.
1097
1098Make
1099
1100Your screen should look something like:
1101# make
1102
1103Making all in srclib
1104
1105make[1]: Entering directory `/home/ryan/dl/apache_guide/httpd-2.0.36/srclib'
1106Making all in apr
1107
1108make[2]: Entering directory `/home/ryan/dl/apache_guide/httpd-2.0.36/srclib/apr'
1109...
1110make[1]: Leaving directory `/home/ryan/dl/apache_guide/httpd-2.0.36'#
1111
1112
1113
1114
1115Finally, you're ready to install your Apache build.
1116
1117
1118# make install
1119
1120Now Apache is installed.
1121
1122
1123Starting Apache
1124
1125Let's take your new Apache installation out for a spin.
1126
1127If you installed Apache using a binary or from scratch, as root, type:
1128
1129
1130
1131/usr/local/apache/bin/apachectl start
1132
1133
1134
1135If you used an RPM, as root, type:
1136
1137
1138
1139/sbin/service httpd start
1140
1141
1142
1143Point your browser at your brand new Web server, http://localhost/. If everything worked you should see the default home page.
1144
1145Customize
1146
1147Apache uses some rather easy to understand text files for configuration. On a Red Hat system,you'll find them in /etc/httpd/conf. Quite a few Linux distributions place them in this same place, but if you can't find such a directory, do a search for "httpd.conf". Once you find these, you've found the main config files. If you're new to Linux, and need help finding this file, here's how you can find it.
11481. Login as root 2.Type: cd /
11493. Type: find -name httpd.conf
1150
1151Now you should see where the file is located. When you move into the directory containing
1152httpd.conf, you should see these three files:
1153
1154·httpd.conf– This has the settings for the overall configuration for theserver.
1155·access.conf– This file contains all the security settings for Apache. ·srm.conf– This file contains the MIME definitions and default document names for files on the server.
1156Restarting Apache
1157
1158Whenever you make changes to the server configuration files, such as httpd.conf, they won't take effect until the server is restarted. In Linux, Apache can be restarted depending on how you installed it. If you installed Apache using a binary or from scratch, as root, type:
1159
1160/usr/local/apache/bin/apachectl start
1161
1162
1163If you used an RPM, as root, type:
1164
1165/sbin/service httpd start
1166
1167After being restarted the changes will have taken effect.
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254Result:
1255Thus, the APACHE INSTALLATION is installed and executed successfully.
1256
1257
1258
1259
1260
1261Exercise No.: 07
1262
1263Date: 12/09/2018
1264
1265PHP DESIGN PAGE
1266
1267Aim:
1268To create a login form and test the connectivity with MYSQL
1269
1270Introduction:
1271PHP stands for PHP: Hypertext Preprocessor. PHP is a general-purpose scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the webpage document. It also has evolved to include a command-line interface capability and can be used in standalone graphical applications.
1272PHP was originally created by Rasmus Lerdorfin 1995. The main implementation of PHP is now produced by The PHP Group and serves as the de facto standard for PHP as there is no formal specification. PHP is free software released under the PHP License which is incompatible with the GNU General Public License (GPL) due to restrictions on the usage of the term PHP.
1273PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
1274Uses of PHP:
1275a) PHP runs on different platforms (Windows, Linux, Unix, etc.)
1276b) PHP is compatible with almost all servers used today (Apache, IIS, etc.)
1277c) PHP is FREE to download from the official PHP resource: www.php.net
1278PHP is easy to learn and runs efficiently on the server side
1279Steps to execute php prgrams :
1280Open the vi editor and write the program
1281Save it using .php extension
1282Execute using the command php filename.php
1283 Steps to execute php programs involving html forms :
1284Open the vi editor and write the program
1285Save it using .php extension with the html forms using .html extension in the directory /var/www/html
1286Open the browser and type the URL as : http://localhost/formname.html
1287
1288[fosslab@fosslab ~]$ su -
1289Password: (admin123)
1290[root@fosslab ~]# cd /var/www/html/
1291[root@ fosslab html]# gedit form1.html (or) vi form1.html
1292
1293
1294Program 1:
1295
1296Program:form1.html
1297<html>
1298<body>
1299<form action="welcome.php" method="post">
1300Name: <input type="text" name="fname" />
1301Age: <input type="text" name="age" />
1302<input type="submit" />
1303</form>
1304</body>
1305</html>
1306
1307
1308Welcome.php
1309<html>
1310<body>
1311Welcome <?php echo $_POST["fname"]; ?>!<br />
1312You are <?php echo $_POST["age"]; ?> years old.
1313</body>
1314</html>
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338Output:
1339
1340OPEN THE BROWSER AND TYPE THE FOLLOWING PATH
1341http://localhost/form1.html
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359Program 2:form2.html
1360<html>
1361<head>
1362<title>LOGIN</title></head>
1363<body>
1364<form action="connect.php" method="post">
1365<p>"Enter course no"<input type="text" name="cid"></p>
1366<p>"enter the coursename"<input type="text" name="cname"></p>
1367<p>"click here to submit"<input type="submit" name="submit"></p>
1368</form>
1369</body>
1370
1371Connect.php:
1372<?php
1373$cid=$_POST['cid'];
1374$cname=$_POST['cname'];
1375$con=mysqli_connect("localhost","root","","student");
1376/ Check connection
1377if (mysqli_connect_errno())
1378{
1379 echo "Failed to connect to MySQL: " . mysqli_connect_error();
1380}
1381echo "connected to database";
1382$str="insert into courses1 values($cid,'$cname')";
1383$res=mysqli_query($con,$str);
1384if($res>0)
1385{
1386echo "Record created";
1387}
1388mysqli_close($con);
1389?>
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409Output:
1410TYPE THE FOLLOWING IN THE BROWSER
1411http://localhost/form2.htmlWhen u press the submit button
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477Result:
1478Thus, the web page is designed and executed successfully.
1479
1480
1481
1482
1483
1484
1485Exercise No.: 09
1486
1487Date: 29/09/2018
1488
1489PROCEDURE FOR PHP – MySQL CONNECTIVITY
1490
1491
1492Aim:
1493To create a login form and test the connectivity with MYSQL using PHP
1494
1495Program 1:
1496
1497Step 1:Open the Terminal
1498
1499Go to terminal:
1500[fosslab@fosslab ~]$ su -
1501Password: (admin123)
1502[root@fosslab ~]# mysql
1503Welcome to the MySQL monitor. Commands end with ; or \g.
1504Your MySQL connection id is 7
1505Server version: 5.1.51 Source distribution
1506
1507Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
1508This software comes with ABSOLUTELY NO WARRANTY. This is free software,
1509and you are welcome to modify and redistribute it under the GPL v2 license
1510Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
1511
1512Step 2:Show the databases
1513mysql> show databases;
1514+--------------------+
1515| Database |
1516+--------------------+
1517| information_schema |
1518| TESTDB |
1519| college |
1520| employee |
1521| mysql |
1522| sample |
1523| steel |
1524| student |
1525| student1 |
1526| test |
1527+--------------------+
152810 rows in set (0.00 sec)
1529
1530mysql> use student;
1531Reading table information for completion of table and column names
1532You can turn off this feature to get a quicker startup with -A
1533Database changed
1534
1535
1536Step 3:Show the Tables
1537mysql> show tables;
1538+-------------------+
1539| Tables_in_student |
1540+-------------------+
1541| EMPLOYEE |
1542| courses |
1543| courses1 |
1544| veni |
1545| veni1 |
1546+-------------------+
15475 rows in set (0.00 sec)
1548
1549
1550
1551
1552Program 2:
1553Create Login Page (HomePage.html):
1554<table width="300" border="0" align="center" cellpadding="0"
1555cellspacing="1" bgcolor="#CCCCCC">
1556<tr>
1557<form name="form1" method="post" action="checklogin.php">
1558<td>
1559<table width="100%" border="0" cellpadding="3" cellspacing="1"
1560bgcolor="#FFFFFF">
1561<tr>
1562<td colspan="3"><strong>Member Login </strong></td>
1563</tr>
1564<tr>
1565<td width="78">Username</td>
1566<td width="6">:</td>
1567<td width="294"><input name="myusername" type="text"
1568id="myusername"></td>
1569</tr>
1570<tr>
1571<td>Password</td>
1572<td>:</td>
1573<td><input name="mypassword" type="text" id="mypassword"></td>
1574</tr>
1575<tr>
1576<td> </td>
1577<td> </td>
1578<td><input type="submit" name="Submit" value="Login"></td>
1579</tr>
1580</table>
1581</td>
1582</form>
1583</tr>
1584</table>
1585
1586Create Validation Page (checklogin.php):
1587<?php
1588$host="localhost"; // Host name
1589$username="root"; // Mysql default username
1590$password=""; // Mysql No password
1591$db_name="student"; // Database name
1592$tbl_name="members"; // Table name
1593// Connect to server and select databse.
1594$con = mysqli_connect("$host", "$username","","$db_name");
1595// Check connection
1596if (mysqli_connect_errno())
1597{
1598 echo "Failed to connect to MySQL: " . mysqli_connect_error();
1599}
1600// username and password sent from form
1601$myusername=$_POST['myusername'];
1602$mypassword=$_POST['mypassword'];
1603$sql="SELECT * FROM $tbl_name WHERE
1604username='$myusername' and password='$mypassword'";
1605$result=mysqli_query($con,$sql);
1606// Mysql_num_row is counting table row
1607$count=mysqli_num_rows($result);
1608// If result matched $myusername and $mypassword, table row must be 1 row
1609if($count==1)
1610echo "Welcome To Our Web Page";
1611else
1612echo "Wrong Username or Password";
1613?>
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636Output:
1637TYPE THE FOLLOWING IN THE BROWSER
1638http://localhost/HomePage.html
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659Program 3:
1660PHP program for create database
1661<?php
1662$con = mysqli_connect("localhost","root","",â€studentâ€);
1663if (mysqli_connect_errno()))
1664 { die('Could not connect: ' . mysqli_connect_errno())); }
1665else
1666 { echo "ok data base"; }
1667mysqli_close($con);
1668?>
1669
1670Output:
1671[root@fosslab html]# php connectdb.php
1672ok data base
1673
1674
1675
1676
1677
1678
1679
1680Program 4:
1681PHP program to create a table in the database
1682[root@fosslab html]# vi cretetable.php
1683<?php
1684$con = mysqli_connect("localhost","root","","student");
1685// Check connection
1686if (mysqli_connect_errno())
1687 {
1688 echo "Failed to connect to MySQL: " . mysqli_connect_error();
1689 }
1690$sql = "CREATE TABLE Per
1691(
1692FirstName varchar(15),
1693LastName varchar(15),
1694Age int
1695)";
1696mysqli_query($con,$sql);
1697echo "table created";
1698// Execute query
1699mysql_close($con);
1700?>
1701
1702
1703Output:
1704[root@fosslab html]# php cretetable.php
1705ok data base table create
1706
1707
1708
1709Open the another terminal
1710Type the following commands on the another terminal
1711
1712mysql> show databases;
1713+--------------------+
1714| Database |
1715+--------------------+
1716| information_schema |
1717| TESTDB |
1718| college |
1719| employee |
1720| mysql |
1721| sample |
1722| steel |
1723| student |
1724| student1 |
1725| test |
1726+--------------------+
172710 rows in set (0.00 sec)
1728
1729mysql> use student;
1730Reading table information for completion of table and column names
1731You can turn off this feature to get a quicker startup with -A
1732
1733Database changed
1734mysql> show tables;
1735+-------------------+
1736| Tables_in_student |
1737+-------------------+
1738| EMPLOYEE |
1739| Per |
1740| courses |
1741| courses1 |
1742| veni |
1743| veni1 |
1744+-------------------+
17456 rows in set (0.00 sec)
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796Result:
1797 Thus, the Php program is executed and output is verified successfully.
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808Exercise No.: 01
1809
1810Date: 11/07/2018
1811
1812PYTHON INSTALLATION
1813
1814Aim:
1815 To demonstrate the Python programs and Python program connectivity with MYSQL database.
1816Introduction
1817Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.
1818Pythonis a high-level programming language. It is an easy to learn, powerful programming language. It has efficient, high-level data structures and a simple yet effective approach to object oriented programming.
1819Python’s elegant syntax and dynamic typing, together with its interpreted nature, makes it an ideal language for scripting and rapid application development in many areas and on most platforms. The Creator of this Language is Guido van Rossum. He named this language after the BBC show “Monty Python‘s Flying Circusâ€. But he doesn’t particularly like snakes that kill animals for food by winding their long bodies around them and crushing them.
1820Python runs on Windows, Linux/Unix, Mac OS X, and has been ported to the Java and .NET virtual machines.
1821Python is free to use, even for commercial products, because of its OSI-approved open source license.
1822Downloading
1823Click Python Download.
1824The following page will appear in your browser.
1825
1826Click the Download Python 3.6.2 button.
1827Move this file to a more permanent location, so that you can install Python (and reinstall it easily later, if necessary).
1828Feel free to explore this webpage further; if you want to just continue the installation, you can terminate the tab browsing this webpage.
1829Start the Installing instructions directly below.
1830
1831Installing
1832Double-click the icon labeling the file python-3.6.2.exe.
1833An Open File - Security Warning pop-up window will appear.
1834
1835Click Run.
1836A Python 3.6.2 (32-bit) Setup pop-up window will appear.
1837
1838Ensure that the Install launcher for all users (recommended) and the Add Python 3.6 to PATH checkboxes at the bottom are checked.
1839Highlight the Install Now (or Upgrade Now) message, and then click it.
1840
1841Click the Yes button.
1842A new Python 3.6.2 (32-bit) Setup pop-up window will appear with a Setup Progress message and a progress bar.
1843
1844During installation, it will show the various components it is installing and move the progress bar towards completion. Soon, a new Python 3.6.2 (32-bit) Setup pop-up window will appear with a Setup was successful message.
1845
1846Click the Close button.
1847Python should now be installed.
1848
1849Steps to execute python programs:
1850Open the vi editor and write the program
1851Save the program with .py as extension
1852Execute the program using python filename.py
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897Result:
1898 Thus, the Python is installed and executed successfully.
1899
1900
1901
1902
1903Exercise No.: 02
1904
1905Date: 11/07/2018
1906
1907PYTHON PROGRAMS
1908
1909Aim:
1910 To demonstrate the Python programs using various operations.
1911
1912Program 1:
1913#!/usr/bin/python
1914#program to select odd number from the list
1915a=[11,12,13,14,15,16,17,18,19,20,21,31,44,45,10];
1916print("List is:",a);
1917n=len(a);
1918print("length:",n);
1919i=0;
1920print("Odd number");
1921for i in range(len(a)):
1922 if(a[i]%2==1):
1923 print(a[i]);
1924
1925
1926
1927Output:
1928[root@fosslab html]# python odd.py
1929('List is:', [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 31, 44, 45, 10])
1930('length:', 15)
1931Odd number
193211 13 15 17 19 21 31 45
1933
1934
1935
1936
1937
1938
1939
1940Program 2: Program to display the celsius value\
1941#!/usr/bin/python
1942a=float(input("enter the celsius value:"))
1943f=(a*1.8)
1944b=f+32;
1945print (b)
1946
1947Output:
1948[fosslab@fosslab ~]$ python celsius.py
1949enter the celsius value:23
195073.4
1951
1952Program 3: Program to display the fibonacciseries values
1953#!/usr/bin/python
1954a, b = 0, 1
1955while (b < 200):
1956 print (b)
1957 a, b = b, a+b
1958
1959Output:
1960[fosslab@fosslab ~]$ python fibanoo.py
19611 1 2 3 5 8 13 21 34 55 89 144
1962
1963
1964
1965Program 4: Program to display odd or even numbers.
1966#!/usr/bin/python
1967a = int(input("Please enter an integer: "));
1968if (a % 2 == 1):
1969 print( 'a' + ' is odd.')
1970else:
1971 print( 'a' + ' is even.')
1972
1973Output:
1974[fosslab@fosslab ~]$ python odd.py
1975Please enter an integer: 23
1976a is odd.
1977
1978
1979
1980
1981Program 5: String Concatenation in python programming
1982#String concatenation
1983worda='computer';
1984wordb='science';
1985print("worda is ",worda);
1986print("wordb is",wordb);
1987wordc=worda+" " +wordb;
1988print("wordc is",wordc);
1989wordd=worda*3;
1990print("wordd is ",wordd);
1991str = 'HelloWorld!'
1992length=len(str);
1993print ("str :",str);
1994print("length:",length);
1995print ("first character is",str[0]);
1996print ("print character from 2rd to 6th :", str[2:7] );
1997print ("Prints string starting from 3rd character:",str[2:]);
1998print ("Prints string two times",str * 2);
1999print ("Prints concatenated string :",str + "TEST" );
2000print(str[-1]); #print last character
2001print(str[-6]);#print character from last 6th position
2002print(str[:-2]);# Everything except the last two characters
2003
2004
2005
2006OUTPUT:
2007(‘word a is’,’computer’)
2008(‘word b is’,’science’)
2009(‘word cis’,’computerscience’)
2010(‘word dis’,’computer computer computer’)
2011(‘str:’,’HelloWord!’)
2012(‘length:’,11)
2013(‘first character is’,’H’)
2014(‘Print character from 2rd to 6th :’,’llowo’)
2015(‘Prints string starting from 3rd character:’,’lloWord!’)
2016(‘Prints string two times’,’HelloWorld! HelloWorld!’)
2017(‘Prints concatenated string :’,’HelloWorld! TEST’ )
2018!
2019W
2020HelloWord
2021>>>
2022
2023
2024
2025Program 6: Write a python program to perform function in Lists
2026#Python Lists
2027#!/usr/bin/python
2028print("\t \t \t Python Lists");
2029list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
2030tinylist = [123, 'john']
2031print("Prints complete list:",list);
2032print("Prints first element of the list : ",list[0]);
2033print("Prints elements starting from 2nd to 4th:",list[1:3]);
2034print("Prints elements starting from 3rd element:",list[2:]);
2035print("Prints list two times:",tinylist * 2);
2036print("Prints concatenated lists: ", list + tinylist );
2037#modify the 4th elements in the list
2038print("Before modifying the 4th element in list :",list[4]);
2039list[4]='efgh';
2040print("4th element in list :",list[4]);
2041print(" complete list:",list);
2042#Appending new elements
2043list.append('ijkl');
2044print("After appending list:",list);
2045#deleting an element in list
2046del list[2];
2047print("List :",list);
2048
2049
2050OUTPUT:
2051(‘Prints complete list:’,[‘abcd’,786,2.23,’john’,70.2])
2052(‘Prints first element of the list : ‘,’abcd’)
2053(‘Prints elements starting from 2nd to 4th:’,[786,2.23])
2054(‘Prints elements starting from 3rd element:’,[2.23,’john’,70.2])
2055(‘Prints list two times:’,[123,’john’,123,’john’])
2056(‘Prints concatenated lists: ‘,[‘abcd’,786,2.23,’john’,70.2,123,’john’])
2057(‘Before modifying the 4th element in list:’,70.2)
2058(‘4th element is list:’,’efgh’)
2059(‘complete list:’,[‘abcd’,786.2.23,’john’,’efgh’])
2060(‘After appending list:’[‘abcd’,786,2.23,’john’,’efgh’,’ijkl’])
2061(‘List:’[‘abcd’,786,2.23,’john’,’efgh’,’ijkl’])
2062
2063
2064
2065
2066Program 7: Write a python Program to select odd number from the lists
2067#!/usr/bin/python
2068#program to select odd number from the list
2069a=[11,12,13,14,15,16,17,18,19,20,21,31,44,45,10];
2070print("List is:",a);
2071n=len(a);
2072print("length:",n);
2073i=0;
2074print("Odd number");
2075for i in range(len(a)):
2076if(a[i]%2==1):
2077print(a[i]);
2078
2079
2080OUTPUT:
208111
208213
208315
208417
208519
208621
208731
208845
2089
2090
2091
2092
2093
2094
2095Program 8: For Statement in Python
2096# Measure some strings:
2097a = ['cat', 'window', 'defenestrate']
2098for x in a:
2099print x, len(x)
2100
2101
2102OUTPUT:
2103cat 3
2104window 6
2105defenestrate 12
2106
2107
2108
2109Program 9: The Range() and Len() in Python
2110a = ['Mary', 'had', 'a', 'little', 'lamb']
2111for i in range(len(a)):
2112print (i, a[i])
2113
2114
2115OUTPUT:
21160 Mary
21171 had
21182 a
21193 little
21204 lamb
2121
2122
2123
2124Program 10: Prime Number using Python.
2125
2126for n in range(2, 10):
2127 for x in range(2, n):
2128 if n % x == 0:
2129 print(n, 'equals', x, '*', n/x);
2130 break;
2131 else:
2132 # loop fell through without finding a factor
2133 print(n, 'is a prime number');
2134 break;
2135OUTPUT:
21362 is a prime number
21373 is a prime number
21384 equals 2 * 2
21395 is a prime number
21406 equals 2 * 3
21417 is a prime number
21428 equals 2 * 4
21439 equals 3 * 3
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201RESULT:
2202 Thus the python programs are executed and outputs are verified.
2203
2204Exercise No.: 08
2205
2206Date: 19/09/2018
2207
2208PYTHON MYSQL CONNECTIVITY PROGRAMS
2209
2210Aim:
2211 To demonstrate Python MySQL Connectivity programs with Sql databases.
2212
2213Program 1:CREATE TABLE PROGRAM USING PYTHON
2214#!/usr/bin/python
2215import MySQLdb
2216
2217# Open database connection
2218db = MySQLdb.connect("localhost","root","","student" )
2219
2220# prepare a cursor object using cursor() method
2221cursor = db.cursor()
2222
2223# Drop table if it already exist using execute() method.
2224cursor.execute("DROP TABLE IF EXISTS student ")
2225
2226# Create table as per requirement
2227sql = """CREATE TABLE student ( FIRST_NAME CHAR(20) NOT NULL,LAST_NAME CHAR(20), AGE INT, SEX CHAR(1),
2228cursor.execute(sql)
2229
2230# disconnect from server
2231db.close()
2232
2233Output:
2234Go to terminal:
2235[fosslab@fosslab ~]$ su -
2236Password: (admin123)
2237[root@fosslab ~]# mysql -u root
2238Welcome to the MySQL monitor. Commands end with ; or \g.
2239Your MySQL connection id is 26
2240Server version: 5.1.51 Source distribution
2241Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2242This software comes with ABSOLUTELY NO WARRANTY. This is free software,
2243and you are welcome to modify and redistribute it under the GPL v2 license
2244Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2245mysql> show databases;
2246+--------------------+
2247| Database |
2248+--------------------+
2249| information_schema |
2250| TESTDB |
2251| college |
2252| employee |
2253| mysql |
2254| sample |
2255| steel |
2256| student |
2257| test |
2258+--------------------+
22599 rows in set (0.00 sec)
2260mysql> use student;
2261Reading table information for completion of table and column names
2262You can turn off this feature to get a quicker startup with -A
2263Database changed
2264mysql> connect;
2265Connection id: 27
2266Current database: student
2267mysql> show tables;
2268+-------------------+
2269| Tables_in_student |
2270+-------------------+
2271| EMPLOYEE |
2272| courses |
2273| student |
2274+-------------------+
22753 rows in set (0.00 sec)
2276
2277Program 2: Insert the values in Python that will be automatically updated in database using mysql as follows
2278#!/usr/bin/python
2279import MySQLdb
2280# Open database connection
2281db = MySQLdb.connect("localhost","root","","student" )
2282# prepare a cursor object using cursor() methods
2283cursor = db.cursor()
2284# Prepare SQL query to INSERT a record into the database.
2285sql = """INSERT INTO student(FIRST_NAME,LAST_NAME, AGE, SEX, INCOME)
2286 VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
2287try:
2288# Execute the SQL command
2289 cursor.execute(sql)
2290 # Commit your changes in the database
2291 db.commit()
2292 except:
2293 # Rollback in case there is any error
2294 db.rollback()
2295 # disconnect from server
2296 db.close()
2297Output:
2298mysql> show databases;
2299+--------------------+
2300| Database |
2301+--------------------+
2302| information_schema |
2303| TESTDB |
2304| college |
2305| employee |
2306| mysql |
2307| sample |
2308| steel |
2309| student |
2310| test |
2311+--------------------+
23129 rows in set (0.00 sec)
2313mysql> use student;
2314Reading table information for completion of table and column names
2315You can turn off this feature to get a quicker startup with -A
2316Database changed
2317mysql> connect;
2318Connection id: 27
2319Current database: student
2320mysql> show tables;
2321+-------------------+
2322| Tables_in_student |
2323+-------------------+
2324| EMPLOYEE |
2325| courses |
2326| student |
2327+-------------------+
23283 rows in set (0.00 sec)
2329mysql> select * from student;
2330+------------+-----------+------+------+--------+
2331| FIRST_NAME | LAST_NAME | AGE | SEX | INCOME |
2332+------------+-----------+------+------+--------+
2333| Mac | Mohan | 20 | M | 2000 |
2334|Malar | vizhi |24 |F |15000 |
2335|Rahul | kannan |23 | M |12000 |
2336|Raksha |kumar |24 |F |16500 |
2337+------------+-----------+------+------+--------+
23384row in set (0.02 sec)
2339
2340Program 3: Fetch some useful information from the database
2341#!/usr/bin/python
2342import MySQLdb
2343# Open database connection
2344db = MySQLdb.connect("localhost","root","","student" )
2345# prepare a cursor object using cursor() method
2346cursor = db.cursor()
2347# Prepare SQL query to INSERT a record into the database.
2348sql = "SELECT * FROM STUDENT \WHERE INCOME > '%d'" % (1000)
2349try:
2350 # Execute the SQL command
2351 cursor.execute(sql)
2352 # Fetch all the rows in a list of lists.
2353 results = cursor.fetchall()
2354 for row in results:
2355 fname = row[0]
2356 lname = row[1]
2357 age = row[2]
2358 sex = row[3]
2359 income = row[4]
2360 # Now print fetched result
2361 print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
2362 (fname, lname, age, sex, income )
2363except:
2364 print "Error: unable to fecth data"
2365# disconnect from server
2366db.close()
2367Output:
2368mysql> show databases;
2369+--------------------+
2370| Database |
2371+--------------------+
2372| information_schema |
2373| TESTDB |
2374| college |
2375| employee |
2376| mysql |
2377| sample |
2378| steel |
2379| student |
2380| test |
2381+--------------------+
23829 rows in set (0.00 sec)
2383mysql> use student;
2384Reading table information for completion of table and column names
2385You can turn off this feature to get a quicker startup with -A
2386Database changed
2387mysql> connect;
2388Connection id: 27
2389Current database: student
2390mysql> show tables;
2391+-------------------+
2392| Tables_in_student |
2393+-------------------+
2394| EMPLOYEE |
2395| courses |
2396| student |
2397+-------------------+
23983 rows in set (0.00 sec)
2399mysql> select * from student;
2400+------------+-----------+------+------+--------+
2401| FIRST_NAME | LAST_NAME | AGE | SEX | INCOME |
2402+------------+-----------+------+------+--------+
2403| Mac | Mohan | 20 | M | 2000 |
2404|Malar | vizhi |24 |F |15000 |
2405|Rahul | kannan |23 | M |12000 |
2406|Raksha |kumar |24 |F |16500 |
2407
2408+------------+-----------+------+------+--------+
24094row in set (0.02 sec)
2410
2411Program 4: Updating the values in a database
2412#!/usr/bin/python
2413import MySQLdb
2414# Open database connection
2415db = MySQLdb.connect("","root","","student" )
2416# prepare a cursor object using cursor() method
2417cursor = db.cursor()
2418# Prepare SQL query to UPDATE required records
2419sql = "UPDATE student SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
2420try:
2421 # Execute the SQL command
2422 cursor.execute(sql)
2423 # Commit your changes in the database
2424 db.commit()
2425except:
2426# Rollback in case there is any error
2427 db.rollback()
2428# disconnect from server
2429 db.close()
2430Program 9: Delete some values from database
2431#!/usr/bin/python
2432import MySQLdb
2433# Open database connection
2434db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
2435# prepare a cursor object using cursor() method
2436cursor = db.cursor()
2437# Prepare SQL query to DELETE required records
2438sql = "DELETE FROM student WHERE AGE > '%d'" % (25)
2439try:
2440 # Execute the SQL command
2441 cursor.execute(sql)
2442 # Commit your changes in the database
2443 db.commit()
2444except:
2445 # Rollback in case there is any error
2446 db.rollback()
2447# disconnect from server
2448db.close()
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492RESULT:
2493
2494Thus, the program for python and MySQL connectivity is executed and verified successfully.